reyyan
Newbie level 1
hello, i am working on a project to detect the voltages of battery 1 and battery 2 and a temperature sensor and display on lcd, so i just made a code it detect battery 1 voltage and battery 2 voltages properly but temperature sensor is unable to give me correct value, if i there is change in battery 1 voltage then value of adc of temperature also change, here i am insert my code, kindly help me to fix it.
Code C++ - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 #include<avr/io.h> #include<util/delay.h> #include <string.h> #include <stdlib.h> #include <stdio.h > char display[50]; adc_read(unsigned int ch) { ch&=00000011; ADMUX=(ADMUX&0xF8)|ch; delay_us(1); ADCSRA |= (1<<ADSC); while((ADCSRA & (1<<ADIF))==0); } void delay_us(int d) { unsigned int x; for(x=0 ; x<d ; x++) _delay_ms(10); } int ready() { delay_us(10); } void LCD_Pulse_E(int t) { PORTB|= 0b00000100 ; // E = 1; delay_us(t); PORTB &= 0b11111011 ; // E = 0; delay_us(t); } void lcdcommand(unsigned char cmnd) { ready(); PORTD=cmnd; PORTB=0b11111000; LCD_Pulse_E(1); } void lcddata(unsigned char data) { ready(); PORTD=data; PORTB|=0b11111001;// RS=1; RW=0; LCD_Pulse_E(1); } void lcd_init() { lcdcommand(0x38); lcdcommand(0x0C); lcdcommand(0x01); delay_us(5); lcdcommand(0x06); //lcdcommand(0x80); /*void lcd_goto_yr(unsigned char y, unsigned char r) { unsigned char firstAddress[] = {0x80,0xC0,0x94,0xD4}; lcdcommand(firstAddress[y-1] + r-1); delay_us(1000); }*/ } void lcdprint(char * str) { unsigned char i=0; while (str[i]!=0) { lcddata(str[i]); i++; } } int main(void) { DDRB=0xFF; DDRD=0xFF; DDRA=0x00; DDRC=0xFF; unsigned int adc_result0,adc_result1,adc_result2; unsigned int volt1,final1,volt2,final2,temp; lcd_init(); ADMUX=0b11100000; // ADC 0,internal 2.56V as Vref, left justify ADCSRA=0b10000111; //lcdcommand(0xC0);// Cursor at Line 2, Position 0 //delay_us(500); while(1) { adc_read(0); adc_result0=ADCH; volt1=adc_result0; final1=volt1*0.4807; sprintf(display,"BATT 1:%d",final1); lcdprint(display); adc_read(1); adc_result1=ADCH; volt2=adc_result1; final2=volt2*0.4807; sprintf(display,"BATT 2:%d",final2); lcdprint(display); lcdcommand(0xC0); adc_read(2); adc_result2=ADCH; sprintf(display,"TEMP:%d",adc_result2); lcdprint(display); lcdcommand(0x02); } return 0; }
Last edited by a moderator: