I made this DS1307 based clock to run with an ATMega8 running at 8 MHz internal clock. Now, the clock works flawlessly, but the LM35 connected to the circuit to show the room temperature doesn't work.
I used libraries from Extreme Electronics.
LCD is connected (D4 to D7) at PD3 to PD6. RS at PB2, RW at PB0, E at PD7.
Keys:
Enter Menu: PC2
Shift cursor left: PC0
Shift cursor right: PC1
Exit: PC6
C5, C4 used by DS1307.
This leaves only PC3 (ADC channel 3) to use with LM35. So I connected the LM35 output with PC3.
But it always displays 255C (AREF voltage itself). Even when I pullup PC3 to ground, the temp doesn't show 0C, rather it always shows 255C.
The compilation completes successfully without any error/warning.
What am I doing wrong?
Is it possible that PC3 is not being used by ADC? Do I need to use an ATMega16 for this kind of project?
#include <avr/io.h> #include <avr/pgmspace.h> #include <util/delay.h> #include "lib\lcd\lcd_hd44780_avr.h" #include "lib\rtcc\clock.h" #include "lib\keypad\keypad.h" typedef bool uint8_t; #define TRUE 1 #define FALSE 0 bool SetTime(); int main(void) { int tempC; ADCSRA = (1<<ADEN)|(7 << ADPS0); // Enable, div128 ADMUX = (3 << REFS0)|(1<<ADLAR)|(3<<MUX0); // VREF=2.56V, 8-bit, channel #0 is on PA0 pin //Initialize the LCD module LCDInit(LS_NONE); //Write a intro text LCDWriteFStringXY(0,0,PSTR(" Bagho's")); LCDWriteFStringXY(0,1,PSTR(" Clock")); _delay_ms(2000); LCDClear(); //Initialize the Clock Module if(!ClockInit()) { //If we fail to initialize then warn user LCDClear(); LCDWriteString("Error !"); LCDWriteStringXY(0,1,"DS1307 Not Found"); while(1); //Halt } //Initialize the keypad KeypadInit(); //Now Read and display time char Time[12]; //hh:mm:ss AM/PM while(1) { ADCSRA |= (1<<ADSC); // Start Conversion while (ADCSRA & (1<<ADSC)); // wait for completion tempC = ADCH; LCDClear(); //Loop, read time from chip and show on lcd while(1) { //Get the Current Time as a String if(!GetTimeString(Time)) { /* If return value is false then some error has occurred Check ->DS1307 Installed Properly */ LCDClear(); LCDWriteString("xBoard MINI v2.0"); LCDWriteStringXY(0,1,"I2C i/o Error !"); while(1);//halt } //Display it LCDWriteIntXY(0,0, tempC, 3); LCDWriteStringXY(4,0, "Degrees") LCDWriteStringXY(2,1,Time); //Check input for ENTER key uint8_t key = GetKeypadCmd(FALSE); if(key==KEY_ENTER) { //Enter time setup SetTime(); break; } _delay_ms(500); } } } bool SetTime() { uint8_t hr,min,sec,am_pm; //Get Seconds sec=GetSecond(); //Get Minute min=GetMinute(); //Get Hour hr=GetHour(); //Get AM/PM am_pm=GetAmPm(); //If Hour is 0 make it 12, as 00:00:00 invalid time if(hr==0 && min==0 && sec==0) hr=12; uint8_t sel=0; bool done=FALSE; while(!done) { LCDClear(); LCDWriteString("00:00:00 <OK>"); LCDWriteIntXY(0,0,hr,2); LCDWriteIntXY(3,0,min,2); LCDWriteIntXY(6,0,sec,2); if(am_pm) { LCDWriteStringXY(9,0,"PM"); } else { LCDWriteStringXY(9,0,"AM"); } //Draw Pointer LCDWriteStringXY(sel*3,1,"^^"); uint8_t key=GetKeypadCmd(1); switch(key) { case KEY_RIGHT: if(sel==0) { //Hour if(hr==12) { hr=1; } else { hr++; } } if(sel==1) { //Min if(min==59) { min=0; } else { min++; } } if(sel==2) { //Sec if(sec==59) { sec=0; } else { sec++; } } if(sel==3) { //AM-PM if(am_pm==0) { am_pm=1; } else { am_pm=0; } } if(sel == 4) { //OK done=TRUE; } break; case KEY_LEFT: if(sel==0) { //Hour if(hr==1) { hr=12; } else { hr--; } } if(sel==1) { //Min if(min==0) { min=59; } else { min--; } } if(sel==2) { //Sec if(sec==0) { sec=59; } else { sec--; } } if(sel==3) { //AM-PM if(am_pm==0) { am_pm=1; } else { am_pm=0; } } if(sel == 4) { //OK done=TRUE; } break; case KEY_ENTER: //Change Selection if(sel==4) sel=0; else sel++; break; } } //Now write time back to RTC Module if(!SetSecond(sec)) { LCDClear(); LCDWriteString("Error !"); LCDWriteStringXY(0,1,"Cannot Set Time"); return 0; } if(!SetMinute(min)) { LCDClear(); LCDWriteString("Error !"); LCDWriteStringXY(0,1,"Cannot Set Time"); return 0; } if(!SetHour(hr)) { LCDClear(); LCDWriteString("Error !"); LCDWriteStringXY(0,1,"Cannot Set Time"); return 0; } if(!SetAmPm(am_pm)) { LCDClear(); LCDWriteString("Error !"); LCDWriteStringXY(0,1,"Cannot Set Time"); return 0; } //Show Message LCDClear(); LCDWriteString("Message !"); LCDWriteStringXY(0,1,"Main Time Set"); uint8_t i; for(i=0;i<100;i++) _delay_loop_2(0); return 1; }