Code:
DEFINE OSC 8
'ANSELH = 0
'/////////////////////////
'// LCD configuration //
'/////////////////////////
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus RB.4, RB.5, RB.6, RB.7
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000
pause 1000
DPIN VAR PORTD.3 'I2C DATA PIN
CPIN VAR PORTC.3 'I2C CLOCK PIN
' -------------- RTC definitions -----------------
RTC CON %11010000
SecReg CON $00 ' seconds Write_Addressess (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
ContReg CON $0E ' control register
cntrl CON %00000000 ' sets the SQW/OUT to 1Hz pulse, logic level low
I2CWRITE DPIN, CPIN, RTC, ContReg, [cntrl]
' The variable below holds the values entered:
' entered by the user
TempVal var byte
Temp1 var byte
Temp2 var byte
i var byte
j var word
sec VAR BYTE : mins VAR BYTE : hr VAR BYTE : day VAR BYTE :date VAR BYTE : mon VAR BYTE : yr VAR BYTE
Write_Address VAR WORD
Battery_Charge_Counter var byte
Battery_Charge_Counter = 0
Write_Address = 0
B0 var byte
B0 =0
GOSUB SET_RTC
Mainloop:
lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100
BUTTON PORTC.6,0,10,5,B0,1,Button_pressed
if PORTC.7 =0 then
if Write_Address = 0 then goto Mainloop
j = 0
For i=0 to (Write_Address / 3) -1
I2CREAD DPIN,CPIN,$A0,j,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC2 i
LCDOUT $FE,$C0,"WA ",DEC j," T:", dec2 hr, ":", dec2 mins, ":", dec2 sec
PAUSE 1000
j =j + 3
next i
Endif
GOTO Mainloop
End
Button_pressed:
GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
Write_Address = Write_Address +3
Goto Mainloop
READ_RTC:
I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
pause 10
TempVal=hr
GoSub BCD_TO_BIN
hr=TempVal
TempVal=sec
GoSub BCD_TO_BIN
sec=TempVal
TempVal=mins
GoSub BCD_TO_BIN
mins=TempVal
RETURN
SET_RTC: ' Do once at startup
yr = $3
mon = $5
date = $05
sec = $09
mins = $07
hr = $06
I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN
BCD_TO_BIN: ' Convert the BCD values into BIN
Temp1 = $0F & TempVal ' Clear off the top four bits
Temp1 = Temp1 Dig 0
Temp2 = TempVal >> 4 ' Shift down four to read 2 BCD value
Temp2 = Temp2 Dig 0
TempVal = Temp2 * 10 + Temp1
Return
Bookmarks