Hi All,
I cannot make a ds1302 RTC work with a pic16F777.
I have used the RTC before with a smaller pic and that worked fine. For this project I need the 3 x HWPWM capabilities of the 16F777. I believe there is something wrong with the config settings - but I'm at a total loss for what the might be. Any help would be gratefully appreciated.
In the code below, I've stripped it down so it should just be displaying the secondsLSB on a seven segment display
Many Thanks,
orjon.
Code:
@ DEVICE PIC16F777, INTRC_OSC_NOCLKOUT, WDT_OFF, BOREN_0 & BORSEN_0, MCLR_OFF
DEFINE OSC 8
OSCCON = %01110010 'see data sheet for INTRC speed setting
ADCON1 = 15 ' All I/O pins digital
INTCON.7 = 0 ' tunr off all interuppts
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00000000
'Set CCPx pins to outputs
TRISC.2=0 ' CCP1 output
TRISC.1=0 ' CCP2 output (could also be assigned to RB3)
TRISB.5=0 ' CCP3 output
' Set CCP modules to PWM mode
CCP1CON = %00001100 ' Mode select = PWM
CCP2CON = %00001100 ' Mode select = PWM
CCP3CON = %00001100 ' Mode select = PWM
' Set period up for 1.22kHz PWM freq
PR2 = $FF
pie2.0 = 0
pie2.1 = 0
pie1.2 = 0
' Set TMR2 up for 1:16 prescale & turn it on
T2CON = %00000110 ' TMR2 ON 1:16 prescale
A var portd.4
B var portd.5
C var portd.6
D var portd.7
'-------------------------- Clock Variables ---------------------------------
DS1302_CLK var portc.4 ' DS1302 Clock Pin
DS1302_DQ var portc.5 ' DS1302 Data Pin
DS1302_RST var portc.6 ' DS1302 Reset 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
mo var byte ' Month variable
yr var byte ' Year variable
xx var byte ' temp hold of above possible bytes
yy var byte
Seconds var byte
Minutes Var Byte
Hours var byte
SecondsLSB VAR byte
MinutesLSB VAR byte
HoursLSB VAR byte
SecondsMSB VAR byte
MinutesMSB VAR byte
HoursMSB VAR byte
BCDSeconds var byte
BCDMinutes VAR Byte
BCDHours Var byte
Seconds =0
Minutes =0
Hours =0
SecondsLSB =0
MinutesLSB =0
HoursLSB =0
SecondsMSB =0
MinutesMSB =0
HoursMSB =0
BCDSeconds =0
BCDMinutes =0
BCDHours =0
ControlSeconds var byte
low DS1302_RST ' Set reset pin low
low DS1302_CLK ' Set clock pin low
gosub setclock
'--------- intro 1-2-3 --------------------------------------------------------
xx=0
gosub DisplayNumber
pause 500
xx=1
gosub DisplayNumber
pause 500
xx=2
gosub DisplayNumber
pause 500
xx=3
gosub DisplayNumber
pause 500
'------main prog ---------------------------------------------------------------
mainprog:
gosub gettime
xx=SecondsLSB
gosub DisplayNumber
pause 50
goto mainprog
'-------------------------------------------------------------------------------
Setclock:
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 = $00
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
return
'-----creates xDec from xBCD --------------------------------------------------
BCD_to_Decimal:
SecondsMSB=BCDSeconds & $70
SecondsMSB=SecondsMSB>>4
SecondsLSB=BCDSeconds & $0F '
Seconds=(SecondsMSB*10)+SecondsLSB 'combines DECMinutes MSB and LSB
MinutesMSB=BCDMinutes & $70
MinutesMSB=MinutesMSB>>4
MinutesLSB=BCDMinutes & $0F '
Minutes=(MinutesMSB*10)+MinutesLSB 'combines DECMinutes MSB and LSB
HoursMSB=BCDHours & $70
HoursMSB=HoursMSB>>4
HoursLSB=BCDHours & $0F
Hours=(HoursMSB*10)+HoursLSB 'combines DECMinutes MSB and LSB
RETURN
'---------------------------------------- Get Time --------------------------
GetTime:
reg_adr = readsec ' Read seconds
gosub w_in
BCDSeconds = mem
reg_adr = readmin ' Read minutes
gosub w_in
BCDMinutes = mem
reg_adr = readhour ' Read Hours
gosub w_in
BCDHours = mem
gosub BCD_to_Decimal 'split and convert to Decimal
Return
'----------------------- Time Commands Subroutines --------------------------
w_in:
mem = reg_adr ' Set mem variable to reg_adr contents
high DS1302_RST ' Activate the DS1302
shiftout DS1302_DQ,DS1302_CLK,0, [mem] ' Send control byte
shiftin DS1302_DQ,DS1302_CLK,1, [mem] ' Retrieve data in from the DS1302
low DS1302_RST ' Deactivate DS1302
return
w_out:
mem = reg_adr ' Set mem variable to reg_adr contents
high DS1302_RST ' Activate the DS1302
shiftout DS1302_DQ,DS1302_CLK,0, [mem] ' Send control byte
mem = outbyte ' Set mem variable to outbyte contents
shiftout DS1302_DQ,DS1302_CLK,0, [mem] ' Send data stored in mem variable to DS1302
low DS1302_RST ' Deactivate DS1302
return
'------------sub routines------------------------
DisplayNumber:
IF (xx = 0) then gosub NUMBER0
IF (xx = 1) then gosub NUMBER1
IF (xx = 2) then gosub NUMBER2
IF (xx = 3) then gosub NUMBER3
IF (xx = 4) then gosub NUMBER4
IF (xx = 5) then gosub NUMBER5
IF (xx = 6) then gosub NUMBER6
IF (xx = 7) then gosub NUMBER7
IF (xx = 8) then gosub NUMBER8
IF (xx = 9) then gosub NUMBER9
return
BLANK:
HIGH D
LOW C
HIGH B
LOW A
return
NUMBER0:
LOW D
LOW C
LOW B
LOW A
return
NUMBER1:
LOW D
LOW C
LOW B
HIGH A
return
NUMBER2:
LOW D
LOW C
HIGH B
LOW A
return
NUMBER3:
LOW D
LOW C
HIGH B
HIGH A
return
NUMBER4:
LOW D
HIGH C
LOW B
LOW A
return
NUMBER5:
LOW D
HIGH C
LOW B
HIGH A
return
NUMBER6:
LOW D
HIGH C
HIGH B
LOW A
return
NUMBER7:
LOW D
HIGH C
HIGH B
HIGH A
return
NUMBER8:
HIGH D
LOW C
LOW B
LOW A
return
NUMBER9:
HIGH D
LOW C
LOW B
HIGH A
return
END
Bookmarks