PDA

View Full Version : Control Register of DS1302 real time clock



agentile
01-11-2006, 11:01 PM
Hello,
· I am working with the Dallas DS1302 real time clock, and I am trying to understand how to handle the control register.· I have download some code from the Parallax Stampworks site, and I have the chip working.· However, there is some code in there which I don't understand.· I've included two subroutines which come from the stampworks experiment #30.· The value of reg·is $8E, and the value of ioByte is $00.· According to the DS1302 datasheet, $8E is a command to write to the control register of the chip.· But what does writing $00 accomplish?

Anyway, I am going into too much detail.· What I need is an explanation of how to set the control register.

thanks,

Andrew


Code from experiment #30 from stampworks

RTC_Out:
HIGH CS1302
SHIFTOUT DataIO,Clock,LSBFIRST,[reg,ioByte]
LOW CS1302
RETURN
RTC_In:
HIGH CS1302
SHIFTOUT DataIO,Clock,LSBFIRST,[reg]
SHIFTIN DataIO,clock,LSBPRE,[ioByte]
LOW CS1302
RETURN

Chris Savage
01-11-2006, 11:09 PM
Hello,

·· Please refer to the following link for a completely documented DS1302_Demo Code which should explain in detail the ways the commands are formed and used to access the chip.· If you still have any questions, post them here and we will address them.·

http://forums.parallax.com/showthread.php?p=531080

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

agentile
01-11-2006, 11:29 PM
Chris,

· Sorry about my previous·poorly worded inquiry.· I·had already looked into that code you listed.· My question is sort of specific.· I have used parts of your older code in my own code, and I have been successful at making the·chip work.· There is just one register I need help on.··In your code, I cannot understand is why you are sending commands to the control register.· In my own code, I have removed those commands and it seems to make no difference.

· The datasheet shows a mostly-detailed listing of all registers on page 7 (of the datasheet).· However, there is only one settable bit in the control register, which is listed as WP.· I assume that·this must be write-protect.· Your code sets this to 0, so zero must mean WP false.· Is this true?· And why would you ever set WP to 1 (control reg = $80)?·

· One other question...after your code sends a 0 to the control register, you then read the contents of the control register, and place the contents into ioByte.· I don't understand why you would read the register just after writing to it.··Could you please explain?

thanks,

Andrew·

Chris Savage
01-11-2006, 11:36 PM
Where are you referring to that it's written, then read?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

agentile
01-12-2006, 12:17 AM
Chris,

· The code shown below comes from the DS1302_Demo.bs2, and it also occurs in Experiment #30 in stampworks.· The first subroutine, RTC_Out, sends reg ($8E) to the control register.· According to the datasheet, this is a command to write to the control register.· Then·ioByte ($00) is sent as the data.· This seems to be setting the MSB of the control register, WP, to zero.· The second subroutine, RTC_In,·calls the control register and then reads the control register value and places it into ioByte.··

· This is·as much sense as I have been able to make out of the code.· So, what I would like to know is what is the purpose of these two subroutines?





RTC_Out:
· HIGH CS1302·························· ' Select DS1302
· SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte]
· LOW CS1302··························· ' Deselect DS1302
· RETURN

RTC_In:
· HIGH CS1302·························· ' Select DS1302
· SHIFTOUT DataIO, Clock, LSBFIRST, [reg]
· SHIFTIN DataIO, Clock, LSBPRE, [ioByte]
· LOW CS1302··························· ' Deselect DS1302
· RETURN

Chris Savage
01-12-2006, 01:25 AM
Okay,

·· Now I see what you're saying.· I think perhaps you misunderstand how the code is working.· The reg variable in both routines specifies and tells the DS1302 which register you are accessing (reading or writing), while the ioByte is the data that is read/written from/to that register.·

·· So in your example, I am not writing one value then another, I am telling the DS1302 which register I want to change, and then I am sending the value to change that register.· I can double check with the datasheet if you still feel that is in error, but it should be correct.

·· As to why you would want to write protect the unit, which I didn't do in this example, but have in other codes, is that you may only update the data just once in a great while, but reading the device many times per second.· There is always the change of something happening, be it noise, or a program error which could possibly alter some settings or registers.· To prevent that in your routine where you set the device you would unprotect it.· At the evry end you would protect it.· Then after that the values can only be read, but not written.· When you're ready to change data, same routine will automatically unprotect it, etc.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Chris Savage
01-12-2006, 01:30 AM
As a follow up...This is from the datasheet:


DS1302 Datasheet said...WRITE PROTECT BIT
Bit 7 of the control register is the write protect bit. The first seven bits (bits 0 – 6) are forced to zero and will always read a zero when read. Before any write operation to the clock or RAM, bit 7 must be zero. When high, the write protect bit prevents a write operation to any
other register. The initial power on state is not defined.· Therefore the WP bit should be cleared before attempting to write to the device.
This explains why it's cleared initially.· In practice this code is for demo purposes and to understand how to access and program various parts of the chip, except the trickle-charging circuit, simply because that varies with the type and rating of battery, so I just use a standard battery in my circuits.· But as I said earlier, normally you would clear the bit when ready to make changes and set it when you're done.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

agentile
01-12-2006, 01:33 AM
Chris,

· Thank you.· That is exactly what I was asking.