6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Sep 28, 2024 10:07 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Mon Jan 12, 2015 5:17 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1738
Location: Sacramento, CA
Hi All,

When I built my CF card interface, I added a DS1302 Real Time clock to provide file date/time stamping. The chip is easy to work with and, using a coin battery, provides a great time of day reference.

Last week, I found a small module on ebay that uses the DS1302 and though it would be a great addition for those wanting a time of day clock.
Here's a link:
http://www.ebay.com/itm/201172366130

Attachment:
$_57.jpg
$_57.jpg [ 61.34 KiB | Viewed 2284 times ]


At $0.99 (including battery!), its a great deal.

For those interested, you can use any three port pins on a 6522 to access the clock. The /RST pin on the board is actually the CE pin. I am including my driver code to access the chip. The code uses 6502 opcodes. I have a WDC65C02 version that takes advantage of the TRB and TSB commands if anyone is interested.

Here's the code:
Code:
;***********************************************;
;  DS1302 Real Time Clock Driver for 6502   ;
;  v1.0                 1/11/2015   ;
;  Written by Daryl Rictor         ;
;***********************************************

;-----------------------------------------------
; IO Device labels
;-----------------------------------------------
RTC_CE_DDR   =   $0283      ; VIA1DDRA
RTC_CE_DP   =   $0281      ; VIA1DRA
RTC_CE_B   =   $01      ; PA0
RTC_SC_DDR   =   $0283      ; VIA1DDRA
RTC_SC_DP   =   $0281      ; VIA1DRA
RTC_SC_B   =   $02      ; PA1
RTC_IO_DDR   =   $0283      ; VIA1DDRA
RTC_IO_DP   =   $0281      ; VIA1DRA
RTC_IO_B   =   $04      ; PA2

;-----------------------------------------------
; RAM Data location for Date/Time values
;-----------------------------------------------
rtcSec      =   $7FF0      ; sec
rtcMin      =   $7FF1      ; min
rtcHR      =   $7FF2      ; hr
rtcDay      =   $7FF3      ; day
rtcMon      =   $7FF4      ; month
rtcDOW      =   $7FF5      ; day of week
rtcYR      =   $7FF6      ; yr
rtcPR      =   $7FF7      ; Write protect

      
;-----------------------------------------------
; Write a cmd/data combo from x,a to the RTC.
;-----------------------------------------------

WrCmd      pha            ; save data
      lda   RTC_CE_DP
      ora   #RTC_CE_B
      sta   RTC_CE_DP      ; set CE to 1
      txa            ; move cmd to A
      jsr   WrByte         ; write cmd to RTC
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO pin to output
      pla            ; restore data byte   
      jsr   WrByte         ; write data to RTC
      lda   RTC_CE_DP
      and   #255-RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 0
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      rts

;-----------------------------------------------
; Clock out the byte in A to the RTC.
; Data is clocked out starting with bit 0.
; Data is clocked into the RTC on the rising edge.
;-----------------------------------------------

WrByte      ldy    #8         ; 8 bits to clock out.
WrByte1      lsr            ; rotate bit 0 into C
      pha            ; save data
      bcc    WrByte2         ; if bit = 0
      lda   RTC_IO_DP
      ora   #RTC_IO_B
      sta   RTC_IO_DP      ; set IO bit to 1
      jmp    WrByte3
WrByte2      lda   RTC_IO_DP
      and   #255-RTC_IO_B      ; set IO bit to 0
      sta   RTC_IO_DP
WrByte3      lda   RTC_SC_DP
      ora   #RTC_SC_B
      sta   RTC_SC_DP      ; set sc to 1
      dey   
      beq   WrByte4         ; do 8 bits          
      lda   RTC_SC_DP
      and   #255-RTC_SC_B
      sta   RTC_SC_DP      ; set sc to 0
      pla            ; restore data   
      jmp   WrByte1
WrByte4      pla            ; restore stack
      lda   RTC_IO_DDR      
      and   #255-RTC_IO_B
      sta   RTC_IO_DDR      ; set data to input
      lda   RTC_IO_DP      
      and   #255-RTC_IO_B
      sta   RTC_IO_DP      ; set data to 0
      lda   RTC_SC_DP      
      and   #255-RTC_SC_B
      sta   RTC_SC_DP      ; set SC to 0
      rts

