// // CMeter.c // This program displays the voltage at the input to the ADC // in Volts as well as displays a bar graph at the bottom of // the LCD terminal display using user defined characters. // // Ted Rossin // 6-27-2003 // 6-30-2003 // #include #include #include "lcd.h" #include "timer.h" #include "serial.h" unsigned char SampleADC(void); void SetCursor(unsigned char x,unsigned char y); void InitTerminal(void); void DisplayBarGraph(unsigned char Count); char bank1 Buf[21]; // Some extra memory from bank1 int main() { unsigned int i; int voltage,whole,frac; unsigned char Raw; TRISB = 0x00; // Porb B[7:0] is output TRISA = 0x01; // Port A[0] is input for A/D ADCON1 = 0x0e; // (left justified) 1 A/D channel ADCON0 = 0x81; // FOSC/32, channel=0, ADON=1 PORTB = 0xaa; /* turn on some lights */ InitRS232(); InitTimer(); InitTerminal(); printf("%c",TERM_CLEAR); for(i=0;0<1;i++){ SetCursor(0,1); Raw = SampleADC(); voltage = ((200*(unsigned int)Raw)/255)*25; whole = voltage/1000; frac = (voltage - (whole*1000) + 5)/10; sprintf(Buf,"ADC=%d.%2dV Raw=0x%2x\n",whole,frac,Raw); if(Buf[6]==' ') Buf[6] = '0'; // Fix leading zero bug if(Buf[16]==' ') Buf[16] = '0'; // Fix leading zero bug printf(Buf); printf("\tLoop=%u \n",i); DisplayBarGraph(voltage/84); Waitms(10); PORTB = i&0xff; } } void DisplayBarGraph(unsigned char Count) { unsigned char i,NumWhole,NumFrac; NumWhole = Count/3; NumFrac = Count - (NumWhole*3); for(i=0;i