ivalex
Newbie level 4
I have a problem with the code that I found and modified a bit to suit the ccs c compiler
I tried printing out the hours:min:secs and what I got was -1:-1:-1
I checked before the variable secs was used in the writing sequence of the i2c and saw that it is still fine (55)
Is there something wrong with the code? I'm wondering if whats wrong is in the write sequence or the read sequence... btw, the test_ variables were going to be used to compare with the time that the ds1307 produces so that at a certain time, I will enable outputs to all ports (0xFF)
If it would help the "investigation", I have no 3v lithium battery connected to pin3 of the ds1307 since I can't get a hold of any 3v bat... Hopefully this is not the problem
I tried printing out the hours:min:secs and what I got was -1:-1:-1
I checked before the variable secs was used in the writing sequence of the i2c and saw that it is still fine (55)
Is there something wrong with the code? I'm wondering if whats wrong is in the write sequence or the read sequence... btw, the test_ variables were going to be used to compare with the time that the ds1307 produces so that at a certain time, I will enable outputs to all ports (0xFF)
If it would help the "investigation", I have no 3v lithium battery connected to pin3 of the ds1307 since I can't get a hold of any 3v bat... Hopefully this is not the problem
Code:
#include <16F877A.h> //Initialization
#include <string.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT
#use delay (clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, FAST=100000)
unsigned short read_ds1307(unsigned short address );
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned int sec;
unsigned int min;
unsigned int hrs;
unsigned int day;
unsigned int month;
unsigned int yr;
unsigned int dow;
unsigned int data;
unsigned int test_sec;
unsigned int test_minute;
unsigned int test_hour;
unsigned int test_day;
unsigned int test_date;
unsigned int test_month;
unsigned int test_year;
void write_ds1307(unsigned int day, unsigned int mth, unsigned int year, unsigned int dow, unsigned int hr, unsigned int min, unsigned int sec)
{
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(sec); // REG 0
i2c_write(min); // REG 1
i2c_write(hr); // REG 2
i2c_write(dow); // REG 3
i2c_write(day); // REG 4
i2c_write(mth); // REG 5
i2c_write(year); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
i2c_start();
i2c_write(0xD0); // address DS1307
i2c_write(0); // start from word at address 0
i2c_write(0); // write 0 to REG0 (enable counting + 0 sec)
i2c_stop();
}
void ds1307_get_time()
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = i2c_read(1);
min = i2c_read(1);
hrs = i2c_read(1);
dow = i2c_read(1);
day = i2c_read(1);
month = i2c_read(1);
yr = i2c_read(0);
i2c_stop();
}
void init_ds1307()
{
int seconds = 0;
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = i2c_read(0);
i2c_stop();
seconds &= 0x7F;
delay_us(3);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(seconds);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x80); // Disable squarewave output pin
i2c_stop();
}
void main()
{ //Start of Main Program
//set_tris_b(0x00);
set_tris_c(0x18);
Delay_ms(1000);
// Set date for -> 15 June 2005 Tuesday
// Set time for -> 15:20:55
write_ds1307(15,6,5,2,15,20,55); //day,mth,yr,dow,hr,min,sec
while(1)
{
Delay_ms(1000);
ds1307_get_time();
printf("\%02d:\%02d:\%02d\n", hrs,min,sec);
}
} //End of program