it is using buttons to set time. The code I have just displays the date and time. There is not code for buttons. can you write code for the buttons?
Here is the code. You have to change the TRISx and ADCONx , and other registers according to your PIC and project. Also you have to change the Lcd module connections code.
Code:#ifndef DS1307 #define DS1307 0xD0 #endif // LCD module connections sbit LCD_RS at LATB0_bit; sbit LCD_EN at LATB1_bit; sbit LCD_D4 at LATB2_bit; sbit LCD_D5 at LATB3_bit; sbit LCD_D6 at LATB6_bit; sbit LCD_D7 at LATB7_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 TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; // End LCD module connections unsigned char num,flag; char b; char x; int cnt; unsigned int ch_cnt = 0; char colon[] = ":"; unsigned char year1, month1, day1, hours1, mins2, seconds1; char i; unsigned char sec, min1, hr, week_day, day, mn, year; char *txt, tnum[4]; int error, am_pm = 0; char time_format[3]; int ihr; char sihr[17], data2log[38], data2log1[23]; unsigned int m, log_data_cnt = 0; void Zero_Fill(char *value) { if (value[1] == 0) { value[1] = value[0]; value[0] = 48; value[2] = 0; } } void Write_Time() { I2C2_Start(); // issue start signal I2C2_Wr(DS1307); // address DS1307 I2C2_Wr(0); // start from word at address (REG0) I2C2_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec) I2C2_Wr(0); // write 0 to minutes word to (REG1) I2C2_Wr(0x17); // write 17 to hours word (24-hours mode)(REG2) I2C2_Wr(0x02); // write 2 - Monday (REG3) I2C2_Wr(0x04); // write 4 to date word (REG4) I2C2_Wr(0x05); // write 5 (May) to month word (REG5) I2C2_Wr(0x01); // write 01 to year word (REG6) I2C2_Stop(); // issue stop signal I2C2_Start(); // issue start signal I2C2_Wr(0xD0); // address DS1307 I2C2_Wr(0); // start from word at address 0 I2C2_Wr(0); // write 0 to REG0 (enable counting + 0 sec) I2C2_Stop(); // issue stop signal } void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) { I2C2_Start(); I2C2_Wr(DS1307); I2C2_Wr(0); I2C2_Repeated_Start(); I2C2_Wr(0xD1); *sec =I2C2_Rd(1); *min =I2C2_Rd(1); *hr =I2C2_Rd(1); *week_day =I2C2_Rd(1); *day =I2C2_Rd(1); *mn =I2C2_Rd(1); *year =I2C2_Rd(0); I2C2_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,7,"PM"); am_pm = 1; } else if(hr < 12) { Lcd_Out(2,7,"AM"); am_pm = 0; } Lcd_Chr(2,11,(hr / 10) + 48); Lcd_Chr(2,12,(hr % 10) + 48); Lcd_Out(2,13,":"); Lcd_Chr(2,14,(min / 10) + 48); Lcd_Chr(2,15,(min % 10) + 48); Lcd_Out(2,16,":"); Lcd_Chr(2,17,(sec / 10) + 48); Lcd_Chr(2,18,(sec % 10) + 48); } void main() { TRISA = 0b00000011; PORTA = 0b00000000; LATA = 0b00000000; TRISB = 0b00100000; PORTB = 0b00000000; LATB = 0b00000000; TRISD = 0b00001111; LATD = 0b00000000; PORTD = 0b00000000; TRISC = 0b00000000; PORTC = 0b00000000; LATC = 0b00000000; TRISE = 0b111; LATE = 0x00; PORTE = 0x00; ANCON0 = 0b00011100; ANCON1 = 0b10011100; ANCON1.PCFG11 = 1; ADCON0 = 0b00000000; //ADCON1 = 0b10110101; ADCON1 = 0b10110101; //CONFIG3H = 0b00001010; CM1CON.CON = 0; CM2CON.CON = 0; cnt = 0; Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1,"RTC"); I2C2_Init(100000); Delay_ms(3000); Lcd_Cmd(_LCD_CLEAR); txt = "Time:"; Lcd_Out(2,1,txt); PORTC.F2 = 1; while(1) { //Write_Time(); Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); Display_Time(sec, min1, hr, week_day, day, mn, year); } }
Proteus realtime simulation is slow. One more thing might be you are using 1 sec or 2 sec or more delays in your while(1) loop. so the rtc time is different from proteus time.
zip and post your proteus .dsn file and mikroC project files. I will check your simulation after 2 hrs. I have to modify the code a little. It is a working code. I have a done rtc usb hid datalogger. It has rtc in it. https://www.edaboard.com/threads/271146/
#ifndef DS1307
#define DS1307 0xD0
#endif
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
unsigned char num,flag;
char b, x, i, sec, min1, hr, week_day, day, mn, year, year1, month1, day1, hours1, mins2, seconds1;
char *txt, tnum[4], time_format[3], sihr[17];
char colon[] = ":";
int cnt, ihr, error, am_pm = 0;
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) {
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,1,txt);
Lcd_Chr(1,5,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,6, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,7,'/');
Lcd_Chr(1,8,(mn / 10) + 48);
Lcd_Chr(1,9,(mn % 10) + 48);
Lcd_Chr(1,10,'/');
Lcd_Out(1,11,"2");
Lcd_Out(1,12,"0");
Lcd_Chr(1,13, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,14, (year % 10) + 48);
if(hr >= 12) {
Lcd_Out(2,2,"PM");
am_pm = 1;
}
else if(hr < 12) {
Lcd_Out(2,2,"AM");
am_pm = 0;
}
Lcd_Chr(2,5,(hr / 10) + 48);
Lcd_Chr(2,6,(hr % 10) + 48);
Lcd_Out(2,7,":");
Lcd_Chr(2,8,(min / 10) + 48);
Lcd_Chr(2,9,(min % 10) + 48);
Lcd_Out(2,10,":");
Lcd_Chr(2,11,(sec / 10) + 48);
Lcd_Chr(2,12,(sec % 10) + 48);
}
void main() {
TRISA = 0b00000111;
PORTA = 0b00000000;
TRISB = 0b00000000;
PORTB = 0b00000000;
ADCON0 = 0b00000000;
ADCON1 = 0b10110101;
cnt = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,4,"RTC DS1307");
Delay_ms(3000);
I2C1_Init(100000);
Lcd_Cmd(_LCD_CLEAR);
while(1) {
//Write_Time();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
}
}
1) its slower than the real time, is it only in simulaiton?
Please edit your last post and wrap your code in code tags. use the # button for code tag.
You said that some buttons are working? How? I have not written the code for buttons.
Why do you need image of my circuit, i have already sent my proteus .dsn file. Is it not opening?
View attachment 83377
The schematic is posted. post your code I will see what is making the button to behave like that.
#ifndef DS1307
#define DS1307 0xD0
#endif
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
unsigned char num,flag;
char b, x, i, sec, min1, hr, week_day, day, mn, year, year1, month1, day1, hours1, mins2, seconds1;
char *txt, tnum[4], time_format[3], sihr[17];
char colon[] = ":";
int cnt, ihr, error, am_pm = 0;
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) {
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,1,txt);
Lcd_Chr(1,5,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,6, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,7,'/');
Lcd_Chr(1,8,(mn / 10) + 48);
Lcd_Chr(1,9,(mn % 10) + 48);
Lcd_Chr(1,10,'/');
Lcd_Out(1,11,"2");
Lcd_Out(1,12,"0");
Lcd_Chr(1,13, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,14, (year % 10) + 48);
if(hr >= 12) {
Lcd_Out(2,2,"PM");
am_pm = 1;
}
else if(hr < 12) {
Lcd_Out(2,2,"AM");
am_pm = 0;
}
Lcd_Chr(2,5,(hr / 10) + 48);
Lcd_Chr(2,6,(hr % 10) + 48);
Lcd_Out(2,7,":");
Lcd_Chr(2,8,(min / 10) + 48);
Lcd_Chr(2,9,(min % 10) + 48);
Lcd_Out(2,10,":");
Lcd_Chr(2,11,(sec / 10) + 48);
Lcd_Chr(2,12,(sec % 10) + 48);
}
void main() {
TRISA = 0b00000111;
PORTA = 0b00000000;
TRISB = 0b00000000;
PORTB = 0b00000000;
ADCON0 = 0b00000000;
ADCON1 = 0b10110101;
cnt = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,4,"RTC DS1307");
Delay_ms(3000);
I2C1_Init(100000);
Lcd_Cmd(_LCD_CLEAR);
while(1) {
//Write_Time();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
}
}
There is no code for button in it. Button cannot work on its own without any code. You have to change ADCON0 and ADCON1 register settings if you are using switches. If tere is ANSEL register in PIC16F877 then configure it also.
I need some time. can you use 4 buttons or 5 buttons?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?