Simulazione esame.

Programmazione PIC in C16.

Classe terza elettrotecnici 2009/2010

22 Aprile 2010

Simulazione d’esame  Elettronica:Programmazione e progettazione sistemi a Microprocessori.

 

Cognome …………………………………………Nome…………………………………………

 

Si consideri il pannello comandi serigrafato disegnato qui sopra. Si produca e si compili un programma in C per PIC 16F877A, da riportare sul retro del foglio diviso in due colonne, che gestisca almeno i pulsanti di selezione caffè, cappuccino, the, che richiamino le tre funzioni omonime. Come verifica del fatto che si è entrati nella procedura di selezione si accendano rispettivamente RB0,RB1,RB2. Un canale analogico mostra la temperatura interna dell'acqua nel display LCD.

Inviare il progetto MpLab a prof.gottardo@tiscali.it dopo averlo impaccato con l’apposito tasto presente sul software della Microchip.

Tempo a disposizione 1 ora.

-------------------------------------------------------------------------

Collegamento dell'Hardware

Nell'immagine riporto il collegamento del display LCD (due righe per 16 caratteri) pilotato in modalità 8 bit.

download dello schema elettrico manoscritto

Al PORT B dovranno essere collegati i pulsanti di comando tramite resistenze di PULL down, ovvero all'azione premuto corrisponde un uno logico.

(Volutamente il disegno elettrico sovrastante non è completo così che possa ancora essere utilizzabile come esercitazione in classe).

 

---------------------------------------------------------------------------

bozza della soluzione:

//(Volutamente il sorgente non è completo, e riferito correttamente alla pulsantiera così che possa ancora essere utilizzabile come

 //esercitazione in classe).

/*********************************************
* Prof. Gottardo Marco *
* simulazione d'esame di qualifica *
* anno formativo 2009/2010 *
* distributore automatico di bevande calde *
* PIC 16F877A caffettiera.c *
*********************************************/


#include<pic.h>
#include<Delay.h>
#include<math.h>
// #include<stdlib.h>
#include<stdio.h>
#define EN RE2
#define RW RE1
#define RS RE0

#define P8 RB7 //PORT B tutti ingressi
#define P7 RB6
#define P6 RB5
#define P5 RB4
#define P4 RB3
#define P3 RB2
#define P2 RB1
#define P1 RB0

#define caffe RC0 //PORT C tutti ingressi
#define cappuccino RC1

// ho dichiarato la mia float to string
char * myftoa(float fl, int * sts);

void CLOCK();
void CLEAR();
void CONFIGURA();
void INIZIALIZZA();
void splash();
void VAI(unsigned char r, unsigned char c);
void SCRIVI(char lettera);
void FRASE(unsigned char l, unsigned char n, const char* msg1);
void CONVERSIONE();

void main(){
CONFIGURA();
INIZIALIZZA();
CLEAR();
DelayMs(10);
splash();
while(1){

//CONVERSIONE();
//FRASE(1,1," selezione caffe ");
//if (caffe==1){
//FRASE(1,1,"selezione caffe");
//}
//if (cappuccino==1){
//FRASE(1,1,"selezione");
//FRASE(2,1,"cappuccino");
//}
DelayMs(10);}
}


void CLEAR(){RS=0; RW=0; PORTD=0x0F;
CLOCK();
DelayMs(1); }
void CLOCK(){EN=1;
DelayMs(1);
EN=0;}
void CONFIGURA(){
TRISB=ob11111111;
TRISC=0b1111;//primo nibble ingressi, lascio libera la seriale
TRISD=0;
TRISE=0;
PORTD=0;
PORTE=0;
OPTION=0x80;
ADCON1=0x02;
}

void INIZIALIZZA(){RS=0; RW=0; PORTD=0x30;
DelayMs(15);
CLOCK();
DelayMs(10);
CLOCK();
DelayMs(10);
CLOCK();
DelayMs(10);
RS=0; RW=0; PORTD=0x38;
CLOCK();
DelayMs(1);
RS=0; RW=0; PORTD=0x08;
CLOCK();
DelayMs(1);
RS=0; RW=0; PORTD=0x0C;
CLOCK();
DelayMs(1);
RS=0; RW=0; PORTD=0x01;
CLOCK();
DelayMs(1);
RS=0; RW=0; PORTD=0x03;
CLOCK();
DelayMs(1);}
void SCRIVI(char lettera){RS=1; RW=0; PORTD=(lettera);
CLOCK();
DelayUs(50);}
void VAI(unsigned char r, unsigned char c){unsigned char a=0;
RS=0; RW=0;
if(r==1){a=c+127;}
if(r==2){a=c+191;}
PORTD=(a);
CLOCK();
DelayUs(50);}
void FRASE(unsigned char l, unsigned char n, const char* msg1){while(n<=16){VAI(l,n);
SCRIVI(*msg1++);
n++;}
n=1;}

void splash(){//scrive su LCD la presentazione per alcuni secondi
FRASE(1,1," Gottardo Marco ");
FRASE(2,1,"G-Tronic Robotic");
DelayMs(255);
DelayMs(255);
DelayMs(255);
DelayMs(255);
DelayMs(255);
DelayMs(255);

};

void CONVERSIONE()
{
unsigned int myaddress = 0;
unsigned int * sttus;
char * mybuf;
char thechar;
float ain = 0;



TRISA=1;
ADCON0=0b11000001;
ADCON1=0b10000010;
ADGO=1;
while(ADGO){}

// memorizzo la parte alta dell'indirizzo
myaddress= ADRESH;
// mi recupero il valore calcolato nella varibile float
ain = ((myaddress*256)+ADRESL)*4,887;
// a questo punto abbiamo il float da visualizzare

mybuf = myftoa(ain, &sttus);
int y = 1;
while (thechar = *mybuf++)
{
VAI(2,y);
SCRIVI(thechar);
y++;
}
}


char * myftoa(float f, int * status)
{
static char buf[17];
char * cp = buf;
unsigned long l, rem;

if(f < 0) {
*cp++ = '-';
f = -f;
}
l = (unsigned long)f;
f -= (float)l;
rem = (unsigned long)(f * 1e6);
sprintf(cp, "%lu.%6.6lu", l, rem);
return buf;
}
 

download file cafettiera.c

librerie da utilizzare Delay.h   Delay.c

  Pagina precedente