Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Using a PIC microcontroller to interface with a DS1302 real time clock

Status
Not open for further replies.

ssylee

New Member
I am using a PIC18F2620 to interface with a DS1302 real time clock. I haven't manually set the time using push button switches yet. I have a set of functions including various data transfer functions, a set of functions that involves setting the current time, a set of functions involving reading the time and displaying it on the LCD, and a function that starts or stops the clock and another function that sets or clears the write protect bit. It appears that the displaying part of the routine is correct, but the actual time displayed is nowhere close to the time that I have set it to. I have checked the timing on the chip enable as well as the SCLK pin, however, they are quite erratic. Does someone have any recommendations on what I should do next? Thanks!
 
ssylee said:
I am using a PIC18F2620 to interface with a DS1302 real time clock. I haven't manually set the time using push button switches yet. I have a set of functions including various data transfer functions, a set of functions that involves setting the current time, a set of functions involving reading the time and displaying it on the LCD, and a function that starts or stops the clock and another function that sets or clears the write protect bit. It appears that the displaying part of the routine is correct, but the actual time displayed is nowhere close to the time that I have set it to. I have checked the timing on the chip enable as well as the SCLK pin, however, they are quite erratic. Does someone have any recommendations on what I should do next? Thanks!

Sounds like there is an issue with the Crystal connection. You need to make sure the leads are as short as possible and solidly connected to the pins (2 & 3).
 
I have a 32 kHz crystal connected between pins 2 and 3 on the breadboard. Not sure if that helps with the problem.
 
ssylee said:
I have a 32 kHz crystal connected between pins 2 and 3 on the breadboard. Not sure if that helps with the problem.

Perhaps the part in red might give you a clue?, breadboards are notorious for poor connections, and have too much capacitance for many uses. Strangely enough I had occasion to scope a 32KHz crystal yesterday - even with a x10 probe it killed the oscillator on one side of the crystal - and that's a LOT less capacitance than a breadboard.
 
ssylee said:
So you're suggesting me to wire up the circuit on an actual circuit board before proceding?

Yes, at least the DS1302 part - I NEVER use breadboards at all!.

You don't need to etch a PCB, use a piece of veroboard (stripboard).
 
Hola Nigel,

I am not trying to say that it should work for anyone else, out there, but in more than 15 years, all my projects, 90% with PICs (starting with the 16C57) were breadboarded. And worked OK.

Yes, with a Xtal of 4 MHz maximum.

Bad quality breadboards (easy to run across) are a trap due to poor contact, I admit.

I insist; that is my experience and not a suggestion to use them.
 
Do you have a capacitor (0.1uF) close to the DS1302 chip between the power pins (1&4) and also pullup resistors (4.7K) on pins 5,6&7.
Ommiting any of those items could make it unreliable.

As others have already said, the crystal placement etc. is very important and it is dealt with in a Maxim app note:
**broken link removed**
 
Don't forget that you can simply connect the 32k crystal and caps to the Tmr1 pins of the 18F2620 and create you own RTC as shown in the Mchip users manual.
 
picasm said:
Do you have a capacitor (0.1uF) close to the DS1302 chip between the power pins (1&4) and also pullup resistors (4.7K) on pins 5,6&7.
Ommiting any of those items could make it unreliable.

As others have already said, the crystal placement etc. is very important and it is dealt with in a Maxim app note:
**broken link removed**

I didn't have a 0.1 uF capacitor connected in between the power pins. I also didn't have pull up resistors on pins 5, 6, 7. I didn't do so b/c I don't recall seeing notes about them on the datasheet.
 
Well you need them. The datasheet will describe the part, not the system. Example in the 16F877A datasheet the USART does not describe a MAX232 and vice versa.
 
What I meant was I didn't see anything about the pullup resistors in the DS1302 datasheet. Btw, after soldering the circuit on a PCB and adding the pullup resistors, it appears that I am still getting the same result. Could the problem be with my code as well?
 
I hate to show myself wrong :( but I guess I had I2C on the brain. The DS1302 does have built in 40K pull DOWN resistors, so take out those pullups.

But to make up for it here's the DS1305 and PIC16F628 with schematic and code.
**broken link removed**
 
atferrari said:
Hola Nigel,

I am not trying to say that it should work for anyone else, out there, but in more than 15 years, all my projects, 90% with PICs (starting with the 16C57) were breadboarded. And worked OK.

Yes, with a Xtal of 4 MHz maximum.

You didn't fully read my post, a scope and x10 probe stopped the 32KHz (NOT 4MHz) crystal working on Thursday when I had occasion to try one, and that's considerably less capacitance than a breadbopard adds!. Considering he appears to be having trouble with his 32KHx crystal, that could well be the reason?.
 
blueroomelectronics said:
I hate to show myself wrong :( but I guess I had I2C on the brain. The DS1302 does have built in 40K pull DOWN resistors, so take out those pullups.

Sorry, I was thinking of the DS1307 when I said that it needed the pullup resistors. You are absolutly right, the DS1302 does not need them.
 
It appears that the displaying part of the routine is correct, but the actual time displayed is nowhere close to the time that I have set it to.

You are reading it as BCD, aren't you?
 
Noted

Nigel Goodwin said:
and that's considerably less capacitance than a breadbopard adds!. Considering he appears to be having trouble with his 32KHx crystal, that could well be the reason?.

Duly noted
 
I have used breadboards for many projects and as I stated in the breadboards thread,
I have never had a problem with capacitance. This is probably because I normally don't go over 20MHz but I have done USB stuff running at 48MHz without any problems. Besides, how much capacitance can there be between 2 strips with a few square mm of area and separated with over a mm of plastic. I really think the capacitance problem is very overstated. Maybe someone can work it out, 40mm² area and 1mm of some plastic (say polycarbonate - Relative permeability 2.8). The long strips are intended for supply and capacitance is always a good thing there. (added) I was curious and so I worked it out. I get 1pF. Completely irrelevant unless your building RF circuits. For the curious I did (8.85*10^-12 * 2.8 * 40*10^-6)/1*10-3

Aren't scope probes still around 10pF even on 10X?

Mike.
 
ssylee,

Is the clock running. If so, is it running slow or fast. If you set a time and immediatly read it back, is it correct?

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top