;-----------------------------------------------
; Read a byte following write of command byte.
; command in A, data returned in A
;-----------------------------------------------

RdCmd      pha            ; save command
      lda   RTC_CE_DP
      ora   #RTC_CE_B
      sta   RTC_CE_DP      ; set CE to 1
      pla            ; restore command
           jsr   WrByte
      jsr   RdByte
      pha            ; save data
      lda   RTC_CE_DP
      and   #255-RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 0
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      pla            ; restore data
      rts

;-----------------------------------------------
; read in a byte from the RTC to A
;-----------------------------------------------

RdByte      ldx   #$00         ; data byte
      ldy   #8
RdByte1      lda   RTC_IO_DP      ; read IO
      and   #RTC_IO_B      ; mask IO bit
      clc            ; Clear carry.
      beq   RDByte2
      sec            ; Set Carry Flag
RDByte2      txa            ; get data
      ror            ; Rotate data into bit.
      tax            ; save data
      lda   RTC_SC_DP
      ora   #RTC_SC_B
      sta   RTC_SC_DP      ; set sc to 1
      nop
      nop
      lda   RTC_SC_DP
      and   #255-RTC_SC_B
      sta   RTC_SC_DP      ; set sc to 0
      dey            ; 8 bits
           bne    RdByte1
      txa            ; put data in A
      rts

;-----------------------------------------------
; Read calendar in burst mode
; Saves data read to RAM locates defined above
;-----------------------------------------------

RdCal      lda   RTC_CE_DP
      ora   #RTC_CE_B
      sta   RTC_CE_DP      ; set CE to 1
      lda   #$BF         ; burts mode read calendar
      jsr   WrByte
      jsr   RdByte
      sta   rtcSec         ; sec
      jsr   RdByte
      sta   rtcMin         ; min
      jsr   RdByte
      sta   rtcHR         ; hr
      jsr   RdByte
      sta   rtcDay         ; day
      jsr   RdByte
      sta   rtcMon         ; month
      jsr   RdByte
      sta   rtcDOW         ; dow
      jsr   RdByte
      sta   rtcYR         ; yr
      jsr   RdByte
      sta   rtcPR         ; WP
      lda   RTC_CE_DP
      and   #255-RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 0
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      rts

;-----------------------------------------------
; Write calendar in burst mode
; gets values from RAM locates defined above
;-----------------------------------------------

WrCal      ldx   #$8E         ; write WP byte
      lda   #$00         ; turn WP off
      jsr   WrCmd
      lda   RTC_CE_DP
      ora   #RTC_CE_B
      sta   RTC_CE_DP      ; set CE to 1
      lda   #$BE         ; burts mode write calendar
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcSec         ; secs
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcMin         ; mins
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcHR         ; hrs
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcDay         ; day
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcMon         ; month
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcDow         ; dow
      jsr   WrByte
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcYR         ; yr
      jsr   WrByte   
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      lda   rtcPR         ; write protect
      jsr   WrByte
      lda   RTC_CE_DP
      and   #255-RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 0
      lda   RTC_IO_DDR
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set IO to output
      rts

;-----------------------------------------------
; Initialize the DS1302
;-----------------------------------------------

ds1302_init   lda   RTC_CE_DDR
      ora   #RTC_CE_B
      sta   RTC_CE_DDR      ; set CE to output
      lda   RTC_CE_DP
      ora   #RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 1
      lda   RTC_SC_DDR
      ora   #RTC_SC_B
      sta   RTC_SC_DDR      ; set SC to output
      lda   RTC_SC_DP
      and   #255-RTC_SC_B
      sta   RTC_SC_DP      ; set CE = 0
      lda   RTC_IO_DDR      
      ora   #RTC_IO_B
      sta   RTC_IO_DDR      ; set data to output
      lda   RTC_IO_DP      
      and   #255-RTC_IO_B
      sta   RTC_IO_DP      ; set data to 0
      lda   RTC_CE_DP
      and   #255-RTC_CE_B
      sta   RTC_CE_DP      ; set CE = 1
      ldx   #$90         ; write charger control reg
      lda   #$00         ; turn trickle charger off
      jsr   WrCmd         ;
      ldx   #$8E         ; write WP byte
      lda   #$00         ; turn WP off
      jsr   WrCmd
      lda   #$81         ; read seconds
      jsr   RDCmd
      and   #$7F         ; turn off clk halt bit
      ldx   #$80         ; seconds
      jsr   WrCmd         ; enable clock
      ldx   #$8E         ; write WP byte
      lda   #$80         ; turn WP off
      jsr   WrCmd
      rts

