From CocaColaKid:
Code:
'-------------------------- Clock Variables ---------------------------------
rst var portb.2 ' DS1302 Reset Pin
clk var portb.1 ' DS1302 Clock Pin
dq var portb.0 ' DS1302 Data Pin
'----------------------- Write Commands For DS1302 --------------------------
writectrl con $8E ' Control byte
writeram con $80 ' Write to RAM
writeprotect con $00 ' Write-protect DS1302
writesec con $80 ' Write seconds
writemin con $82 ' Write minutes
writehour con $84 ' Write hour
writedate con $86 ' Write date
writemonth con $88 ' Write month
writeyear con $8C ' Write year
'------------------------- Read Commands For DS1302 -------------------------
readsec con $81 ' Read seconds from DS1302
readmin con $83 ' Read minutes from DS1302
readhour con $85 ' Read hours from DS1302
readdate con $87 ' Read date from DS1302
readyear con $8D ' Read year from DS1302
readmonth con $89 ' Read month from DS1302
'------------------------------ Time Variables ------------------------------
mem var byte ' Temporary data holder
outbyte var byte ' Second byte to ds1302
reg_adr var byte ' First byte to DS1302
date var byte ' Date variable
ss var byte ' Seconds variable
mm var byte ' Minutes varibale
hh var byte ' Hours variable
mo var byte ' Month variable
yr var byte ' Year variable
'------------------------ Initial Settings For Ports ------------------------
low rst ' Set reset pin low
low clk ' Set clock pin low
trisb = 0
trisa = 0
'----------------------- Set Initial Time Variables -------------------------
if portb.4 = 1 then START ' Set inital clock start up if jumper is present
reg_adr = writectrl ' Set to control byte
outbyte = writeprotect ' Set turn off protection
gosub w_out ' Send both bytes
reg_adr = writesec ' Set to write seconds register
outbyte = $00 ' Set to write 00 to seconds register
gosub w_out
reg_adr = writemin
outbyte = $30
gosub w_out
reg_adr = writehour
outbyte = $00
gosub w_out
reg_adr = writedate
outbyte = $01
gosub w_out
reg_adr = writemonth
outbyte = $01
gosub w_out
reg_adr = writeyear
outbyte = $00
gosub w_out
reg_adr = writectrl
outbyte = writeprotect
gosub w_out
start:
reg_adr = readsec ' Read seconds
gosub w_in
ss = mem
reg_adr = readmin ' Read minutes
gosub w_in
mm = mem
reg_adr = readhour ' Read Hours
gosub w_in
hh = mem
reg_adr = readyear ' Read Year
gosub w_in
yr = mem
reg_adr = readdate ' Read Date
gosub w_in
date = mem
reg_adr = readmonth ' Read Month
gosub w_in
mo = mem
lcdout $fe,1,"TIME:",HEX2 hh,":",HEX2 mm,":",HEX2 ss
pause 500
goto start
'----------------------- Time Commands Subroutines --------------------------
w_in:
mem = reg_adr ' Set mem variable to reg_adr contents
high rst ' Activate the DS1302
shiftout dq,clk,0, [mem] ' Send control byte
shiftin dq,clk,1, [mem] ' Retrieve data in from the DS1302
low rst ' Deactivate DS1302
return
w_out:
mem = reg_adr ' Set mem variable to reg_adr contents
high rst ' Activate the DS1302
shiftout dq,clk,0, [mem] ' Send control byte
mem = outbyte ' Set mem variable to outbyte contents
shiftout dq,clk,0, [mem] ' Send data stored in mem variable to DS1302
low rst ' Deactivate DS1302
return
I have used an assembler routine with the 1302 (in a basic program) and it works:
Code:
#define SPI_CLK PORTA,1 ; spi bus clock line
#define SPI_MOSI PORTA,2 ; spi master out data
#define SPI_MISO PORTA,3 ; spi slave input data
#define SPI_CE PORTA,4 ; chip enable for SPI device
SCRATCH equ 0x40 ; 1 by general purpose scratchpad
TMP equ 0x41 ; temp register
TMP2 equ 0x42 ; temp register
COUNT equ 0x43
;YRS equ 0x44
;MON equ 0x45
;DOW equ 0x46
;DAYS equ 0x47
;HRS equ 0x48
;MINS equ 0x49
;SECS equ 0x4a
;user_bits equ 0x2C ;
;save_w equ 0x38
;save_status equ 0x39
;---- Read RTC into W (assume address already sent) ----
;---- assumes CE is asserted ----
read_RTC:
movlw 08h ;Send 8 bits
movwf COUNT
SPI_read_loop:
rlf TMP, 1
bcf SPI_CLK ; clock data out
bcf TMP, 0 ; assume data out is low
btfsc SPI_MISO
bsf TMP, 0 ; if data out=1, set bit
bsf SPI_CLK
decfsz COUNT, 1
goto SPI_read_loop
movf TMP, W
return
;--- Write the byte in W to RTC ---
;---- assumes CE is asserted ----
write_RTC:
movwf TMP ;Save the data
;
;--- Do a SPI bus write of byte in 'TMP' ---
;
SPI_write:
movlw 08h ;Send 8 bits
movwf COUNT
SPI_w_loop:
bcf SPI_CLK
bcf SPI_MOSI ; assume data out is low
btfsc TMP, 7
bsf SPI_MOSI ; if data out=1, set bit
SPI_w_cont:
rlf TMP, 1
bsf SPI_CLK ; clock it in
decfsz COUNT, 1
goto SPI_w_loop
return
Cheers, Art.
Bookmarks