- Joined
- Jan 22, 2008
- Messages
- 53,286
- Helped
- 14,794
- Reputation
- 29,875
- Reaction score
- 14,322
- Trophy points
- 1,393
- Location
- Bochum, Germany
- Activity points
- 301,816
49 decimal is 0x31, but you said I have to use bcd? bcd of decimal 49 is 49. Should write 0x49 or just 49? Just 49 means decimal 49, 0x49 means bcd 49.
Makes no sense at all.when I simulated in proteus I actually had to give decimal values 0..15, 22..31, 38..43 for day 0..31 otherwise it is giving wrong values.
#ifndef DS1307
#define DS1307 0xD0
#endif
#define SET RA0_bit
#define INC RA1_bit
#define DEC RA2_bit
#define OK RA3_bit
// 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, set_time_flag;
char b, x, i, sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4], time_format[3], sihr[17];
char colon[] = ":";
int cnt, ihr, error, am_pm = 0;
bit set_flag, inc_flag, dec_flag, ok_flag, do_once_hr, do_once_min, do_once_sec, do_once_day, do_once_mn, do_once_year;
bit dec_do_once_hr, dec_do_once_min, dec_do_once_sec, dec_do_once_day, dec_do_once_mn, dec_do_once_year;
bit inc_do_once_hr, inc_do_once_min, inc_do_once_sec, inc_do_once_day, inc_do_once_mn, inc_do_once_year;
bit alarm_flag, alarm_set_flag;
unsigned char set_cnt, inc_cnt, dec_cnt = 0;
unsigned char ssec, smin, shr, sweek_day, sday, smn, syear;
unsigned char s_sec, s_min, s_hr, s_week_day, s_day, s_mn, s_year;
unsigned char asec, amin, ahr, aweek_day, aday, amn, ayear;
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(s_min); // write 11 to minutes word to (REG1)
I2C1_Wr(s_hr); // write 17 to hours word (24-hours mode)(REG2)
I2C1_Wr(s_week_day); // write 6 - Friday (REG3)
I2C1_Wr(s_day); // write 23 to date word (REG4)
I2C1_Wr(s_mn); // write 11 (Nov) to month word (REG5)
I2C1_Wr(s_year); // write 12 to year word (REG6)
I2C1_Stop(); // issue stop signal
I2C1_Start(); // issue start signal
I2C1_Wr(DS1307); // 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();
I2C1_Start();
I2C1_Wr(DS1307);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
s_sec = I2C1_Rd(1);
s_min = I2C1_Rd(1);
s_hr = I2C1_Rd(1);
s_week_day = I2C1_Rd(1);
s_day = I2C1_Rd(1);
s_mn = I2C1_Rd(1);
s_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);
/*
ssec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
smin = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
shr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
sweek_day =(*week_day & 0x07);
sday = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
smn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
syear = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
*/
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
if(alarm_flag == 0) {
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 Display_Alarm_Time(char asec, char amin, char ahr, char aweek_day, char aday, char amn, char ayear) {
if(alarm_flag == 1) {
Lcd_Out(1,1,"ALR");
Lcd_Chr(1,5,(aday / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,6, (aday % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,7,'/');
Lcd_Chr(1,8,(amn / 10) + 48);
Lcd_Chr(1,9,(amn % 10) + 48);
Lcd_Chr(1,10,'/');
Lcd_Out(1,11,"2");
Lcd_Out(1,12,"0");
Lcd_Chr(1,13, (ayear / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,14, (ayear % 10) + 48);
Lcd_Chr(2,5,(ahr / 10) + 48);
Lcd_Chr(2,6,(ahr % 10) + 48);
Lcd_Out(2,7,":");
Lcd_Chr(2,8,(amin / 10) + 48);
Lcd_Chr(2,9,(amin % 10) + 48);
Lcd_Out(2,10,":");
Lcd_Chr(2,11,(asec / 10) + 48);
Lcd_Chr(2,12,(asec % 10) + 48);
}
}
void main() {
TRISA = 0b00001111;
PORTA = 0b00000000;
TRISB = 0b00000000;
PORTB = 0b00000000;
ADCON0 = 0b00000000;
ADCON1 = 0b10000111;
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);
set_time_flag = 0;
ok_flag = 1;
alarm_flag = 0;
alarm_set_flag = 0;
ssec = 0x00;
smin = 0x00;
shr = 0x00;
sweek_day = 0x01;
sday = 0x00;
smn = 0x00;
syear = 0x00;
asec = 0x00;
amin = 0x00;
ahr = 0x00;
aday = 0x00;
amn = 0x00;
ayear = 0x00;
while(1) {
if((SET == 1) && (INC == 0) && (DEC == 0) &&(OK == 0)) {
Delay_ms(100);
if((SET == 1) && (INC == 0) && (DEC == 0) && (OK == 0)) {
set_flag = 1;
set_time_flag = 1;
set_cnt++;
if(set_cnt > 11) {
set_cnt = 1;
alarm_flag = 0;
}
else if(set_cnt == 7) alarm_flag = 1;
}
}
else if((INC == 1) && (DEC == 0) && (SET == 0) && (OK == 0) && (set_flag == 1)) {
Delay_ms(100);
if((INC == 1) && (DEC == 0) && (SET == 0) && (OK == 0) && (set_flag == 1)) {
inc_flag = 1;
if(alarm_flag == 0) {
switch(set_cnt) {
case 1:
sweek_day++;
if(sweek_day > 7) sweek_day = 1;
break;
case 2:
sday++;
if(sday == 10) sday = 16;
if(sday == 26) sday = 32;
if(sday == 42) sday = 48;
if(sday > 49) sday = 1;
break;
case 3:
smn++;
if(smn == 9) smn = 16;
if(smn > 18) smn = 1;
break;
case 4:
syear++;
if(syear == 10) syear = 16;
if(syear == 26) syear = 32;
if(syear == 42) syear = 48;
if(syear == 58) syear = 64;
if(syear == 74) syear = 80;
if(syear == 90) syear = 96;
if(syear == 106) syear == 112;
if(syear == 122) syear = 128;
if(syear == 138) syear = 144;
if(syear > 153) syear = 0;
break;
case 5:
shr++;
if(shr == 10) shr = 16;
if(shr == 26) shr = 32;
if(shr > 35) shr = 0;
break;
case 6:
smin++;
if(smin == 10) smin = 16;
if(smin == 26) smin = 32;
if(smin == 42) smin = 48;
if(smin == 58) smin = 64;
if(smin == 74) smin = 80;
if(smin > 89) smin = 0;
break;
default:
break;
};
}
else if(alarm_flag == 1) {
switch(set_cnt) {
case 7:
aday++;
if(aday > 31) aday = 1;
break;;
case 8:
amn++;
if(amn > 12) amn = 1;
break;
case 9:
ayear++;
if(ayear > 99) ayear = 0;
break;
case 10:
ahr++;
if(ahr > 23) ahr = 0;
break;
case 11:
amin++;
if(amin > 59) amin = 0;
break;
default:
break;
};
}
}
}
else if((DEC == 1) && (SET == 0) && (INC == 0) && (OK == 0) && (set_flag == 1)) {
Delay_ms(100);
if((DEC == 1) && (SET == 0) && (INC == 0) && (OK == 0) && (set_flag == 1)) {
dec_flag = 1;
if(alarm_flag == 0) {
switch(set_cnt) {
case 1:
sweek_day--;
if(sweek_day < 1) sweek_day = 7;
break;
case 2:
sday--;
if(sday == 15) sday = 9;
if(sday == 33) sday = 25;
if(sday == 47) sday = 41;
if(sday > 49) sday = 49;
break;
case 3:
smn--;
if(smn == 9) smn = 16;
if(smn > 18) smn = 18;
break;
case 4:
syear--;
if(syear == 15) syear = 9;
if(syear == 33) syear = 25;
if(syear == 47) syear = 41;
if(syear == 63) syear = 57;
if(syear == 79) syear = 73;
if(syear == 95) syear = 89;
if(syear == 111) syear == 105;
if(syear == 127) syear = 121;
if(syear == 143) syear = 137;
if(syear > 153) syear = 153;
break;
case 5:
shr--;
if(shr == 15) shr = 9;
if(shr == 33) shr = 25;
if(shr > 35) shr = 35;
break;
case 6:
smin--;
if(smin == 15) smin = 9;
if(smin == 33) smin = 25;
if(smin == 47) smin = 41;
if(smin == 63) smin = 57;
if(smin == 79) smin = 73;
if(smin > 89) smin = 89;
break;
default:
break;
};
}
else if(alarm_flag == 1) {
switch(set_cnt) {
case 7:
aday--;
if(aday > 31) aday = 31;
if(aday < 1) aday = 31;
break;
case 8:
amn--;
if(amn > 12) amn = 12;
if(amn < 1) amn = 12;
break;
case 9:
ayear--;
if(ayear > 99) ayear = 99;
if(ayear < 0) ayear = 99;
break;
case 10:
ahr--;
if(ahr > 23) ahr = 23;
if(ahr < 0) ahr = 23;
break;
case 11:
amin--;
if(amin > 59) amin = 59;
if(amin < 0) amin = 59;
break;
default:
break;
};
}
}
}
else if((OK == 1) && (SET == 0) && (INC == 0) && (DEC == 0) && (set_flag == 1)) {
Delay_ms(100);
if((OK == 1) && (SET == 0) && (INC == 0) && (DEC == 0) && (set_flag == 1)) {
set_flag = 0;
set_time_flag = 0;
ok_flag = 1;
alarm_flag = 0;
alarm_set_flag = 1;
set_cnt = 0;
inc_flag = 0;
dec_flag = 0;
}
}
if((inc_flag == 1) || (dec_flag == 1)) {
s_sec = ssec;
s_min = smin;
s_hr = shr;
s_week_day = sweek_day;
s_day = sday;
s_mn = smn;
s_year = syear;
/*
ssec = s_sec;
smin = s_min;
shr = s_hr;
sweek_day = s_week_day;
sday = s_day;
smn = s_mn;
syear = s_year;
*/
/*
s_sec = Dec2Bcd(ssec);
s_min = Dec2Bcd(smin);
s_hr = Dec2Bcd(shr);
s_week_day = Dec2Bcd(sweek_day);
s_day = Dec2Bcd(sday);
s_mn = Dec2Bcd(smn);
s_year = Dec2Bcd(syear);
*/
}
//Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
if(set_time_flag == 1) {
Write_Time();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
ok_flag = 0;
}
if(ok_flag == 1) {
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
}
if(alarm_flag == 0) {
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
if(alarm_set_flag == 1) {
//if((day == aday) && (mn == amn) && (year == ayear) && (hr == ahr) && (min1 == amin)) {
//PORTB.F0 = 1;
//}
if((day == aday) && (mn = amn) && (year == ayear) && (hr == ahr) && (min1 == amin)) {
PORTB.F0 = 1;
}
}
}
if(alarm_flag == 1) {
Display_Alarm_Time(asec, amin, ahr, aweek_day, aday, amn, ayear);
}
}
}
if(syear == 10) syear = 16;
if(syear == 26) syear = 32;
if(syear == 42) syear = 48;
if(syear == 58) syear = 64;
if(syear == 74) syear = 80;
if(syear == 90) syear = 96;
if(syear == 106) syear [B] [COLOR="#0000FF"]==[/COLOR] [/B] 112;
if(syear == 122) syear = 128;
if(syear == 138) syear = 144;
if(syear > 153) syear = 0;
if(syear == 106) syear == 112;
if(syear == 111) syear == 105;
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?