|
|
View previous topic :: View next topic |
Author |
Message |
guest Guest
|
trouble with ds1302(RTC) |
Posted: Wed Feb 15, 2006 7:43 am |
|
|
Hi everyone,
i am having some trouble with the Real time clock ds1302. The clock does work some times but other times it just refuses to tick. all this is very random. so clock may tick some times and other times it just wont. i am running the clock in normal mode (not battery back up mode)
the connections are as follows:
pin1 ---- 5v
pin2 and pin3 -----32.768khz external crystal
pin4-------GND
pin5--------4.7k resistor pull-up and relavent pin of PIC18f452
pin6 and pin7---------relavent pin of PIC18f452 (according to the driver)
pin8----5v
Some times the clock works but other times it doesnt tick (i can still set the time but the time which is read is the same)
Any suggestions
Best Regards |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Feb 15, 2006 8:32 am |
|
|
If you are not using battery backup, you need to GROUND pin 8. _________________ David |
|
|
guest Guest
|
Trouble using RTC (ds1302) |
Posted: Wed Feb 15, 2006 12:43 pm |
|
|
Thanx for the suggestion.
I have tried the RTC with pin8 grounded but even then the inconsistency is there. some times the clock would tick (minutes count would increment) but other times the RTC will display the same time as was set initially. Cant understand y this is happening, its quit bizarre actually.
Do i need to change the pull up resistor value or something. Please help!!
Best regards |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Feb 15, 2006 2:46 pm |
|
|
Does your software initialise the Clock Halt Flag?
From the DS1302 manual:
Quote: | CLOCK HALT FLAG
Bit 7 of the seconds register is defined as the clock halt (CH) flag. When this bit is set to logic 1, the clock oscillator
is stopped and the DS1302 is placed into a low-power standby mode with a current drain of less than 100nA. When
this bit is written to logic 0, the clock will start. The initial power-on state is not defined. |
Did you check Crystal Considerations with Dallas Real-Time Clocks (RTCs)? It is mentioned in the DS1302 datasheet and some common error problems are discussed in this document including the above mentioned Clock Halt Flag |
|
|
guest Guest
|
trouble with RTC!! |
Posted: Thu Feb 16, 2006 1:05 am |
|
|
thankyou for your reply
i have included the ds1302.c file for interfacing the RTC (ds1302) clock. doesnt this driver cater for the 'clock halt' bit on its own or do we have to set/reset the bit ourselves in the software. if we are suppose to reset the bit ourselves then can u please tell me the syntax for the code.
Many thanks |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 3:29 am |
|
|
Quote: | if we are suppose to reset the bit ourselves then can u please tell me the syntax for the code. | Sorry, but I get the impression here that you are a bit lazy and want me to do your work. I never used the DS1302 but it only took me a few minutes to find the info in the datasheet and check the driver code for correct working. You can do the same.
Code: | i have included the ds1302.c file for interfacing the RTC (ds1302) clock. doesnt this driver cater for the 'clock halt' bit on its own or do we have to set/reset the bit ourselves in the software. | The standard answer to this is would be: Post a small but complete program demonstrating your initialization and access to the DS1302 and we will have a look at it.
Only including ds1302.c isn't enough, are you calling the rtc_init function in this file? |
|
|
guest Guest
|
|
Posted: Thu Feb 16, 2006 4:05 am |
|
|
sorry if i gave u the impression that i am lazy. its just that i have wasted so much time on this RTC that it is becoming rather tedious.
It may be the Clock halt bit problem, but the fact is that some times the clock works fine. Could this inconsistency be due to the clock halt bit. i would imagine that if clock halt were the problem than the RTC would not work at all.
any way here is the code
Code: |
#include <18F452.h>
#device *=16 ADC=10 //change
#include <stdlib.h>
#include <string.h>
#include <math.h>
#USE DELAY (CLOCK=12000000) //this used to be 4mega
#include <DS1302.C>
#fuses HS,NOWDT,NOLVP
#use rs232(baud=9600,xmit=pin_c2,rcv=pin_c3,parity=N,stream=other)
#use rs232(baud=9600,xmit=pin_c1,rcv=pin_c0,parity=N,stream=pc)
byte get_bcd()
{
char first,second;
first=fgetc(other);
first-='0';
second=fgetc(other);
return((first<<4)|(second-'0'));
}
void set_clock()
{
byte day,mth,year,dow,hour,min;
year=get_bcd();
mth=get_bcd();
day=get_bcd();
dow=get_bcd();
hour=get_bcd();
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}
void main()
{
byte day,mth,year,dow,hour,min,sec;
rtc_init();
set_clock();
while(1)
{
delay_ms(10000);
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
fprintf(pc,"%2X%2X",hour,min);
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 4:58 am |
|
|
At first glance your program looks OK. You are calling rtc_init() so the the Clock Halt Flag will be set correctly.
In order to eliminate other potential problems like receiving invalid data over RS-232, try the simplified version of your program as given below.
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOLVP
#USE DELAY (CLOCK=12000000) //this used to be 4mega
#include <DS1302.C>
#use rs232(baud=9600,xmit=pin_c1,rcv=pin_c0,parity=N,stream=pc)
void main()
{
byte day,mth,year,dow,hour,min,sec;
rtc_init();
day=1;
mth=2;
year=6;
dow=5;
hour=11;
min=30;
rtc_set_datetime(day, mth, year, dow, hour, min);
while(1)
{
delay_ms(10000);
rtc_get_time(hour, min, sec);
fprintf(pc,"%2X:%2X:2X\r\n", hour, min, sec);
}
} |
Which version of the compiler are you using? I need this in order to check for possible differences in the ds1302.c driver. |
|
|
guest Guest
|
|
Posted: Thu Feb 16, 2006 6:05 am |
|
|
sorry i forgot to mention the compiler version. it is 3.203. and below is my ds1302.c file
Code: |
#ifndef RTC_SCLK
#define RTC_SCLK PIN_B7
#define RTC_IO PIN_B6
#define RTC_RST PIN_B5
#endif
void write_ds1302_byte(BYTE cmd) {
BYTE i;
for(i=0;i<=7;++i) {
output_bit(RTC_IO, shift_right(&cmd,1,0) );
output_high(RTC_SCLK);
output_low(RTC_SCLK);
}
}
void write_ds1302(BYTE cmd, BYTE data) {
output_high(RTC_RST);
write_ds1302_byte(cmd);
write_ds1302_byte(data);
output_low(RTC_RST);
}
BYTE read_ds1302(BYTE cmd) {
BYTE i,data;
output_high(RTC_RST);
write_ds1302_byte(cmd);
for(i=0;i<=7;++i) {
shift_right(&data,1,input(RTC_IO));
output_high(RTC_SCLK);
delay_us(2);
output_low(RTC_SCLK);
delay_us(2);
}
output_low(RTC_RST);
return(data);
}
void rtc_init() {
BYTE x;
output_low(RTC_RST);
delay_us(2);
output_low(RTC_SCLK);
write_ds1302(0x8e,0);
write_ds1302(0x90,0xa6);
x=read_ds1302(0x81);
if((x & 0x80)!=0)
write_ds1302(0x80,0);
}
void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min) {
write_ds1302(0x86,day);
write_ds1302(0x88,mth);
write_ds1302(0x8c,year);
write_ds1302(0x8a,dow);
write_ds1302(0x84,hr);
write_ds1302(0x82,min);
write_ds1302(0x80,0);
}
void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) {
day = read_ds1302(0x87);
mth = read_ds1302(0x89);
year = read_ds1302(0x8d);
dow = read_ds1302(0x8b);
}
void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) {
hr = read_ds1302(0x85);
min = read_ds1302(0x83);
sec = read_ds1302(0x81);
}
void rtc_write_nvr(BYTE address, BYTE data) {
write_ds1302(address|0xc0,data);
}
BYTE rtc_read_nvr(BYTE address) {
return(read_ds1302(address|0xc1));
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 6:26 am |
|
|
Your ds1302.c is equal to the version I have in v3.226 and I don't see any errors in it.
Two remarks:
1) v3.203 is an unstable compiler version, don't use it for commercial products. Current stable version is v3.236 or use the older v3.191 or v3.216+ versions.
2) You are not allowed to publish the code of CCS supplied drivers.
Most likely your problem is hardware related. Did you check out the document Crystal Considerations with Dallas Real-Time Clocks (RTCs) that I mentioned to you earlier? It has some helpfull info on locating hardware problems. Investigate this and tell us the results. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Feb 16, 2006 6:37 am |
|
|
Disclaimer - I do not use the DS1302 however I am familiar with similar challenges with the DS1307 which may be applicable to your implementation.
The DS1307 also has two power supply connections. If the battery backup input is higher than the primary supply input then it will not enable the DS1307 to be configured. Unfortunately this situation can occur in implementations where no backup battery is configured. There are a couple of work arounds (actually three is you count installing a battery).
1. Instead of connecting the battery terminal to +5v connect it to ground via a 1K resistor.
or
2. Instead of connecting the battery terminal (pin 8) to +5V connect it to a voltage divider. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Thu Feb 16, 2006 8:36 am |
|
|
Guest, I use these all the time, I am only careful to use the correct crystal (6pf) for accuracy and I lay the board out using the suggestions in the data sheet. In the 100's I have used, I don't ever see this problem. I am using a 1F Cap to keep the clock operating on power down. I also use the supplied D1302.C from CCS. I change their code a little to make my 1F cap trickle charge faster. Their default setting is to enable the trickle charge with 1 diode drop and a 4K resistor in series.
This line in RTC_INIT sets the trickle charger:
Code: | write_ds1302(0x90,0xa6); |
Send instead:
Code: | write_ds1302(0x90,0xa0); |
This will disable the trickle charger then just leave the pin floating. It looks like all internal swithes to different diode/resistor combinations will be left open and I don't see in the data sheet whether to ground that pin or not. You may want to try a new chip as well. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 9:19 am |
|
|
I just found APPLICATION NOTE 82: Using the Dallas Tricklecharge Timekeeper. In this document they recommend to ground pin 8 when using the chip without a battery.
Ljbeng is correct in that it is better to change the CCS code and disable the trickle charger, this will lower the power consumption a bit. |
|
|
guest Guest
|
|
Posted: Thu Feb 16, 2006 12:16 pm |
|
|
asmallri a.k.a Andrew you are a genius. you suggestion worked beautifully. i have pulled down pin8 of RTC with 1k resistor and now the RTC works perfectly.
Thanks man , you rule
Best Regards |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 12:37 pm |
|
|
Funny that it works with the resistor to ground.
In the start of this thread you tried connecting pin8 to ground directly with little improvement to stability. The configuration as used by the CCS code configures a 4kOhm resistor to pin 8, I don't see the addition of an extra 1kOhm resistor changing the circuit a lot.
I'm glad for you it works now, but are you sure you didn't change any other thing as well? |
|
|
|
|
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
|