Can't get LM35 to work with this circuit. Please help.

Go To Last Post
35 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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;

}

 

This topic has a solution.

Last Edited: Sat. Jun 20, 2015 - 12:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Three things I can think of....

 

1) what is the voltage on the PIN that you have the LM35 connected to?

2) what is the LM35's VCC?

3) You have a lot of While(1) loops in that code you posted.  are you sure you are not stuck in a loop somewhere?

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply.

 

1. The voltage on the PC3 pin without LM35 connected is 3.8Volts. Even when I touch the Ground to that pin, the temperature is shown 255C. The clock ticks fine.

2. The LM35 draws power from the LM7805 used for the MCU.

3. Might be. I couldn't find a more efficient, better way to put down this code. 

 

Is it possible that the PC3 is not used by the ADC because C ports are used at inputs? Do I need an ATMega16 in this case?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wait a sec, I missed the PORTC as inputs.  YES that will cause problems.  Can you move your buttons elsewhere?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you. I understand now. I'm trying to move the buttons but the clock can't be moved, right? Because ADC and SCL SDA coincide. 

Last Edited: Sat. Jun 20, 2015 - 02:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just edited the keypad.h to move the buttons to Port B. But the problem persists. 255C.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just added a number counter and adder with each cycle of the while loop, just to make sure there weren't any loop sticking. The number adds up every second. So the while loop is working where I wanted to read the ADC.

 

I guess I need ATMega16 where the ADC port is free. Right?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No SCA and SCL are on different pins.  ADC3 is the port pin your LM35 is connected to.

 

Also, you MUST disable the Pull up resistor I think you have enabled on the pin you have the LM35 connected to.  THats why you are getting 255 all the time.

 

After looking at your OP again, you could possibly put your pushbuttons back on port c 0..2 but for now leave them on B and disable the pullup resistor on PORTC_3

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the ATmega8(L) datasheet:

 

ADC3 – Port C, Bit 3
PC3 can also be used as ADC input Channel 3. Note that ADC input channel 3 uses analog
power.

 

Am I looking at the wrong datasheet?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nope.  That is correct information

 

Also, do you have AVCC connected to +5v, and a capacitor on VREF?

 

YOU still must disable the pullup resistor for that pin.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bagho,

 

With the output of the LM35 not connected to the PC3 (ADC3 input), using your multimeter, what is the voltage on the LM35 output. At 25 degrees C, it should be about 250 millivolts because the LM35's output should be about 10 millivolts per degree C.

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I have AVCC connected to VCC and there's a VREF Capacitor.

 

But can you please guide me how to disable the pull up resistor on port C3? Not really sure.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

Bagho,

 

With the output of the LM35 not connected to the PC3 (ADC3 input), using your multimeter, what is the voltage on the LM35 output. At 25 degrees C, it should be about 250 millivolts because the LM35's output should be about 10 millivolts per degree C.

 

Cheers,

 

Ross

 

 

THis is puzzling though:

1. The voltage on the PC3 pin without LM35 connected is 3.8Volts. Even when I touch the Ground to that pin, the temperature is shown 255C. The clock ticks fine.

I think he has something in his code that is screwing things up. - JMO

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply, Ross.

 

The LM35 output without connecting it is around 320 mV.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You think so? I copied a part of the code from somewhere. Especially the adjustment areas.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So it is about 32 degrees C at the moment for you? In which case your LM35 is functioning correctly. One less thing to check off your list.

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you suggest me to go the digital way? I have a DHT-11 lying around. But I do not know how to interface it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, it's exactly 32C here! smiley So the LM35 is precise.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK with a 320mV input and a 2.56 volt reference, you should be getting an ADC value of about (0.320/2.560)*1024 = 128.

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, I used 8 bit ADC. So we get 32.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you think if I made another circuit with ATMega16, this code would work?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
tempC = ADCH;

try ADC perhaps

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Alright, thank you! Trying.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All the same, I'm sorry. I just assembled this circuit on my project board with ATMega16 and wrote the hex file to the ATMega16. All I'm getting is a blank LCD. Any idea why this is happening?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you saying that you wrote the ATMega8 hex file to an ATMega16 chip?

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just changed the type of chip to Atmega16 in the top middle of the editor of Atmel Studio. Everything else was unchanged. Not sure if that worked.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I just changed the type of chip to Atmega16 in the top middle of the editor of Atmel Studio. And then recompiled?

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I did recompile. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I couldn't get the LM35 to work. I'm thinking of using a DHT-11 to connect with it. Humidity will be an added bonus. 

 