;-----------------------------------------------
; END
;-----------------------------------------------


You only need to run the DS1320_init code once after the battery is installed. It just gets the clock running and turns off the trickle charging circuits.

The data bytes are BCD coded. To set the clock, enter the date and time in the RAM locations and call the WrCal routine.

Calling the RdCal routine will copy the date and time to the RAM locations where your code can use it.

Here are the valid ranges for the data bytes:
rtcSec - 0-59
rtcMin - 0-59
rtcHR - 1-12 or 0-23 (0-23 is entered directly - see data sheet for using a 12 hour clock)
rtcDay - 1-31
rtcMon - 1-12
rtcDOW - 1-7 (you can set any reference you like 1-SUN, 1-MON, etc. - it just cycles through the range)
rtcYR - 00-99
rtcPR - $80 (leave this set to $80)

I hope someone finds this useful.

Daryl


Attachments:
File comment: Source Code for DS1302
ds1302.asm [6.89 KiB]
Downloaded 93 times

_________________
Please visit my website -> https://sbc.rictor.org/
Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 6:47 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8404
Location: Midwestern USA
8BIT wrote:
...I found a small module on ebay that uses the DS1302 and though it would be a great addition for those wanting a time of day clock...At $0.99 (including battery!), its a great deal.

Not to be a curmudgeon, but that price raises a red flag.

The budgetary price for the DS-1302 listed at Maxim's site is 1.46 USD each in 1000 piece quantities. "Budgetary" means the price an OEM or distributor could expect to pay for that device if ordered in increments of 1000 pieces. Digi-Key lists that device at 1.7748 USD each if 1000 pieces are ordered.

I don't see how the whole module can be produced at a low enough cost to allow the seller to make a profit at a 99 cent piece price, especially with the inclusion of a battery. I suspect that the DS-1302 being shipped with the module may be a counterfeit part and would recommend caution to anyone thinking of buying this item.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 7:14 am 
Offline

Joined: Sun Apr 10, 2011 8:29 am
Posts: 597
Location: Norway/Japan
.. and free shipping! :-)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 9:22 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8520
Location: Southern California
See also the Philips/NXP PCF8593P I2C RTC which has an alarm and pulls the IRQ\ output pin low when the time matches.

Edit: Uh-oh, I see now that it may have gone out of production-- or maybe that's just the DIP version.

One thing I'm not the most fond of regarding the NXP part as well as many others is that their output is in decimal rather than hex.

Another RTC I used 25+ years ago is the Saronix RTC58321 which had even the crystal integrated into the DIP. It had a very slow 4-bit parallel interface.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 10:53 am 
Offline
User avatar

Joined: Wed Feb 13, 2013 1:38 pm
Posts: 588
Location: Michigan, USA
Has anyone tried one of the DS3231 boards? It's I2C rather than serial and includes a 2ppm TCXO. All for $1.22, including shipping. Can that be right?

Mike

Image


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 11:11 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8520
Location: Southern California
Michael wrote:
Has anyone tried one of the DS3231 boards? It's I2C rather than serial and includes a 2ppm TCXO. All for $1.22, including shipping. Can that be right?

The DS1302 above is I2C also. It is a synchronous serial interface. "Serial" today seems to mean I2C or SPI more often than it does RS-232. I don't know how they can make them so cheap though, unless they're dumping, ie, selling them at a loss to try to gain some foothold in the market. I'm not too concerned though, as I'm not into the consumer market which is so price-sensitive. I got a cheap Chinese IDE-to-SD-card converter, and it needed repair almost immediately because they used lead-free solder that cracked. I of course used leaded solder to fix it. Companies are putting 6502's into products at a rate of hundreds of millions a year partly because of its economy of silicon. We sure don't see that in the hobby and light-industrial field, yet it's still worth it to us to use the 6502 anyway, for other reasons. To me, if I'm only going to buy one or two little RTC modules, it doesn't really matter if it's $1 or $5, maybe even $10. OTOH, a device programmer that's hundreds of dollars definitely makes me delay the purchase.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 12:42 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1738
Location: Sacramento, CA
BigDumbDinosaur wrote:
8BIT wrote:
...I found a small module on ebay that uses the DS1302 and though it would be a great addition for those wanting a time of day clock...At $0.99 (including battery!), its a great deal.

