// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
int a=0;
unsigned short read_ds1307(unsigned short address)
unsigned short read_data;
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (data address)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0xD1);
read_data=I2C1_rd(0);
I2C_stop();
return(read_data);
}
void write_ds1307(unsigned short address,unsigned short write_data)
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of EEPROM location)
I2C1_Wr(write_data); // send data (data to be written)
I2C1_Stop();
unsigned char MSB(unsigned char x)
return ((x>>4) + '0');
unsigned char LSB(unsigned char x)
return ((x & 0x0F) + '0');
int BinarytoBCD(int x)
int t1,t2;
t1=x%10;
t1=t1 & 0x0F;
x=x/10;
t2=a%10 ;
t2=0x0F & t2;
t2=t2 << 4;
t2=0xF0 & t2;
t1=t1 | t2;
return t1;
int BCDtoBinary(int x)
int r,t;
t=x & 0x0F;
r=t;
x=0xF0 & x;
t=a >> 4;
t=0x0F & t;
r=t*10 +r;
return r;
int second;
int minute;
int hour;
int hr;
int day;
int dday;
int month;
int year;
char time[] = "[Link]";
char date[] = "00-00-00";
unsigned short set_count=0;
short set;
void main()
TRISA=0xff;
ADCON1=0b0110;
TRISD=0x00;
PORTD=0x00;
// OSCCON = 0x66;
I2C1_Init(100000);
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Digital...");
delay_ms(2000);
Lcd_Out(2,8,"Time....");
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Hour:");
Lcd_Out(2,1,"Date:"); // Write text in first row
for(;;)
second = read_ds1307(0);
minute = read_ds1307(1);
hour = read_ds1307(2);
hr = hour & 0b000011111;
dday = read_ds1307(3);
day = read_ds1307(4);
month = read_ds1307(5);
year = read_ds1307(6);
time[0] = MSB(hr);
time[1] = LSB(hr);
time[3] = MSB(minute);
time[4] = LSB(minute);
time[6] = MSB(second);
time[7] = LSB(second);
date[0] = MSB(day);
date[1] = LSB(day);
date[3] = MSB(month);
date[4] = LSB(month);
date[6] = MSB(year);
date[7] = LSB(year);
Lcd_Out(1,6,time);
Lcd_Out(2,6,date);
delay_ms(100);
PORTD=0x01;
if(hour==0b00010101 && minute==0b01011000)
PORTD=0x04;
delay_ms(5000);
PORTD=0x00;
}
set=0;
if(PORTA.RA0==1)
delay_ms(10);
set_count++;
PORTD=0X01;
delay_ms(1000);
PORTD=0x00;
if(set_count>=7)
set_count=0;
if(set_count)
if(PORTA.RA1==1)
delay_ms(100);
set=1;
PORTD=0x01;
if(PORTA.RA2==1)
delay_ms(100);
set=-1;
if(set_count && set)
{
switch(set_count)
/* case 1:
hour=BCDtoBinary(hour);
hour= hour + set;
hour=BinarytoBCD(hour);
PORTD=0x80;
if((hour & 0x1F) >= 0x13)
hour=hour & 0b11100001;
hour=hour^0x20;
else if((hour & 0x1F) <= 0x00)
hour=hour| 0b00010010;
hour=hour^0x20;
//Lcd_Out(1,6,hour);
write_ds1307(2,hour);
break; */
case 2:
//minute=BCDtoBinary(hour);
minute= minute + set;
//minute=BinarytoBCD(hour);
if(minute > 59)
{
minute=0;
Lcd_Out(1,9,minute);
break;
case 3:
Lcd_Out(1,12,0x00);
break;
case 4:
//day=BCDtoBinary(hour);
day= day + set;
//day=BinarytoBCD(hour);
if(day > 0x31)
day=1;
if(day <= 0)
day=0x31;
Lcd_Out(2,6,day);
break;
case 5:
//day=BCDtoBinary(hour);
month= month + set;
//day=BinarytoBCD(hour);
if(month > 0x12)
{
month=1;
if(day <= 0)
day=0x12;
Lcd_Out(2,9,month);
break;
case 6:
//day=BCDtoBinary(hour);
year= year + set;
//day=BinarytoBCD(hour);
if(year > 0x45)
year=0;
if(day <= 0)
year=0x99;
Lcd_Out(2,12,year);
break;
}
}