Dear friend,
Here i post a code for digital clok using pic16f877a,ds1307 and 2*16 lcd.Here i use MikroC.
char sec,minu,hour,day,month,year,date;
char m1,s1,h1,d1,dt,y1,mo1;
void write()
{
// I2C_Init(100000); // initialize full master mode
Lcd8_Out(1,1,"write");
I2C_Start(); // issue start signal
I2C_Wr(0xd0); // address DS1307
I2C_Wr(0); // start from word at address (REG0)
I2C_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C_Wr(0); // write 0 to minutes word to (REG1)
I2C_Wr(0x12); // write 12 to hours word (24-hours mode)(REG2)
I2C_Wr(0x05); // write 5 - Thursday (REG3)
I2C_Wr(0x25); // write 25 to date word (REG4)
I2C_Wr(0x09); // write 9 (September) to month word (REG5)
I2C_Wr(0x08); // write 08 to year word (REG6)
I2C_Stop(); // issue stop signal
I2C_Start(); // issue start signal
I2C_Wr(0xD0); // address DS1307
I2C_Wr(0); // start from word at address 0
I2C_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C_Stop();
}
void read()
{
Lcd8_Out(1,1,"read");
I2C_Start();
I2C_wr(0xD0);
I2C_wr(0);
I2C_Repeated_Start();
I2C_wr(0xD1);
sec=I2C_Rd(1);
minu=I2C_Rd(1);
hour=I2C_Rd(1);
day=I2C_Rd(1);
date=I2C_Rd(1);
month=I2C_Rd(1);
year=I2C_Rd(0);
I2C_Stop();
}
void display()
{
LCD_Out(1,1,"display");
s1=Bcd2Dec(sec);
Lcd8_Chr(2,7,(s1/10 +48));
Lcd8_Chr(2,8,(s1%10 +48));
Lcd8_Out(2,6,":");
m1=Bcd2Dec(minu);
Lcd8_Chr(2,4,(m1/10 +48));
Lcd8_Chr(2,5,(m1%10 +48));
Lcd8_Out(2,3,":");
h1=Bcd2Dec(hour);
Lcd8_Chr(2,1,(h1/10 +48));
Lcd8_Chr(2,2,(h1%10 +48));
d1=Bcd2Dec(day);
Lcd8_Chr(1,5,(d1/10 +48));
Lcd8_Chr(1,6,(d1%10 +48));
Lcd8_Out(1,11,"/");
dt=Bcd2Dec(date);
Lcd8_Chr(1,9,(dt/10 +48));
Lcd8_Chr(1,10,(dt%10 +48));
mo1=Bcd2Dec(month);
Lcd8_Chr(1,12,(mo1/10 +48));
Lcd8_Chr(1,13,(mo1%10 +48));
Lcd8_Out(1,14,"/");
y1=Bcd2Dec(year);
Lcd8_Chr(1,15,(y1/10 +48));
Lcd8_Chr(1,16,(y1%10 +48));
}
void main()
{
trisc.f4=0;
trisc.f3=0;
Lcd8_Config(&portc,&portb,0,2,1,7,6,5,4,3,2,1,0);
Lcd8_Out(1,1,"start");
Delay_ms(1000);
I2C_Init(100000);
write();
while(1)
{
read();
display();
}
}