#include<reg51.h>
sbit e1=P2^6;
sbit e2=P2^7;
sbit e3=P2^3;
sbit led=P2^2;
sbit e4=P1^5;
sbit e5=P1^7;
sbit e6=P1^6;
sbit e7=P3^4;
sbit e8=P2^0;
sbit e9=P2^1;
sbit e10=P0^6;
sbit e11=P0^7;
sbit e12=P0^5;
sbit set=P1^0;
sbit mov=P1^1;
sbit inc=P1^2;
sbit dec=P1^3;
sbit ent=P1^4;
sbit alarmset=P3^7;
bit c;
bit d;
bit e;
void all_disp();
void time_set();
alarm_chk();
alarm_chk1();
alarm_chk2();
alarm_chk3();
alarm_chk4();
alarm_chk5();
alarm_chk6();
alarm_chk7();
void alarm_set();
sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;
void Delay(unsigned int);
void del();
void msdelay(unsigned int value);
void alarm_bel();
void display_alarmd1(unsigned char value);
void display_alarmd2(unsigned char value);
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char);
Send2lcd(unsigned char);
void Seg8_decimal2(min);
void Led8decimal2(hour);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
unsigned char i,j,a[10],cur=0;
unsigned char sec,hour,min,date,month,year,sec1,hour1,min1,sec2, hour2,min2,event,hh,mm,ss,dd,mn,yy,asec,amin,ahour,asec1,amin1,ahour1,asec2,amin2,ahour2,asec3,amin3,ahour3,asec4,amin4,ahour4,asec5,amin5,ahour5,asec6,amin6,ahour6,asec7,amin7,ahour7;
unsigned char count[13]={0x86,0x89,0x8c,0xc6,0xc9,0xcc,0x80,0x81,0x82,0x83,0x84,0x85,0x86};
unsigned char dec_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
}
void all_disp()
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
min=Send2lcd(min);
hour=Send2lcd(hour);
Seg8_decimal2(min);
Led8decimal2(hour);
}
void main()
{
Delay(65000);
Delay(65000);
Rtc_Init();
//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);
while(1)
{
all_disp();
alarm_chk();
alarm_chk1();
alarm_chk2();
alarm_chk3();
alarm_chk4();
alarm_chk5();
alarm_chk6();
alarm_chk7();
if(!set)
time_set();
if(alarmset==1)
alarm_set();
}
}
void time_set()
{
Delay(65000);
all_disp();
cur=3;
while(ent)
{
if(mov==0)
{
while(!mov);
cur++;
if(cur==6)
cur=3;
}
else if(count[cur]==0x86)
{
if(inc==0)
{
while(!inc);
date++;
if(date>=32)
date=1;
}
else if(dec==0)
{
while(!dec);
date--;
if(date==0xff)
date=31;
}
}
else if(count[cur]==0x89)
{
if(inc==0)
{
while(!inc);
month++;
if(month>=13)
month=1;
}
else if(dec==0)
{
while(!dec);
month--;
if(month==0xff)
month=12;
}
}
else if(count[cur]==0x8c)
{
if(inc==0)
{
while(!inc);
year++;
if(year>99)
year=0;
}
else if(dec==0)
{
while(!dec);
year--;
if(year==0xff)
year=99;
}
}
else if(count[cur]==0xc6)
{
if(inc==0)
{
while(inc==0);
hour++;
Led8decimal2(hour);
if(hour>=24)
hour=0;
}
else if(dec==0)
{
while(dec==0);
hour--;
Led8decimal2(hour);
if(hour==0)
hour=23;
}
}
else if(count[cur]==0xc9)
{
if(inc==0)
{
while(inc==0);
min++;
Seg8_decimal2(min);
if(min>=60)
min=0;
}
else if(dec==0)
{
while(dec==0);
min--;
Seg8_decimal2(min);
if(min==0)
min=59;
}
}
else if(count[cur]==0xcc)
{
if(inc==0)
{
while(inc==0);
sec++;
if(sec>=60)
sec=0;
}
else if(dec==0)
{
while(dec==0);
sec--;
if(sec==0xff)
sec=59;
}
}
}
Delay(65000);Delay(65000);
dd=dec_hex(date);
mn=dec_hex(month);
yy=dec_hex(year);
hh=dec_hex(hour);
mm=dec_hex(min);
ss=dec_hex(sec);
DS1307_settime(hh,mm,ss);
DS1307_setdate(dd,mn,yy);
Delay(65000);
}
void Seg8_decimal2(min)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=min%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
e4=0;
P3=Lcd_t+0x30;
e4=1;
e3=0;
P3=Lcd_o+0x30;
e3=1;
}
void Led8decimal2(hour)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=hour%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
e5=0;
P3=Lcd_t+0x30;
e5=1;
e6=0;
P3=Lcd_o+0x30;
e6=1;
}
void Delay(unsigned int del)
{
while(del--);
}
void del()
{
Delay(65000);
Delay(65000);
}
void alarm_set()
{
cur=7;
event=0x01;
e12=0; //ALARM1
P0=event;
e12=1;
while(ent)
{
if(mov==0)
{
while(!mov);
cur++;
if(cur==13)
cur=7;
}
else if(count[cur]==0x82)
{
if(inc==0)
{
while(!inc);
amin++;
Send2lcd(amin);
display_alarmd1(amin);
}
else if(dec==0)
{
while(!dec);
amin--;
}
}
else if(count[cur]==0x81)
{
if(inc==0)
{
while(!inc);
ahour++;
Send2lcd(ahour);
display_alarmd2(ahour);
}
else if(dec==0)
{
while(!dec);
ahour--;
}
}
else if(count[cur]==0x83)
{
event=0x02;
e12=0;
P0=event; //ALARM2
e12=1;
Send2lcd(amin1);
display_alarmd1(amin1);
Send2lcd(ahour1);
display_alarmd2(ahour1);
if(inc==0)
{
while(!inc);
amin1++;
Send2lcd(amin1);
display_alarmd1(amin1);
}
else if(dec==0)
{
while(!dec);
amin1--;
}
}
else if(count[cur]==0x84)
{
if(inc==0)
{
while(!inc);
ahour1++;
Send2lcd(ahour1);
display_alarmd2(ahour1);
}
else if(dec==0)
{
while(!dec);
ahour1--;
}
}
else if(count[cur]==0x85)
{
event=0x03;
e12=0;
P0=event; //ALARM3
e12=1;
Send2lcd(amin2);
display_alarmd1(amin2);
Send2lcd(ahour2);
display_alarmd2(ahour2);
if(inc==0)
{
while(!inc);
amin2++;
Send2lcd(amin2);
display_alarmd1(amin2);
}
else if(dec==0)
{
while(!dec);
amin2--;
}
}
else if(count[cur]==0x86)
{
if(inc==0)
{
while(!inc);
ahour2++;
Send2lcd(ahour2);
display_alarmd2(ahour2);
}
else if(dec==0)
{
while(!dec);
ahour2--;
}
}
}
}
Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;
buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;
return buf;
}
void display_alarmd1(unsigned char value)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=value%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
e9=0;
P0=Lcd_t+0x30;
e9=1;
e8=0;
P0=Lcd_o+0x30;
e8=1;
}
void display_alarmd2(unsigned char value)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=value%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
e10=0;
P0=Lcd_t+0x30;
e10=1;
e11=0;
P0=Lcd_o+0x30;
e11=1;
}
void msdelay(unsigned int value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
alarm_chk()
{
asec=0;
if(min==amin)
c=1;
else
c=0;
if(hour==ahour)
d=1;
else
d=0;
if(sec==asec)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk1()
{
asec1=0;
if(min==amin1)
c=1;
else
c=0;
if(hour==ahour1)
d=1;
else
d=0;
if(sec==asec1)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk2()
{
asec2=0;
if(min==amin2)
c=1;
else
c=0;
if(hour==ahour2)
d=1;
else
d=0;
if(sec==asec2)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk3()
{
asec3=0;
if(min==amin3)
c=1;
else
c=0;
if(hour==ahour3)
d=1;
else
d=0;
if(sec==asec3)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk4()
{
asec=0;
if(min==amin4)
c=1;
else
c=0;
if(hour==ahour4)
d=1;
else
d=0;
if(sec==asec4)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk5()
{
asec5=0;
if(min==amin5)
c=1;
else
c=0;
if(hour==ahour5)
d=1;
else
d=0;
if(sec==asec5)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk6()
{
asec6=0;
if(min==amin6)
c=1;
else
c=0;
if(hour==ahour6)
d=1;
else
d=0;
if(sec==asec6)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
alarm_chk7()
{
asec=0;
if(min==amin7)
c=1;
else
c=0;
if(hour==ahour7)
d=1;
else
d=0;
if(sec==asec7)
e=1;
else
e=0;
if(c==1)
if(d==1)
if(e==1)
alarm_bel();
else
led=1;
return 0;
}
void alarm_bel()
{
led=0;
msdelay(150);
led=1;
}
/*********************************LCD HEADER FILE END*************************************/
/*********************************RTC HEADER FILE START*********************************/
DS1307_get(unsigned char addr)
{
unsigned char ret;
Rtc_Start();
ret = Rtc_Read(addr);
Rtc_Stop();
return ret;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start();
Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
Rtc_Stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
Rtc_Start();
Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Stop(); /* Stop i2c bus */
}
Send2lcd_alarm(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;
buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;
return buf;
}
void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}
void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}
Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
void Rtc_Start()// must for any operation on EEPROM
{
sda_rtc=1;
scl_rtc=1;
sda_rtc=0;
scl_rtc=0;
}
void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
sda_rtc=0;
scl_rtc=1;
sda_rtc=1;
}
void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
sda_rtc=CY;
scl_rtc=1;// clock is essential inorder to write or read
scl_rtc=0;// clk should be alternated
}
sda_rtc=1;
scl_rtc=1;
CY=sda_rtc;
scl_rtc=0;
}
void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
sda_rtc=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
scl_rtc=1;
in_rtc=in_rtc<<1;
in_rtc|=sda_rtc;
scl_rtc=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}
void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
scl_rtc=0;
}
void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?