In the present circuit, most B ports are free (with only two B ports used for LCD control pins.) What if I connected the DHT11 with one of the B port's pins? Or will it not work because B is already the output for LCD, and it won't accept digital input?

 

 

Last Edited: Sat. Jun 20, 2015 - 04:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes,   a mega8 or mega328 will use PC4, PC5 for I2C.    The spare analog pins are on PC0-PC3

A mega16 uses PC0, PC1 for I2C.   The analog is on PA0-PA7

 

You must always design your project for the appropriate pins.

Your DHT11 library code might use a specific pin.

 

Personally,   I would avoid anything from Extreme Electronics like the plague.    However,   you can always try their code.

 

I would buy an Arduino UNO clone.    There is working code for almost every hardware chip.

You can always write your own C code for the UNO.    At least you can compare it with the working Arduino code.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much for your advice.

 

Yes, that's why I was thinking of using an ATMega16. The Analogs are untouched in this program.

 

Yes, I'll be buying a clone Arduino. I hope the learning curve isn't steep. 

 

Extreme Electronics is to be avoided? Okay. I shall try finding codes elsewhere.

 

Psst: I also think there are a lot of problems in circuit integrity in EE and the writer of codes is extremely arrogant and rude. He inserts the following lines into his codes:

 

If this helped you, please leave a 5 star review @ http://ex-tr.me/review

I know most Indians won't do this! They just know how to take, but not how to give!
But we have your mobile number and email with us! And we maintain a database of
all mobiles and email IDs. And it takes only a click to ban anyone. Once banned,
the user will not be able to download anything from our servers!

Am I threatening you? Well, the answer is yes! 

After 7 years of publishing my works online and helping more than 3000 persons 
on daily basis, and still finding lack of loyalty in them, I was forced to develop 
this system.

I spent lots of time and money to develop this system just to keep *****rs away!

 

PS: It took me a while to type. I'm not abandoning this thread. smiley

Last Edited: Sat. Jun 20, 2015 - 01:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All the best on your two threads

 

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wink Thanks, Jim!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All I'm getting is a blank LCD. Any idea why this is happening?

 

I'm not sure why you decided to change micros.

 

You now took a step backwards...

 

Do you have a contrast pot hooked up, and have you adjusted the contrast?

 

You need to recheck your wiring carefully.

 

On the uC you need all Vcc and AVcc tied to Vcc, and all Ground and AGround pins tied to Ground.

You also need a by-pass cap, 0.1 uF, connected from each Vcc and AVcc to Ground, as close to the chip as possible. 

 

Hook up your programmer and see if you can read the uC's Signature.  Report back.

No sense working on the LCD until you can read the Signature.

 

Then hook up an LED and flash it at a given rate.

This proves the uC is running your code, and is running at the clock speed you think it is running at.

 

Then work on the LCD display.

 

Then hook up another pot, 10K, from Vcc to Ground, wiper to the analog input pin.

Put another 0.1 uF cap from the wiper to Ground.

 

Use a DMM to read the wiper voltage, and work on a simple program to read the ADC and display the ADC value on the LCD.

Compare the expected ADC value to the known, (measured) input voltage.

 

When this works, then add the conversion code to convert the ADC value to a temperature.

 

Then add back in the Clock code.

 

One step at a time and this project is doable.

 

JC

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yay, yay and triple YAY! We finally did it!

 

I reassembled the circuit on an ATMega16 project board. The blank LCD I was getting was because of a small bit of shredded copper wire stuck, which was shorting in the back of the LCD Board. Now whoever thought of being fooled like that!

 

A million thanks to you, wonderful people.

 

Couldn't have done it without your support!

 

Here's the final code in case it ever benefits anyone.

 







#include <avr/io.h>

#define F_CPU 8000000
#include <avr/pgmspace.h>
#include <util/delay.h>

#include "lcd_hd44780_avr.h"
#include "clock.h"
#include "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)|(0<<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("     Gloria,"));
	LCDWriteFStringXY(0,1,PSTR("In Excelcis Deo"));
	
	_delay_ms(3000);
	
	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
			ADCSRA |= (1<<ADSC);          // Start Conversion
			while (ADCSRA & (1<<ADSC));   // wait for completion

			tempC = ADCH;
			LCDClear();
			LCDWriteStringXY(7,0, "Celsius")
			LCDWriteIntXY(3,0, tempC, 2);
			
			
			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;

}