Code: Select all
char sec,minute,hour,day,date,month,year;
char time_disp[12];
char date_disp[11];
char am_pm(char);
char digit2(char);
char digit1(char);
void main(){
trisc=0xff;
trisd=0;
portd=0;
portc=0;
Lcd_Config(&PORTB, 1,3,2,7,6,5,4); //initializing the LCD
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
Lcd_Out(1, 1, "TIME:"); //LCD output for label TIME:
Lcd_Out(2, 1, "DATE:"); //LCD output for label DATE:
delay_ms(500);
soft_i2c_config(&PORTc,4,3); //configuring DS1307 connection
soft_i2c_start(); //write settings on DS1307
soft_i2c_write(0xd0);
soft_i2c_write(0);
soft_i2c_write(0); //sec
soft_i2c_write(20); //min
soft_i2c_write(0x70); //hr
soft_i2c_write(0x6); //day
soft_i2c_write(0x25); //date
soft_i2c_write(0x07); //month
soft_i2c_write(0x15); //year
soft_i2c_stop();
while(1) {
soft_i2c_Start(); //setup for reading DS1307
soft_i2c_Write(0xd0);
soft_i2c_Write(0);
soft_i2c_Start();
soft_i2c_Write(0xd1);
sec =soft_i2c_read(1); // read second
minute=soft_i2c_read(1); // read minute
hour =soft_i2c_read(1); // read hour
day =soft_i2c_read(1); // read day
date =soft_i2c_read(1); // read date
month =soft_i2c_read(1); // read month
year =soft_i2c_read(0); // read year
soft_I2C_Stop();
//time_disp array content for LCD output column 6 to 16
time_disp[0] = (((hour>>4)&0x01)+'0'); //second digit of hour
time_disp[1] = digit1(hour); //first digit of hour
time_disp[2] = ':';
time_disp[3] = digit2(minute); //second digit of minute
time_disp[4] = digit1(minute); //first digit of minute
time_disp[5] = ':';
time_disp[6] = digit2(sec); //second digit of second
time_disp[7] = digit1(sec); //first digit of second
time_disp[8]=' ';
time_disp[9]= am_pm(hour); //display for am/pm
time_disp[10]='m';
time_disp[11] = '\0';
//date_disp array content for LCD output column 6 to 15
date_disp[0] = digit2(date); //second digit of date
date_disp[1] = digit1(date); //first digit of date
date_disp[2] ='/';
date_disp[3] = digit2(month); //second digit of month
date_disp[4] = digit1(month); //first digit of month
date_disp[5] ='/';
date_disp[6] = '2';
date_disp[7] = '0';
date_disp[8] = digit2(year); //second digit of tens digit (year)
date_disp[9] = digit1(year); //first digit of ones digit (year)
date_disp[10] = '\0';
lcd_Out(1,6,time_disp);
lcd_Out(2,6,date_disp);
delay_ms(100);
}
}
char digit2( char x){
return ((x >> 4) + '0'); //shift by four places and place value as string
}
char digit1( char y){
return ((y & 0x0F) + '0'); //AND with 0x0F to get the lower nibble
}
char am_pm(char hr){
if (hr.f5==0) return ('a'); //to determine the status - am/pm
else return ('p');
} //end