/********************************************* * Prof. Gottardo Marco * * simulazione d'esame di qualifica * * anno formativo 2009/2010 * * distributore automatico di bevande calde * * PIC 16F877A caffettiera.c * *********************************************/ #include #include #include // #include #include #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; }