Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#ifndef DS1307
#define DS1307 0xD0
#endif
// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATD2_bit;
sbit LCD_D5 at LATD3_bit;
sbit LCD_D6 at LATD4_bit;
sbit LCD_D7 at LATD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections
double temp;
char sTemp[23];
char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
void Zero_Fill(char *value) {
if (value[1] == 0) {
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
void Write_Time() {
I2C1_Start(); // issue start signal
I2C1_Wr(DS1307); // address DS1307
I2C1_Wr(0); // start from word at address (REG0)
I2C1_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C1_Wr(0); // write 0 to minutes word to (REG1)
I2C1_Wr(0x17); // write 17 to hours word (24-hours mode)(REG2)
I2C1_Wr(0x02); // write 2 - Monday (REG3)
I2C1_Wr(0x04); // write 4 to date word (REG4)
I2C1_Wr(0x05); // write 5 (May) to month word (REG5)
I2C1_Wr(0x01); // write 01 to year word (REG6)
I2C1_Stop(); // issue stop signal
I2C1_Start(); // issue start signal
I2C1_Wr(0xD0); // address DS1307
I2C1_Wr(0); // start from word at address 0
I2C1_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C1_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
I2C1_Start();
I2C1_Wr(DS1307);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
*sec =I2C1_Rd(2);
*min =I2C1_Rd(2);
*hr =I2C1_Rd(2);
*week_day =I2C1_Rd(2);
*day =I2C1_Rd(2);
*mn =I2C1_Rd(2);
*year =I2C1_Rd(2);
I2C1_Stop();
}
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
Lcd_Out(1,1,"Date:");
switch(week_day){
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
Lcd_Out(1,7,txt);
Lcd_Chr(1,11,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,12, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,13,'/');
Lcd_Chr(1,14,(mn / 10) + 48);
Lcd_Chr(1,15,(mn % 10) + 48);
Lcd_Chr(1,16,'/');
Lcd_Out(1,17,"2");
Lcd_Out(1,18,"0");
Lcd_Chr(1,19, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,20, (year % 10) + 48);
if(hr > 12) {
Lcd_Out(2,10,"PM");
}
else if(hr <= 12) {
Lcd_Out(2,10,"AM");
}
Lcd_Chr(2,13,(hr / 10) + 48);
Lcd_Chr(2,14,(hr % 10) + 48);
Lcd_Out(2,15,":");
Lcd_Chr(2,16,(min / 10) + 48);
Lcd_Chr(2,17,(min % 10) + 48);
Lcd_Out(2,18,":");
Lcd_Chr(2,19,(sec / 10) + 48);
Lcd_Chr(2,20,(sec % 10) + 48);
}
void main() {
TRISA = 0b00000011;
PORTA = 0x00;
TRISB = 0b00000011;
PORTB = 0x00;
LATB = 0x00;
TRISD = 0b00000000;
PORTD = 0xFF;
LATD = 0x00;
ADCON0 = 0b00000000;
ADCON1 = 0b00001101;
CMCON = 0x07;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);
Lcd_Out(1,1,"RTC And Temperature");
Lcd_Out(2,1,"Data Logger");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
ADC_Init();
I2C1_Init(100000);
txt = "Time:";
Lcd_Out(2,1,txt);
Lcd_Out(3,1,"Temperature:");
while(1) {
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // read time from RTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
Display_Time(sec, min1, hr, week_day, day, mn, year);
temp = ADC_Read(0);
temp = temp * 0.39682550; //18056883083831503659128
FloatToStr(temp, sTemp);
Lcd_Out(3,14,sTemp);
}
}
#ifndef DS1307
#define DS1307 0xD0
#endif
// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATD2_bit;
sbit LCD_D5 at LATD3_bit;
sbit LCD_D6 at LATD4_bit;
sbit LCD_D7 at LATD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections
double temp;
char sTemp[23];
char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
void Zero_Fill(char *value) {
if (value[1] == 0) {
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
void Write_Time() {
I2C1_Start(); // issue start signal
I2C1_Wr(DS1307); // address DS1307
I2C1_Wr(0); // start from word at address (REG0)
I2C1_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C1_Wr(0); // write 0 to minutes word to (REG1)
I2C1_Wr(0x17); // write 17 to hours word (24-hours mode)(REG2)
I2C1_Wr(0x02); // write 2 - Monday (REG3)
I2C1_Wr(0x04); // write 4 to date word (REG4)
I2C1_Wr(0x05); // write 5 (May) to month word (REG5)
I2C1_Wr(0x01); // write 01 to year word (REG6)
I2C1_Stop(); // issue stop signal
I2C1_Start(); // issue start signal
I2C1_Wr(0xD0); // address DS1307
I2C1_Wr(0); // start from word at address 0
I2C1_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C1_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
I2C1_Start();
I2C1_Wr(DS1307);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
*sec =I2C1_Rd(1);
*min =I2C1_Rd(1);
*hr =I2C1_Rd(1);
*week_day =I2C1_Rd(1);
*day =I2C1_Rd(1);
*mn =I2C1_Rd(1);
*year =I2C1_Rd(0);
I2C1_Stop();
}
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
Lcd_Out(1,1,"Date:");
switch(week_day){
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
Lcd_Out(1,7,txt);
Lcd_Chr(1,11,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,12, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,13,'/');
Lcd_Chr(1,14,(mn / 10) + 48);
Lcd_Chr(1,15,(mn % 10) + 48);
Lcd_Chr(1,16,'/');
Lcd_Out(1,17,"2");
Lcd_Out(1,18,"0");
Lcd_Chr(1,19, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,20, (year % 10) + 48);
if(hr > 12) {
Lcd_Out(2,10,"PM");
}
else if(hr <= 12) {
Lcd_Out(2,10,"AM");
}
Lcd_Chr(2,13,(hr / 10) + 48);
Lcd_Chr(2,14,(hr % 10) + 48);
Lcd_Out(2,15,":");
Lcd_Chr(2,16,(min / 10) + 48);
Lcd_Chr(2,17,(min % 10) + 48);
Lcd_Out(2,18,":");
Lcd_Chr(2,19,(sec / 10) + 48);
Lcd_Chr(2,20,(sec % 10) + 48);
}
void main() {
TRISA = 0b00000011;
PORTA = 0x00;
//TRISB = 0b00000011;
//PORTB = 0x00;
//LATB = 0x00;
TRISD = 0b00000000;
PORTD = 0xFF;
LATD = 0x00;
ADCON0 = 0b00000000;
ADCON1 = 0b00001101;
CMCON = 0x07;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);
Lcd_Out(1,1,"RTC And Temperature");
Lcd_Out(2,1,"Data Logger");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
ADC_Init();
I2C1_Init(100000);
txt = "Time:";
Lcd_Out(2,1,txt);
Lcd_Out(3,1,"Temperature:");
while(1) {
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // read time from RTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
Display_Time(sec, min1, hr, week_day, day, mn, year);
Delay_ms(1000);
temp = ADC_Read(0);
temp = temp * 0.39682550; //18056883083831503659128
FloatToStr(temp, sTemp);
Lcd_Out(3,14,sTemp);
}
}