/* ADC.C LCD connected to Port B : PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 D7 D6 D5 D4 EN RS NC NC ADC connected to Port D : PD6 PD5 PD4 PD3 PD2 PD1 PD0 CS SCLK DIN DOUT BUSY NC NC The R/W pin of LCD is connected to ground, because we will never read any data from LCD, we will always write into it. */ #include #include #include void init_lcd () ; void display_lcd () ; void lcd_cmd1 (unsigned char) ; void lcd_cmd (unsigned char) ; void lcd_dat1 (unsigned char) ; void lcd_dat (unsigned char) ; #define TRUE 1 #define FALSE 0 unsigned char index ; unsigned long int ind ; unsigned int cw ; char lcd_buff[33] = "1: 2: " ; unsigned char count; unsigned int mask, adc_value ; unsigned int divisor, mask1 ; char negative, overflow ; void init_lcd() { lcd_cmd1((unsigned char)3); delay(6000); /*4.5 ms*/ // assuming 4 MHZ crystal lcd_cmd1((unsigned char)3); delay(200); /*200 micro sec*/ lcd_cmd1((unsigned char)3); delay(200); /*200 micro sec*/ lcd_cmd((unsigned char)0x28); /*DL=0:4 bit;N=1:2 line,F=0:5x7*/ lcd_cmd((unsigned char)8); /*display, cursor, blinking off*/ lcd_cmd((unsigned char)0x0c); /*display on*/ lcd_cmd((unsigned char)6); /*entry mode set increment*/ } void display_lcd () { /* sends lcd_buff to display */ lcd_cmd(0x80); // cursor set to first line first char position for(count= 0 ; count < 32; count++) { if(count == 16) { lcd_cmd((unsigned char)0xc0) ; // cursor to second line first char position } if(lcd_buff[count]) lcd_dat(lcd_buff[count]); else lcd_dat(' ') ; } } void lcd_cmd (unsigned char tempchar) { /* This function sends the contents of global variable 'tempchar' as a command to LCD */ lcd_cmd1((tempchar / (unsigned char)16)); // MS nibble lcd_cmd1((tempchar & (unsigned char)0x0f)); // LS nibble delay(200) ; } void lcd_cmd1 (unsigned char data) { /* sends the 4 LSBits of Acc. as a (half) command to the LCD */ PORTB = (data << (unsigned char) 4) & (unsigned char) 0xf0 ; // A0 and CS low PORTB |= (unsigned char) 8 ; // CS high PORTB &= (unsigned char) 0xf7 ; // CS low } void lcd_dat1 (unsigned char data) { PORTB = ((data << (unsigned char) 4) & (unsigned char) 0xf0) | (unsigned char) 0x04 ; // A0 high and CS low PORTB |= (unsigned char) 8 ; //CS high PORTB &= (unsigned char) 0xf7; // CS low } void lcd_dat (unsigned char tempchar) { /* This function sends the contents of global variable 'tempchar' as data to LCD */ lcd_dat1((tempchar / (unsigned char)16)) ; lcd_dat1((tempchar & (unsigned char)0xf)) ; delay(200) ; } unsigned char check_for_ready () { for(ind = 0 ; ind < 0xffff ; ind++) { if ((PIND & (unsigned char)4)) return TRUE ; } return FALSE ; } oid read_adc (unsigned int control_word) { adc_value = 0 ; if (!check_for_ready()) return ; // check the busy status - TRUE -> ok delay(0xffff) ; // a big delay PORTD &= (unsigned char) 0xbf ; // cs - chip select low - start conversion mask = 0x8000 ; for(index = 0 ; index < 16 ; index++) { // SCLK - high if(control_word & mask) PORTD |= (unsigned char) 0x10 ; // place data else PORTD &= (unsigned char) 0xef ; PORTD |= (unsigned char) 0x20 ; // sclk = 1 adc_value = (adc_value << 1) ; if((PIND & (unsigned char)8)) adc_value |= 1 ; // read data mask >>= 1 ; // SCLK - low PORTD &= (unsigned char) 0xdf ; // sclk } PORTD |= (unsigned char) 0x40 ; // cs - chip select high if (adc_value & 0x4000) overflow = 'E' ; else overflow = ' ' ; adc_value &= mask1 ; if(adc_value & 0x8000) { adc_value = -(adc_value | ((~mask1) & 0xf000)) ; // adc_value = -(adc_value | 0x7000) ; negative = '-' ; if (overflow == ' ') overflow = 'E' ; else overflow = ' ' ; } else negative = ' ' ; adc_value = (adc_value * 2500ul) / divisor ; } void main() { DDRB = 0xff ; // define PORTB as output DDRD = 0x70 ; // define PORTD pins direction init_lcd(); // initialise the LCD display cw1=0x8c80; //control word for channel1 cw2=0x8c90; //control word for channel2 divisor=16384; mask1=0xbfff; read_adc(cw1 | 0x0c) ; //offset correction read_adc(cw1 | 0x8) ; //gain caliberation read_adc(cw1 | 4) ; //offset null delay(0xffff); read_adc(cw2 | 0x0c) ; //offset correction read_adc(cw2 | 0x8) ; //gain caliberation read_adc(cw2 | 4) ; //offset null while(1) { read_adc(cw) ; ultoa(adc_value, lcd_buff+19, 5) ; lcd_buff[18] = negative ; lcd_buff[31] = overflow ; read_adc(cw|0x10) ; ultoa(adc_value, lcd_buff+3, 5) ; lcd_buff[2] = negative ; lcd_buff[15] = overflow ; display_lcd() ; delay(0xffff) ; // some delay } }