Not to be a curmudgeon, but that price raises a red flag.

The budgetary price for the DS-1302 listed at Maxim's site is 1.46 USD each in 1000 piece quantities. "Budgetary" means the price an OEM or distributor could expect to pay for that device if ordered in increments of 1000 pieces. Digi-Key lists that device at 1.7748 USD each if 1000 pieces are ordered.

I don't see how the whole module can be produced at a low enough cost to allow the seller to make a profit at a 99 cent piece price, especially with the inclusion of a battery. I suspect that the DS-1302 being shipped with the module may be a counterfeit part and would recommend caution to anyone thinking of buying this item.


Good observations BDD. All that crossed my mind too. I did order one and will report back with the results - good or bad. The photo shows the DS1302 is socketed, so if it is a counterfeit part that does not work, one could buy a "real" DS1302 and install it - and still not have spent too much.

Daryl

_________________
Please visit my website -> https://sbc.rictor.org/


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 12:56 pm 
Offline

Joined: Mon Aug 05, 2013 10:43 pm
Posts: 258
Location: Southampton, UK
GARTHWILSON wrote:
Another RTC I used 25+ years ago is the Saronix RTC58321 which had even the crystal integrated into the DIP. It had a very slow 4-bit parallel interface.


I use a DS3234 (http://datasheets.maximintegrated.com/en/ds/DS3234.pdf) in my computer. It's SPI and has a built in crystal oscillator, temperature sensor, 256 bytes of SRAM and alarms. Not particularly cheap, but it's a very nice part. Also not available in DIP but it is in fairly easy to solder SO.

EDIT: Fix part name..

_________________
8 bit fun and games: https://www.aslak.net/


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 13, 2015 3:53 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
8BIT wrote:
if it is a counterfeit part that does not work, one could buy a "real" DS1302 and install it - and still not have spent too much.
Spending too much is pretty clearly not an issue here! And I suspect counterfeiters will be pursuing more lucrative markets.

My guess is that the deal is a promo, perhaps simply to harvest your email and/or postal address. (And that's fairly benign; even major suppliers put you on their spam lists.)

Garth makes a good point that, in hobbyist volumes (onesy-twosy), the price hardly matters. If it's a matter of only a few dollars most of us would begin by ensuring it's a chip we'll be happy with in the long term.

-- Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 29, 2015 12:51 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1738
Location: Sacramento, CA
The DS1302 module arrived last night. I connected it up, initialized it, set the date and time, and it worked fine. I then shut it down for the night. Got up this morning and checked it and the clock was running as expected, no surprises.

The module did come with the 2032 battery installed, so I'm not sure how long it will run for before it dies, but hey, for $0.99, I am satisfied.

I'll keep checking up on it over the next few weeks and let you all know how it holds up.

thanks!

Daryl

_________________
Please visit my website -> https://sbc.rictor.org/


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 29, 2015 4:56 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8404
Location: Midwestern USA
8BIT wrote:
The module did come with the 2032 battery installed, so I'm not sure how long it will run for before it dies, but hey, for $0.99, I am satisfied.

When on battery, the (genuine) DS1302 draw microamps, so I wouldn't be concerned about battery life. I'm still suspect of the 1302's provenance. I can't see how anyone can produce that module, ship it for free and even manage to break even at 99 cents.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 12, 2015 12:02 am 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1005
Location: Canada
BigDumbDinosaur wrote:
I can't see how anyone can produce that module, ship it for free and even manage to break even at 99 cents.


It is, after all, only $0.99. How far wrong could you go?

Even someone as poor as me wouldn't loose their house over a buck.
:D

_________________
Bill


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


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 post attachments in this forum

Search for:
Jump to: