|
|
View previous topic :: View next topic |
Author |
Message |
samuel.sam77
Joined: 21 Feb 2012 Posts: 3 Location: Coimbatore
|
my rtc code does not write and read |
Posted: Fri Jul 20, 2012 6:49 am |
|
|
hi dear i am doing own rtc clock here is my code the display shows only
Time : 00:00:00
Date : 00:00:00
why it does not set time
pic16f877a uC
Ds1307
32.768Khz am using with 3V battery
but not working
someone please help me
Code: |
#include<lcd.c>
#include "ds1307.c"
byte sec;
byte min;
byte hrs;
byte day;
byte month;
byte year;
byte dow;
void main()
{
lcd_init();
ds1307_init();
//port_b_pullups(TRUE);
lcd_putc("\Welcome\n");
ds1307_set_date_time(31,12,9,2,23,59,55);//day, month,year,dow,hr,min.sec
while(1)
{
ds1307_get_date(day,month,year,dow);
ds1307_get_time(hrs,min,sec);
lcd_gotoxy(1,1);
printf(lcd_putc,"Time : %02d:%02d:%02d",hrs,min,sec);
lcd_gotoxy(1,2);
printf(lcd_putc,"Date : %02d:%02d:%02d",dow,month,year);
}
} |
my ds1307.c is
Code: |
////////////////////////////////////////////////////////////////////////////////
/// DS1307.C ///
/// Driver for Real Time Clock ///
/// ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
/// used when PIC loses power and DS1307 run from 3V BAT ///
/// - Disable squarewave output ///
/// ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec) Set the date/time ///
/// ///
/// ds1307_get_date(day,mth,year,dow) Get the date ///
/// ///
/// ds1307_get_time(hr,min,sec) Get the time ///
/// ///
////////////////////////////////////////////////////////////////////////////////
#define RTC_SDA PIN_C4
#define RTC_SCL PIN_C3
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);
void ds1307_init(void)
{
BYTE seconds = 0;
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
i2c_stop();
seconds &= 0x7F;
delay_us(3);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x10); // Disable squarewave output pin
i2c_stop();
}
void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x10); // REG 7 - Disable squarewave output pin
i2c_stop();
}
void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03); // Start at REG 3 - Day of week
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f); // REG 3
day = bcd2bin(i2c_read() & 0x3f); // REG 4
mth = bcd2bin(i2c_read() & 0x1f); // REG 5
year = bcd2bin(i2c_read(0)); // REG 6
i2c_stop();
}
void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = bcd2bin(i2c_read(0) & 0x3f);
i2c_stop();
}
BYTE bin2bcd(BYTE binary_value)
{
BYTE temp;
BYTE retval;
temp = binary_value;
retval = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}
return(retval);
}
// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
BYTE temp;
temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;
// Now return: (Tens * 8) + (Tens * 2) + Ones
return(temp + (temp >> 2) + (bcd_value & 0x0f));
} |
_________________ Samuel.M |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Fri Jul 20, 2012 9:17 am |
|
|
Q1: Do you have proper I2C bus pullup resistors? I've used 3k3 and 4k7 with 5 volt PICs. |
|
|
samuel.sam77
Joined: 21 Feb 2012 Posts: 3 Location: Coimbatore
|
|
Posted: Sat Jul 21, 2012 12:43 am |
|
|
temtronic wrote: | Q1: Do you have proper I2C bus pullup resistors? I've used 3k3 and 4k7 with 5 volt PICs.
|
Yes I used 4.7K both sda and scl to 5V. _________________ Samuel.M |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Jul 21, 2012 2:23 am |
|
|
Triple check your connections to the 1307.
Things easy to forget - ground as well as the data lines. Similarly power.
Then, what is feeding Vbatt?. The chip will refuse to connect, if the voltage on this pin is 4v or higher, when the main supply rail is 5v. Battery 'selection' is vital. So, 'what 3v battery'?. Have you actually tested the cell voltage?. What voltage is on the 5v rail?. The lower this is, the lower the 'switch off' point. A single '3v', Lithium cell, can give over 3.7v. The chip will then stop responding, if Vcc is about 4.7v!....
Beware of your variable names.
You have global variables called sec, min etc. Then exactly the same names used for the local variables in the functions. Then you have a function declared, which presumably is meant to receive pointers, but are using the '&' declaration in the function, not '*', and then call this with numbers (which don't directly have an address), and write to these!. Several problems waiting to happen here......
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|