ps_arunkumar
Member level 1
RTC interfacing with AT89C52
Hi all,
I have interfaced ds1307 with AT89C52 and display time and date in lcd. I have interfaced 3 switches to controller. I am trying to achieve" When P1^0 is pressed i should be taken to set time and using P1^1 and P1^2, i should be able to set hour and minute. I am trying to compare the time with RTC using conditional loop. When the time matches, in lcd it display as Alarm".
As of now I am able to display time in lcd. When i press the P1^o, its taking me to set time but once i release the button I quits and displays the time again. I am making some logical mistakes in loop. Can you please look after my program and tell me where i am missing the logic.
thanks,
arun
Hi all,
I have interfaced ds1307 with AT89C52 and display time and date in lcd. I have interfaced 3 switches to controller. I am trying to achieve" When P1^0 is pressed i should be taken to set time and using P1^1 and P1^2, i should be able to set hour and minute. I am trying to compare the time with RTC using conditional loop. When the time matches, in lcd it display as Alarm".
As of now I am able to display time in lcd. When i press the P1^o, its taking me to set time but once i release the button I quits and displays the time again. I am making some logical mistakes in loop. Can you please look after my program and tell me where i am missing the logic.
thanks,
arun
Code:
/************************************************
* PIN descriptions
***********************************************
* Hardware : Controller -> AT89C52
* XTAL -> 11.0592 MHz
* I/O : SCL -> P2^6;
* SDA -> P2^7;
* RS -> P2.5
* Enable -> P2.4
* Data4567 -> P2.0,P2.1,P2.2,P2.3
* settime -> P1^0;
* hour_pin -> P1^1;
* min_pin -> P1^2;
* Compiler : KEIL uVision 4.22
* Date : 27/11/2011
*/
#include<at89x52.h>
#include<stdio.h>
#include<ds1307.c>
#include<lcd.c>
sbit settime = P1^0;
sbit hour_pin =P1^1;
sbit min_pin = P1^2;
unsigned char RTC_ARR[7]; /* Buffer for second,minute,.....,year */
unsigned int j, p;
unsigned char hour_update = 0, min_update = 0;
void set_hour()
{
while(hour_pin==1);
hour_update++;
if(hour_update>23)
hour_update=0;
LCD_command(0x80);
sendno2lcd(hour_update);
LCD_putc(':');
}
void set_min()
{
while(min_pin==1);
min_update++;
if(min_update>59)
min_update=0;
LCD_command(0x83);
sendno2lcd(min_update);
}
void display_time()
{
LCD_row1(); LCD_puts("Date:");
LCD_row2(); LCD_puts("Time:");
/* Show Date in format dd/mm/yr */
LCD_command(0x86); /* Set LCD cursor at (1,6) */
send2lcd(RTC_ARR[4]); /* Show date on LCD */
LCD_putc('/');
send2lcd(RTC_ARR[5]); /* Show month on LCD */
LCD_putc('/');
send2lcd(RTC_ARR[6]); /* Show year on LCD */
/* Show Time in format hr:min:sec */
LCD_command(0xC6); /* Set LCD cursor at (2,6) */
send2lcd(RTC_ARR[2]); /* Show hour on LCD */
LCD_putc(':');
send2lcd(RTC_ARR[1]); /* Show min on LCD */
LCD_putc(':');
send2lcd(RTC_ARR[0]); /* Show sec on LCD */
}
/***************************** Main function *************************************/
void main()
{
P0 = 0; P1 = 0;
PowerOn(); /*Initialize LCD */
LCD_row1(); LCD_puts("Date:");
LCD_row2(); LCD_puts("Time:");
/* Setup time and enable oscillator */
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; /* enable oscillator (bit 7=0) */
RTC_ARR[1] = 0x59; /* minute = 59 */
RTC_ARR[2] = 0x11; /* hour = 11 ,24-hour mode(bit 6=0) */
RTC_ARR[3] = 0x04; /* Day = 1 or sunday */
RTC_ARR[4] = 0x23; /* Date = 23 */
RTC_ARR[5] = 0x11; /* month = November */
RTC_ARR[6] = 0x11; /* year = 2011 */
WriteRTC(&RTC_ARR[0]); /* Set RTC */
while(1)
{
ReadRTC(&RTC_ARR[0]);
display_time();
if(settime==1)
{
for(p=0;p=10;p++); //debounce check
if(settime==1)
{
LCD_clear();
LCD_puts("Time set");
for(j=0;j<1000;j++);
if(hour_pin==1)
set_hour();
if(min_pin==1)
set_min();
}
LCD_clear();
}
if(RTC_ARR[2]==hour_update)
{
if(RTC_ARR[1]==min_update)
{
LCD_clear();
LCD_puts("Alarm");
}
}
}
}
Last edited: