Hi All, Hope I posted the code correctly in the box
I have been working on this for about 8 hours now and have GOOGLED it to death, I am usualy very good at finding my solution on my own.
When I set the time I get some strange outputs on the LCD, like I am thinking HEX and it is thinking DEC and visa versa.
I have writen some test code to play with the modifiers (DEC, HEX, BIN) to see if I could figure it out but..................
The trees seem to be so thick I can't see the problem on this one.
Any pointers would be great.
Thanks
Aaron
Code:
'----------------------- Chip Setup ----------------------------------------
Define osc 4
CMCON = 7 ' PortA = digital I/O
VRCON = 0 ' A/D Voltage reference disabled
TRISA = %11110000 ' Set PortA I/O
TRISB = %11110001 ' Set PortB I/O
PORTA = 0 ' Clear PortA
PORTB = 0 ' Clear PortB
'-------------------------- I/O Variables ----------------------------------
rst var portb.2 ' DS1302 Reset PIC Pin 8 Blue
clk var portb.1 ' DS1302 Clock PIC Pin 7 Orange
dq var portb.0 ' DS1302 Data PIC Pin 6 White
LCD var PortA.2 ' PIC pin 1
'----------------------- 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
'------------------------- Commands For LCD ---------------------------------
Baud con 16780 'Baud rate 2400 8N1
'------------------------------ 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
ss2 var byte 'seconds comparison byte
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
'----------------------- Set Initial Time ----------------------------------
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 = $2D
gosub w_out
reg_adr = writehour
outbyte = $04
gosub w_out
reg_adr = writedate
outbyte = $A
gosub w_out
reg_adr = writemonth
outbyte = $01
gosub w_out
reg_adr = writeyear
outbyte = $09
gosub w_out
reg_adr = writectrl
outbyte = writeprotect
gosub w_out
'----------------------- Main Program -----------------------------------
Main_Program:
gosub Get_time
gosub Display
goto Main_Program
end
'----------------------- Get Time Subroutine ---------------------------
Get_time:
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
return
'----------------------- Display Subroutine ---------------------------
Display:
pause 250
serout2 LCD,Baud,[12,14, "TIME ", hex2 hh,":", hex2 mm,":", hex2 ss]
return
'----------------------- 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
Bookmarks