Let me elucidate (big word for a Friday afternoon!). When I posted point 5 saying not to use a constant in the I2C ADDRESS, I also intended to mean NOT to use a constant in the RTC Register ADDRESS as well. Sometimes it works, sometimes it doesn't. It'll be the subject of an FAQ shortly. However, the PBP manual in the I2CWrite section although mentioning this, isn't too clear either. This is the main problem you've been having.
Don't jump up and down for joy just yet, there's other problems in your code... you're still not using CPin, DPin and RTC consistantly throughout your code (by not being consistant it'll bite you one day). Also... where's the square brackets in your I2Cwrite statements?
I appended some sample code for you to cut, paste, compile, marvel and wonder at. It's basically your own code, rehashed and born again. Do I need any thanks? Of course not... just that big Diamond and Sapphire cluster from Macey's window.
Code:
'
' Hardware Pin Defines
' --------------------
CPin var PortA.1 ' I2CLock
DPin var PortA.0 ' I2CData
Tick VAR PORTA.2
'
' RAM Assignments and Variables
' -----------------------------
RTC var BYTE ' RTC Hardware Address
RTCData var BYTE ' RTC Data
RTCRegister var BYTE ' RTC Register Address
Temp var BYTE ' Temporary variable (used in BCD conversions)
'
' Start Program
' =============
'
' Initialise Processor
' --------------------
TRISA=%00000001
TRISB=%11001111
TRISC=%00000000
ADCON0=%11000000
ADCON1=%00000111
RTC = %11010000 ' RTC Hardware Address ($D0)
Pause 750 ' Pause for external LCD to settle
' I saw in one of the responses to a similar problem, someone reset, set and reset again
' the CE bit. The lines below are an attempt at doing this... Cobblers! - see Note below - Melanie
' Cobblers! - A quaint olde English expression
' meaning a group of people that make or mend shoes
'
' Initialise RTC to starting Date & Time 23rd July 2004, 15:10:00
' ---------------------------------------------------------------
RTCData=4:RTCRegister=6:Gosub WriteRTC
' Write Year
RTCData=7:RTCRegister=5:Gosub WriteRTC
' Write Month
RTCData=23:RTCRegister=4:Gosub WriteRTC
' Write Days
RTCData=15:RTCRegister=2:Gosub WriteRTC
' write Hours
RTCData=10:RTCRegister=1:Gosub WriteRTC
' write Minutes
RTCData=0:RTCRegister=0:Gosub WriteRTC
' write Seconds & activate RTC
RTCData=%10010000:RTCRegister=7:Gosub WriteRTC
' write Control Register
RTCData=6:RTCRegister=$11:Gosub WriteRTC
' This is your Test write to RAM
'
' Main Program Loop
' -----------------
Loop:
IF tick = 0 Then
SerOut PORTD.2,4, $FE,$83," " 'locate cursor 1st line position 3
Else
SerOut PORTD.2,4, $FE,$83,":" 'locate cursor 1st line position 3
EndIF
'
' Your Debug Display
' ------------------
RTCRegister=$11:Gosub ReadRTC
SerOut PORTD.2,4, $FE,$C4,#RTCData 'locate cursor & Display Data
'
' New Section - Display rolling time at Cursor
' --------------------------------------------
RTCRegister=2:Gosub ReadRTC ' Get and display Hours
SerOut PORTD.2,4, $FE,$84,DEC2 RTCData
RTCRegister=1:Gosub ReadRTC ' Get and display Minutes
SerOut PORTD.2,4, ":",DEC2 RTCData
RTCRegister=0:Gosub ReadRTC ' Get and display Seconds
SerOut PORTD.2,4, ":",DEC2 RTCData
Goto Loop ' Do it forever
'
' Subroutine Reads Data from RTC converting from BCD where required
' -----------------------------------------------------------------
' RTCData is always in NUMERIC format on exit from this subroutine
' it is NEVER in BCD format.
ReadRTC:
I2CRead DPin,CPin,RTC,RTCRegister,[RTCData]
If RTCRegister < 7 then ' Convert from BCD to Numeric
Temp=RTCData & $7F ' Mask out Bit 7 in all cases
Temp=Temp >> 4
RTCData=(RTCData & $0F)+(Temp*10)
endif
Return
'
' Subroutine Writes Data to RTC converting to BCD where required
' --------------------------------------------------------------
' Caution: RTCData may be converted to BCD on exit from this subroutine
' if the Register address is less than 7.
WriteRTC:
If RTCRegister < 7 then ' Convert from Numeric to BCD
Temp=RTCData DIG 1
Temp=Temp << 4
RTCData=(RTCData DIG 0)+Temp
endif
I2Cwrite DPin,CPin,RTC,RTCRegister,[RTCData]
Pause 10
Return
End
PS. Count the number of Pause statements...
*smiles*
Melanie
Bookmarks