Try adjusting LCD contrast.
]]>It won’t be the problem with code. I have experienced similar issue. Refer the datasheet and follow the recommended practices to increase the accuracy. I recommend to use some other RTC chips like DS3231 for better accuracy.
]]>unsigned short ReadI2C(unsigned short address)
{
unsigned short temp;
I2C1_Start();
I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0
I2C1_Wr(address);
I2C1_Repeated_Start();
I2C1_Wr(0xD1); //0x68 followed by 1 –> 0xD1
temp=I2C1_Rd(0);
I2C1_Stop();
return(temp);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C1_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}
int Binary2BCD(int a)
{
int t1, t2;
t1 = a%10;
t1 = t1 & 0x0F;
a = a/10;
t2 = a%10;
t2 = 0x0F & t2;
t2 = t2 <> 4;
t = 0x0F & t;
r = t*10 + r;
return r;
}
unsigned short set_count = 0;
short set;
int minute;
int hour;
int a=1,b=1;
char min,hr,t,l=10;
void main()
{
I2C1_Init(100000);
OSCCON.IRCF0=1;
OSCCON.IRCF1=1;
OSCCON.IRCF2=1;
ANSEL = 0;
ANSELH = 0;
TRISB=0x00;
TRISC.F0=0;
TRISC.F3=1;
TRISC.F4=1;
TRISC.F5=0;
TRISA.F0=1;
TRISA.F1=1;
TRISA.F2=0;
TRISE.F3=1;
TRISA.F5=0;
while(1)
{
// ReadI2C(7);
min=ReadI2C(1);
t= ReadI2C(2);
hr = t & 0b00011111;
min=((min & 0xf0)>>4)* 10 +(min & 0x0f);
hr=((hr & 0xf0)>>4)* 10 +(hr& 0x0f);
if(a==1)
{
PORTB = hex(min%10);
PORTC.F5= 1;
Delay_ms(5);
PORTC.F5=0;
PORTB = hex((min/10)%10);
PORTC.F0 = 1;
Delay_ms(5);
PORTC.F0 = 0;
}
if(b==1)
{
PORTB = hex(hr%10);
PORTA.F5 = 1;
Delay_ms(5);
PORTA.F5 = 0;
PORTB = hex((hr/10)%10);
PORTA.F2 = 1;
Delay_ms(5);
PORTA.F2 = 0;
}
set = 0;
if(PORTE.F3== 0)
{
Delay_ms(100);
if(PORTE.F3 == 0)
{
set_count++;
if( set_count==1)
{
a=0;
b=1;
}
if( set_count==2)
{
a=1;
b=0;
}
if(set_count >= 3)
{
set_count = 0;
a=1;
b=1;
write_ds1307(0,0×00);
write_ds1307(7,0×10);
}
}
}
if(set_count)
{
if(PORTA.F0 == 0)
{
Delay_ms(100);
if(PORTA.F0 == 0)
set = 1;
}
if(PORTA.F1 == 0)
{
Delay_ms(100);
if(PORTA.F1 == 0)
set = -1;
}
if(set_count && set)
{
switch(set_count)
{
case 1:
hour = BCD2Binary(hour);
hour = hour + set;
Delay_ms(250);
hour = Binary2BCD(hour);
if((hour & 0x1F) >= 0x13)
{
hour = hour & 0b11100001;
hour = hour ^ 0x20;
}
else if((hour & 0x1F) = 60)
minute = 0;
if(minute < 0)
minute = 59;
minute = Binary2BCD(minute);
write_ds1307(1, minute); //write min
break;
}
}
}
}
}
int Binary2BCD(int a) //Rehman! Here ‘a’ is the given binary number
{
int t1, t2;
t1 = a%10; //Rehman! This will give the 2nd digit ie right dgit
t1 = t1 & 0x0F;
a = a/10; //This will give the First digit i.e left digit……………………
t2 = a%10;
t2 = 0x0F & t2;
t2 = t2 << 4;
t2 = 0xF0 & t2;
t1 = t1 | t2;
return t1;
}
You can compare the time and do it easily. We have a premium and professionally developed school bell project. If you need just drop a mail to [email protected]
]]>