// PIC Project Board // PIC18F2550 + LTC2400 + LCD + USB // Sample code demonstrates the use of sigma-delta converter. // The program prints analog input 0-2.5V on LCD in micro Volts unit // source code was compiled with Mikro-C compiler // Copyright 2006 Wichit Sirichote, kswichit@kmitl.ac.th #define ADC_CS1 PORTC.F0 // output bit #define ADC_SDO PORTC.F1 // input bit #define ADC_SCK PORTC.F2 // output bit char *text = "PICProject Board"; unsigned long x1,x2,x3,x4,x5; /* read 32-bit data from LTC2400 */ unsigned long read_ADC1(void) { char k; long n; n= 0; ADC_CS1 = 0; for(k=0; k<32; k++) { n<<= 1; ADC_SCK = 1; n |= ADC_SDO; ADC_SCK = 0; } ADC_CS1 = 1; n&=0x01fffffff; // maskout sign bit n>>=4; // get 24-bit conversion result return n; } /* 5-point moving average */ // return data that scaled with reference voltage in uV unit unsigned long filter_ADC(void) { x5 = x4; x4 = x3; x3 = x2; x2 = x1; x1 = read_ADC1(); return ((((x1+x2+x3+x4+x5)/5)*148)/100); // x 149 E-9 convert to 2.479V } void main() { char buffer[20]; long d; ADCON1 = 0x0E; TRISB = 0; // PORTB is output PORTC = 0xFF; TRISC = 0x02; // PORTC.1 is input bit Lcd_Init(&PORTB); // Initialize LCD connected to PORTB Lcd_Cmd(Lcd_CLEAR); // Clear display Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off Lcd_Out(1, 1, text); while(1) { PORTC.F7 ^= 1; d = filter_ADC(); sprintl(buffer,"%08ld x0.1%cV",d,0xe4); Lcd_Cmd(LCD_SECOND_ROW); Lcd_Out_CP(buffer); Delay_ms(200); } }