hello , every one in the following code , the seven segments clock is working correctly but i don not need to loose the seconds every time i switched on , so i add " void ds1307_init() " but it always show - in seconds - 03 ; so please what is wrong ?
[I've put your code into a [CODE] block so the indentation can be shown -- KrisBlueNZ, moderator]
[I've put your code into a [CODE] block so the indentation can be shown -- KrisBlueNZ, moderator]
Code:
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
char seconds, minutes, hours, day, date, month, year;
char s1, s10, m1, m10, h1, h10, date1, date10, month1, month10, year1, year10;
char x = 0, a = 0, b = 0;
sbit swup at portc.B6;
sbit swdown at portc.B7;
sbit swset at portc.B5;
sbit buzzer at portc.B2;
void read_DS1307(void) {
I2C1_Start();
I2C1_Wr(0xD0); // DS1307 address + 0write
I2C1_Wr(0x00); // second address
I2C1_Repeated_Start();
I2C1_Wr(0xD1); // DS1307 address + 1read
seconds = I2C1_Rd(1);
minutes = I2C1_Rd(1);
hours = I2C1_Rd(1);
day = I2C1_Rd(1);
date = I2C1_Rd(1);
month = I2C1_Rd(1);
year = I2C1_Rd(0);
I2C1_Stop();
}
void transform(void) {
seconds = Bcd2Dec(seconds);
minutes = Bcd2Dec(minutes);
hours = Bcd2Dec(hours);
date = Bcd2Dec(date);
month = Bcd2Dec(month);
year = Bcd2Dec(year);
s1 = seconds % 10;
m1 = minutes % 10;
h1 = hours % 10;
date1 = date % 10;
month1 = month % 10;
year1 = year % 10;
s10 = seconds / 10;
m10 = minutes / 10;
h10 = hours / 10;
date10 = date / 10;
month10 = month / 10;
year10 = year / 10;
}
void display(void) {
portb = mask(s1); porta = 0; delay_ms(10);
portb = mask(s10); porta = 1; delay_ms(10);
portb = mask(m1); porta = 2; delay_ms(10);
portb = mask(m10); porta = 3; delay_ms(10);
portb = mask(date1); porta = 4; delay_ms(1);
portb = mask(date10); porta = 5; delay_ms(1);
portb = mask(month1); porta = 6; delay_ms(1);
portb = mask(month10); porta = 7; delay_ms(1);
portb = mask(year1); porta = 8; delay_ms(1);
portb = mask(year10); porta = 9; delay_ms(1);
}
void ds1307_init(void) {
int seconds = 0;
I2C1_Start();
I2C1_Wr(0xD0); // WR to RTC
I2C1_Wr(0x00); // REG 0
I2C1_Start();
I2C1_Wr(0xD1); // RD from RTC
seconds = Bcd2Dec(I2C1_Rd(0)); // Read current "seconds" in DS1307
I2C1_Stop();
seconds &= 0x7F;
Delay_ms(300);
I2C1_Start();
I2C1_Wr(0xD0); // WR to RTC
I2C1_Wr(0x00); // REG 0
I2C1_Wr(Dec2Bcd(seconds)); // Start oscillator with current "seconds value
I2C1_repeated_Start();
I2C1_Wr(0xD0); // WR to RTC
I2C1_Wr(0x07); // Control Register
I2C1_Wr(0x80); // Disable squarewave output pin
I2C1_Stop();
}
void main(void) {
trisb = 0;
trisa = 0;
trisc = 0b11100000;
portc = 0;
I2C1_Init(100000);
ds1307_init();
while (1) {
read_DS1307();
transform();
display();
}
}
Last edited by a moderator: