#include<reg51.h>
sbit set=P1^0;
sbit mov=P1^1;
sbit inc=P1^2;
sbit dec=P1^3;
sbit ent=P1^4;
sbit alarmset=P3^7;
void all_disp();
void time_set();
void alarm_set1();
void alarm_set2();
void alarm_set3();
void alarm_set4();
void alarm_set5();
void alarm_set6();
void alarm_set7();
void alarm_set8();
void alarm_chk1();
void alarm_chk2();
void alarm_bel_HI();
void alarm_bel_LOW();
sbit e5=P1^5;//3rd e5
sbit e6=P2^3;//4th e6
sbit e4=P1^7;//1st e4
sbit e3=P1^6;//2nd e3
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
sbit Lcd_rs = P2^0;
sbit Lcd_rw = P2^1;
sbit Lcd_en = P2^2;
sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;
/*sbit Lcd_rs = P3^5;
sbit Lcd_rw = P3^6;
sbit Lcd_en = P3^7; */
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Delay(unsigned int);
void del();
sbit SDA=P2^6;
sbit SCL=P2^7;
bit f;
void Seg8_decimal2(min);
void Led8decimal2(hour);
void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data);
unsigned char EEPROM_ReadByte(unsigned char eeprom_Address);
void I2C_Start();
void I2C_Stop(void);
void I2C_Ack();
void I2C_Write(unsigned char dat);
unsigned char I2C_Read();
#define EEPROM_ID 0xa0
void delay_ms(unsigned int ms_count);
void I2C_NoAck();
void I2C_Clock();
void delay_us(unsigned int us_count);
void msdelay(unsigned int value);
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DAY 0x03
#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 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,day,month,year,sec1,hour1,min1,sec2,hour2,min2,hh,mm,ss,dd,mn,yy,ahour,amin,asec,amin1,ahour1,ahour2,amin2,ahour3,amin3;
unsigned char amin4,ahour4,ahour5,amin5,amin6,ahour6,ahour7,amin7,ahour8,amin8,ahour9,amin9,amin10,ahour10,ahour11,amin11,ahour12,amin12,ahour13,amin13,ahour14,amin14,ahour15,amin15,ahour16,amin16,ahour17,amin17,ahour18,amin18,ahour19,amin19,ahour20,amin20,ahour21,amin21,ahour22,amin22,ahour23,amin23,ahour24,amin24;
unsigned char count[8]={0x82,0x85,0x8a,0x8d,0xc2,0xc5,0xca,0xcd};
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);
date = DS1307_get(DATE);
day = DS1307_get(DAY);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
min=Send2lcd(min);
hour=Send2lcd(hour);
day=Send2lcd(day);
Seg8_decimal2(min);
Led8decimal2(hour);
if(day==0x04)
Lcd8_Display(0x84,"TUESDAY: ",8);
}
void main()
{
Lcd8_Init();
ahour=EEPROM_ReadByte(0x01);
amin= EEPROM_ReadByte(0x02);
asec=0;
ahour1=EEPROM_ReadByte(0x03);
amin1= EEPROM_ReadByte(0x04);
Lcd8_Display(0x80,"RTC TESTING: ",12);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0xc0,"alarm can set:",14);
Rtc_Init();
//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);
while(1)
{
all_disp();
if(day==4)
alarm_chk1();
else
alarm_chk2();
if(!set)
time_set();
if(alarmset==1)
{
alarm_set1();
alarm_set2();
alarm_set3();
alarm_set4();
alarm_set5();
alarm_set6();
alarm_set7();
alarm_set8();
}
}
}
void time_set()
{
all_disp();
cur=1;
while(ent)
{
if(mov==0)
{
while(!mov);
cur++;
if(cur==2)
cur=0;
}
else if(count[cur]==0x82)
{
if(inc==0)
{
while(inc==0);
hour++;
Led8decimal2(hour);
if(hour>=24)
hour=0;
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
min++;
Seg8_decimal2(min);
if(min>=60)
min=0;
}
}
}
hh=dec_hex(hour);
mm=dec_hex(min);
DS1307_settime(hh,mm,ss);
DS1307_setdate(dd,mn,yy);
}
void alarm_set1()
{
Lcd8_Command(0x01);
Lcd8_Display(0x84,"NOW set",7);
Lcd8_Display(0xc0,"Regular schedule",17);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0xc0,"Long alarm times",16);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0x80,"1:",2);
Lcd8_Decimal2(0x82,ahour);
Lcd8_Decimal2(0x85,amin);
Lcd8_Display(0x88,"2:",2);
Lcd8_Decimal2(0x8a,ahour1);
Lcd8_Decimal2(0x8d,amin1);
Lcd8_Display(0xc0,"3:",2);
Lcd8_Decimal2(0xc2,ahour2);
Lcd8_Decimal2(0xc5,amin2);
Lcd8_Display(0xc8,"4:",2);
Lcd8_Decimal2(0xca,ahour3);
Lcd8_Decimal2(0xcd,amin3);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour++;
if(ahour>=24)
ahour=0;
Lcd8_Decimal2(count[cur],ahour);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin++;
if(amin>=60)
amin=0;
Lcd8_Decimal2(count[cur],amin);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour1++;
if(ahour1>=60)
ahour1=0;
Lcd8_Decimal2(count[cur],ahour1);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin1++;
if(amin1>=60)
amin1=0;
Lcd8_Decimal2(count[cur],amin1);
}
}
else if(count[cur]==0xc2)
{
if(inc==0)
{
while(inc==0);
ahour2++;
if(ahour2>=60)
ahour2=0;
Lcd8_Decimal2(count[cur],ahour2);
}
}
else if(count[cur]==0xc5)
{
if(inc==0)
{
while(inc==0);
amin2++;
if(amin2>=60)
amin2=0;
Lcd8_Decimal2(count[cur],amin2);
}
}
else if(count[cur]==0xca)
{
if(inc==0)
{
while(inc==0);
ahour3++;
if(ahour3>=60)
ahour3=0;
Lcd8_Decimal2(count[cur],ahour3);
}
}
else if(count[cur]==0xcd)
{
if(inc==0)
{
while(inc==0);
amin3++;
if(amin3>=60)
amin3=0;
Lcd8_Decimal2(count[cur],amin3);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x01, ahour);
EEPROM_WriteByte(0x02, amin);
EEPROM_WriteByte(0x03, ahour1);
EEPROM_WriteByte(0x04, amin1);
EEPROM_WriteByte(0x05, ahour2);
EEPROM_WriteByte(0x06, amin2);
EEPROM_WriteByte(0x07, ahour3);
EEPROM_WriteByte(0x08, amin3);
Lcd8_Command(0x01);
}
void alarm_set2()
{
Lcd8_Command(0x01);
Lcd8_Display(0x80,"5:",2);
Lcd8_Decimal2(0x82,ahour4);
Lcd8_Decimal2(0x85,amin4);
Lcd8_Display(0x88,"6:",2);
Lcd8_Decimal2(0x8a,ahour5);
Lcd8_Decimal2(0x8d,amin5);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour4++;
if(ahour4>=24)
ahour4=0;
Lcd8_Decimal2(count[cur],ahour4);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin4++;
if(amin4>=60)
amin4=0;
Lcd8_Decimal2(count[cur],amin4);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour5++;
if(ahour5>=60)
ahour5=0;
Lcd8_Decimal2(count[cur],ahour5);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin5++;
if(amin5>=60)
amin5=0;
Lcd8_Decimal2(count[cur],amin5);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x09, ahour4);
EEPROM_WriteByte(0x10, amin4);
EEPROM_WriteByte(0x11, ahour5);
EEPROM_WriteByte(0x12, amin5);
Lcd8_Command(0x01);
}
void alarm_set3()
{
Lcd8_Command(0x01);
Lcd8_Display(0xc0,"short alarm times",17);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0x80,"1:",2);
Lcd8_Decimal2(0x82,ahour6);
Lcd8_Decimal2(0x85,amin6);
Lcd8_Display(0x88,"2:",2);
Lcd8_Decimal2(0x8a,ahour7);
Lcd8_Decimal2(0x8d,amin7);
Lcd8_Display(0xc0,"3:",2);
Lcd8_Decimal2(0xc2,ahour8);
Lcd8_Decimal2(0xc5,amin8);
Lcd8_Display(0xc8,"4:",2);
Lcd8_Decimal2(0xca,ahour9);
Lcd8_Decimal2(0xcd,amin9);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour6++;
if(ahour6>=24)
ahour6=0;
Lcd8_Decimal2(count[cur],ahour6);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin6++;
if(amin6>=60)
amin6=0;
Lcd8_Decimal2(count[cur],amin6);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour7++;
if(ahour7>=60)
ahour7=0;
Lcd8_Decimal2(count[cur],ahour7);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin7++;
if(amin7>=60)
amin7=0;
Lcd8_Decimal2(count[cur],amin7);
}
}
else if(count[cur]==0xc2)
{
if(inc==0)
{
while(inc==0);
ahour8++;
if(ahour8>=60)
ahour8=0;
Lcd8_Decimal2(count[cur],ahour8);
}
}
else if(count[cur]==0xc5)
{
if(inc==0)
{
while(inc==0);
amin8++;
if(amin8>=60)
amin8=0;
Lcd8_Decimal2(count[cur],amin8);
}
}
else if(count[cur]==0xca)
{
if(inc==0)
{
while(inc==0);
ahour9++;
if(ahour9>=60)
ahour9=0;
Lcd8_Decimal2(count[cur],ahour9);
}
}
else if(count[cur]==0xcd)
{
if(inc==0)
{
while(inc==0);
amin9++;
if(amin9>=60)
amin9=0;
Lcd8_Decimal2(count[cur],amin9);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x13, ahour6);
EEPROM_WriteByte(0x14, amin6);
EEPROM_WriteByte(0x15, ahour7);
EEPROM_WriteByte(0x16, amin7);
EEPROM_WriteByte(0x17, ahour8);
EEPROM_WriteByte(0x18, amin8);
EEPROM_WriteByte(0x19, ahour9);
EEPROM_WriteByte(0x20, amin9);
Lcd8_Command(0x01);
}
void alarm_set4()
{
{
Lcd8_Command(0x01);
Lcd8_Display(0x80,"5:",2);
Lcd8_Decimal2(0x82,ahour10);
Lcd8_Decimal2(0x85,amin10);
Lcd8_Display(0x88,"6:",2);
Lcd8_Decimal2(0x8a,ahour11);
Lcd8_Decimal2(0x8d,amin11);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour10++;
if(ahour10>=24)
ahour10=0;
Lcd8_Decimal2(count[cur],ahour10);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin10++;
if(amin11>=60)
amin10=0;
Lcd8_Decimal2(count[cur],amin10);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour11++;
if(ahour11>=60)
ahour11=0;
Lcd8_Decimal2(count[cur],ahour11);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin11++;
if(amin11>=60)
amin11=0;
Lcd8_Decimal2(count[cur],amin11);
}
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x21, ahour10);
EEPROM_WriteByte(0x22, amin10);
EEPROM_WriteByte(0x23, ahour11);
EEPROM_WriteByte(0x24, amin11);
Lcd8_Command(0x01);
}
void alarm_set5()
{
Lcd8_Command(0x01);
Lcd8_Display(0x84,"NOW set",7);
Lcd8_Display(0xc0,"FRIDAY schedule",17);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0xc0,"Long alarm times",16);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0x80,"1:",2);
Lcd8_Decimal2(0x82,ahour12);
Lcd8_Decimal2(0x85,amin12);
Lcd8_Display(0x88,"2:",2);
Lcd8_Decimal2(0x8a,ahour13);
Lcd8_Decimal2(0x8d,amin13);
Lcd8_Display(0xc0,"3:",2);
Lcd8_Decimal2(0xc2,ahour14);
Lcd8_Decimal2(0xc5,amin14);
Lcd8_Display(0xc8,"4:",2);
Lcd8_Decimal2(0xca,ahour15);
Lcd8_Decimal2(0xcd,amin15);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour12++;
if(ahour12>=24)
ahour12=0;
Lcd8_Decimal2(count[cur],ahour12);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin12++;
if(amin12>=60)
amin12=0;
Lcd8_Decimal2(count[cur],amin12);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour13++;
if(ahour13>=60)
ahour13=0;
Lcd8_Decimal2(count[cur],ahour13);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin13++;
if(amin13>=60)
amin13=0;
Lcd8_Decimal2(count[cur],amin13);
}
}
else if(count[cur]==0xc2)
{
if(inc==0)
{
while(inc==0);
ahour14++;
if(ahour14>=60)
ahour14=0;
Lcd8_Decimal2(count[cur],ahour14);
}
}
else if(count[cur]==0xc5)
{
if(inc==0)
{
while(inc==0);
amin14++;
if(amin14>=60)
amin14=0;
Lcd8_Decimal2(count[cur],amin14);
}
}
else if(count[cur]==0xca)
{
if(inc==0)
{
while(inc==0);
ahour15++;
if(ahour15>=60)
ahour15=0;
Lcd8_Decimal2(count[cur],ahour15);
}
}
else if(count[cur]==0xcd)
{
if(inc==0)
{
while(inc==0);
amin15++;
if(amin15>=60)
amin15=0;
Lcd8_Decimal2(count[cur],amin15);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x25, ahour12);
EEPROM_WriteByte(0x26, amin12);
EEPROM_WriteByte(0x27, ahour13);
EEPROM_WriteByte(0x28, amin13);
EEPROM_WriteByte(0x29, ahour14);
EEPROM_WriteByte(0x30, amin14);
EEPROM_WriteByte(0x31, ahour15);
EEPROM_WriteByte(0x32, amin15);
Lcd8_Command(0x01);
}
void alarm_set6()
{
Lcd8_Command(0x01);
Lcd8_Display(0x80,"5:",2);
Lcd8_Decimal2(0x82,ahour16);
Lcd8_Decimal2(0x85,amin16);
Lcd8_Display(0x88,"6:",2);
Lcd8_Decimal2(0x8a,ahour17);
Lcd8_Decimal2(0x8d,amin17);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour16++;
if(ahour16>=24)
ahour16=0;
Lcd8_Decimal2(count[cur],ahour16);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin16++;
if(amin16>=60)
amin16=0;
Lcd8_Decimal2(count[cur],amin16);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour17++;
if(ahour17>=60)
ahour17=0;
Lcd8_Decimal2(count[cur],ahour17);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin17++;
if(amin17>=60)
amin17=0;
Lcd8_Decimal2(count[cur],amin17);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x33, ahour16);
EEPROM_WriteByte(0x34, amin16);
EEPROM_WriteByte(0x35, ahour17);
EEPROM_WriteByte(0x36, amin17);
Lcd8_Command(0x01);
}
void alarm_set7()
{
Lcd8_Command(0x01);
Lcd8_Display(0xc0,"short alarm times",17);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(0x80,"1:",2);
Lcd8_Decimal2(0x82,ahour18);
Lcd8_Decimal2(0x85,amin18);
Lcd8_Display(0x88,"2:",2);
Lcd8_Decimal2(0x8a,ahour19);
Lcd8_Decimal2(0x8d,amin19);
Lcd8_Display(0xc0,"3:",2);
Lcd8_Decimal2(0xc2,ahour20);
Lcd8_Decimal2(0xc5,amin20);
Lcd8_Display(0xc8,"4:",2);
Lcd8_Decimal2(0xca,ahour21);
Lcd8_Decimal2(0xcd,amin21);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour18++;
if(ahour18>=24)
ahour18=0;
Lcd8_Decimal2(count[cur],ahour18);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin18++;
if(amin18>=60)
amin18=0;
Lcd8_Decimal2(count[cur],amin18);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour19++;
if(ahour19>=60)
ahour19=0;
Lcd8_Decimal2(count[cur],ahour19);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin19++;
if(amin19>=60)
amin19=0;
Lcd8_Decimal2(count[cur],amin19);
}
}
else if(count[cur]==0xc2)
{
if(inc==0)
{
while(inc==0);
ahour20++;
if(ahour20>=60)
ahour20=0;
Lcd8_Decimal2(count[cur],ahour20);
}
}
else if(count[cur]==0xc5)
{
if(inc==0)
{
while(inc==0);
amin20++;
if(amin20>=60)
amin20=0;
Lcd8_Decimal2(count[cur],amin20);
}
}
else if(count[cur]==0xca)
{
if(inc==0)
{
while(inc==0);
ahour21++;
if(ahour21>=60)
ahour21=0;
Lcd8_Decimal2(count[cur],ahour21);
}
}
else if(count[cur]==0xcd)
{
if(inc==0)
{
while(inc==0);
amin21++;
if(amin21>=60)
amin21=0;
Lcd8_Decimal2(count[cur],amin21);
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x37, ahour18);
EEPROM_WriteByte(0x38, amin18);
EEPROM_WriteByte(0x39, ahour19);
EEPROM_WriteByte(0x40, amin19);
EEPROM_WriteByte(0x41, ahour20);
EEPROM_WriteByte(0x42, amin20);
EEPROM_WriteByte(0x43, ahour21);
EEPROM_WriteByte(0x44, amin21);
Lcd8_Command(0x01);
}
void alarm_set8()
{
{
Lcd8_Command(0x01);
Lcd8_Display(0x80,"5:",2);
Lcd8_Decimal2(0x82,ahour22);
Lcd8_Decimal2(0x85,amin22);
Lcd8_Display(0x88,"6:",2);
Lcd8_Decimal2(0x8a,ahour23);
Lcd8_Decimal2(0x8d,amin23);
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==8)
cur=0;
}
else if(count[cur]==0x82) ///1st line of lcd
{
if(inc==0)
{
while(inc==0);
ahour22++;
if(ahour22>=24)
ahour22=0;
Lcd8_Decimal2(count[cur],ahour22);
}
}
else if(count[cur]==0x85)
{
if(inc==0)
{
while(inc==0);
amin22++;
if(amin22>=60)
amin22=0;
Lcd8_Decimal2(count[cur],amin22);
}
}
else if(count[cur]==0x8a)
{
if(inc==0)
{
while(inc==0);
ahour23++;
if(ahour23>=60)
ahour23=0;
Lcd8_Decimal2(count[cur],ahour23);
}
}
else if(count[cur]==0x8d)
{
if(inc==0)
{
while(inc==0);
amin23++;
if(amin23>=60)
amin23=0;
Lcd8_Decimal2(count[cur],amin23);
}
}
}
}
Lcd8_Command(0x0c);
Delay(65000);Delay(65000);
EEPROM_WriteByte(0x45, ahour22);
EEPROM_WriteByte(0x46, amin22);
EEPROM_WriteByte(0x47, ahour23);
EEPROM_WriteByte(0x48, amin23);
Lcd8_Command(0x01);
}
void alarm_chk1()
{
if(min==amin)
if(sec==asec)
alarm_bel_HI();
else
dec=1;
}
void alarm_chk2()
{
if(min==amin)
if(sec==asec)
alarm_bel_LOW();
else
dec=1;
}
void alarm_bel_HI()
{
dec=0;
Delay(65000);Delay(65000);
Delay(65000);Delay(65000);
Delay(65000);Delay(65000);
dec=1;
}
void alarm_bel_LOW()
{
dec=0;
Delay(65000);Delay(65000);
dec=1;
}
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
void Delay(unsigned int del)
{
while(del--);
}
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;
e5=0;
P3=Lcd_t+0x30;
e5=1;
e6=0;
P3=Lcd_o+0x30;
e6=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;
e4=0;
P3=Lcd_t+0x30;
e4=1;
e3=0;
P3=Lcd_o+0x30;
e3=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(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;
}
void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data)
{
I2C_Start(); // Start i2c communication
I2C_Write(EEPROM_ID); // connect to AT2404 by sending its ID on I2c Bus
I2C_Ack();
I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
I2C_Ack();
I2C_Write(eeprom_Data); // Write the data at specified address
I2C_Ack();
I2C_Stop(); // Stop i2c communication after Writing the data
delay_ms(1); // Write operation takes max 5ms, refer At2404 datasheet
}
unsigned char EEPROM_ReadByte(unsigned char eeprom_Address)
{
unsigned char eeprom_Data;
I2C_Start(); // Start i2c communication
I2C_Write(EEPROM_ID); // connect to AT2404(write) by sending its ID on I2c Bus
I2C_Ack();
I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
I2C_Ack();
I2C_Start(); // Start i2c communication
I2C_Write(0xA1); // connect to AT2404(read) by sending its ID on I2c Bus
I2C_Ack();
eeprom_Data = I2C_Read(); // Read the data from specified address
I2C_NoAck();
I2C_Stop(); // Stop i2c communication after Reading the data
delay_us(10);
return eeprom_Data; // Return the Read data
}
void I2C_Start()
{
SCL = 0; // Pull SCL low
SDA = 1; // Pull SDA High
delay_us(1);
SCL = 1; //Pull SCL high
delay_us(1);
SDA = 0; //Now Pull SDA LOW, to generate the Start Condition
delay_us(1);
SCL = 0; //Finally Clear the SCL to complete the cycle
}
void I2C_Stop(void)
{
SCL = 0; // Pull SCL low
delay_us(1);
SDA = 0; // Pull SDA low
delay_us(1);
SCL = 1; // Pull SCL High
delay_us(1);
SDA = 1; // Now Pull SDA High, to generate the Stop Condition
}
void I2C_Ack()
{
SDA = 0; //Pull SDA low to indicate Positive ACK
I2C_Clock(); //Generate the Clock
SDA = 1; // Pull SDA back to High(IDLE state)
}
void I2C_NoAck()
{
SDA = 1; //Pull SDA high to indicate Negative/NO ACK
I2C_Clock(); // Generate the Clock
SCL = 1; // Set SCL */
}
void I2C_Write(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++) // loop 8 times to send 1-byte of data
{
SDA = dat & 0x80; // Send Bit by Bit on SDA line
I2C_Clock(); // Generate Clock at SCL
dat = dat<<1;
}
SDA = 1; // Set SDA at last
}
unsigned char I2C_Read(void)
{
unsigned char i, dat=0x00;
SDA=1; //Make SDA as I/P
for(i=0;i<8;i++) // loop 8times to read 1-byte of data
{
delay_us(1);
SCL = 1; // Pull SCL High
delay_us(1);
dat = dat<<1; //dat is Shifted each time and
dat = dat | SDA; //ORed with the received bit to pack into byte
SCL = 0; // Clear SCL to complete the Clock
}
return dat; // Finally return the received Byte*
}
void delay_ms(unsigned int ms_count)
{
while(ms_count!=0)
{
delay_us(112); //delay_us is called to generate 1ms delay
ms_count--;
}
}
void delay_us(unsigned int us_count)
{
while(us_count!=0)
{
us_count--;
}
}
void I2C_Clock(void)
{
delay_us(1);
SCL = 1; // Wait for Some time and Pull the SCL line High
delay_us(1); // Wait for Some time
SCL = 0; // Pull back the SCL line low to Generate a clock pulse
}
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?