'/////////////////////////
'//
'// Using the PIC16F886
'// AM PM mode
'// Increment Hour and Minutes
'// Output to LCD 2x16
'//
'/////////////////////////
'/////////////////////////
'// Define section //
'/////////////////////////
INCLUDE "modedefs.bas"
Define OSC 8
CM1CON0.7 = 0
CM2CON0.7 = 0
DEFINE BUTTON_PAUSE 50
'////////////////// HPWM /////////////////////////
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2
Pause 20
'/////////////////////////
'// LCD configuration //
'/////////////////////////
DEFINE LCD_DREG PORTA ' Set LCD Data port
DEFINE LCD_DBIT 0 ' 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 7 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 6 ' 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
'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////
TrisB= 0
TrisA= 0
TrisC=%1110000
SDApin var PORTC.4 ' RTC data
SCLpin var PORTC.3 ' RTC clock
tmp var byte
pkt var byte[11]
rtcAMPM var bit ' Storage flag to indicate AM or PM ; 0=AM, 1=PM
hourmode var bit ' Storage flag for RTC 12/24 hour mode ; 0=24Hr, 1=12Hr
BCDResult var byte ' Storage for temporary result for BCD Conversions
RTCHour var byte
RTCMin var byte
RTCSec var byte
'The DS1337 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12- or 24-
'hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with
'logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20–23 hours).
'The only time you should need to worry about bit 5 is when bit 6 = 1 (12 hour mode).
ansel=0 'dig i/o
' CMCON0=7 ' compare off
trisc=%1110000
'i2cwrite sdapin,sclpin,$D0,0,[$0,$14,$18,3,$14,1,$14] ' TIME AND DATE
pause 10
main:
Gosub gethour
if PortC.5 & PortC.6 then gosub ampm
if PortC.6 then gosub ADDHour
if PortC.5 then gosub ADDMin
goto main
end
Gethour:
i2cREAD sdapin,sclpin,$D0,0,[str pkt \3 ]
RTCHour = pkt[2]
if RTCHour.6 then ' this is HIGH, therfore 12H mode
lcdout $fe,1,"Time=", hex2 (pkt[2]& %00011111),":",hex2 pkt[1],":",hex2 pkt[0] 'bit 0=sec, bit 1=min, bit 2=hrs
if RTCHour.5 then
lcdout $fe,$c0, "pm:"' ,bin RTCHOUR.6,":", Bin RTCHOUR.5,":", bin RTCHOUR
else
lcdout $fe,$c0, "am:"' ,bin RTCHOUR.6,":", Bin RTCHOUR.5,":", bin RTCHOUR
endif
else
lcdout $fe,1,"Time2=", hex2 pkt[2],":",hex2 pkt[1],":",hex2 pkt[0]
' lcdout $fe,$c0, " :" ,bin RTCHOUR.6,":", Bin RTCHOUR.5,":", bin RTCHOUR
endif
pause 100
return
ampm:
pause 500
if PortC.5 & PortC.6 then
I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]
hourmode = RTCHour.6
if hourmode = 1 then ' Is the RTC set for 12-hour mode
rtcAMPM = RTCHour.5 ' Assign AM/PM
RTCHour = RTCHour & %00011111 ' Bits 4-0 = Hours 0-12
endif
BCDResult = RTCHour
gosub Bcd2Dec
if hourmode = 1 then '12Hr Convert to 24Hr
hourmode = 0 ' change to 24Hr
if rtcAMPM = 1 then 'PM
BCDResult = BCDResult + 12 'PM
endif
else '24Hr Convert to 12Hr
hourmode = 1
if BCDResult > 12 then
BCDResult = BCDResult - 12
rtcAMPM = 1
else
rtcAMPM = 0
endif
endif
gosub Dec2Bcd
RTCHour = BCDResult
RTCHour.6 = hourmode
RTCHour.5 = rtcAMPM
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
pause 10
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour] ' Need to write twice because you changed the mode bit
pause 10
endif
return
Bcd2Dec:
' Subroutine to convert BCD (2 hex digits) to Decimal
' Make sure to set BCDResult with parameter value before calling this sub
' Result is stored in BCDResult
BCDResult = (((BCDResult >> 4) * 10) + (BCDResult & $0f))
return
Dec2Bcd:
' Subroutine to convert Decimal to BCD (2 hex digits)
' Make sure to set BCDResult with parameter value before calling this sub
' Result is stored in BCDResult
BCDResult = (((BCDResult / 10) << 4) | (BCDResult // 10))
return
ADDHour: 'increment hours
pause 200
if PortC.5=1 then return
if PortC.6=1 then
i2cREAD sdapin,sclpin,$D0,0,[str pkt \3 ]
RTCHour = pkt[2]
hourmode = RTCHour.6
rtcAMPM = RTCHour.5
'////////Selection Begins here
if hourmode = 0 then ' this is 24H mode 0H to 23H and bit 5 is used to calculate hours
RTCHour = RTCHour & $3F ' keeping the bit 5
BCDResult = RTCHour
gosub Bcd2Dec
BCDResult = BCDResult + 1
if BCDResult > 23 then
BCDResult = 0
' hourmode = 1
rtcAMPM=0
endif
if BCDResult > 19 then ' bit 5 is needed for the hours 20h to 23h
rtcAMPM=1
endif
gosub Dec2Bcd
endif
RTCHour = BCDResult
RTCHour.6 = hourmode
RTCHour.5 = rtcAMPM
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
pause 10
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour] ' Need to write twice because you changed the mode bit
pause 10
endif
if hourmode = 1 then ' this is 12H mode 0H to 11H
pause 100
RTCHour = RTCHour & %00011111 ' keeping the bit 5
BCDResult = RTCHour
gosub Bcd2Dec
BCDResult = BCDResult + 1
if BCDResult > 12 then
BCDResult = 1
rtcAMPM = not rtcAMPM ' 0 is for the AM mode
endif
gosub Dec2Bcd
RTCHour = BCDResult
RTCHour.6 = hourmode
RTCHour.5 = rtcAMPM
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
pause 10
I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour] ' Need to write twice because you changed the mode bit
pause 10
endif
return
ADDMin: ' increment a minutes
pause 200
if PortC.6=1 then return
if PortC.5=1 then
' I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]
I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]
BCDResult = RTCMin
gosub Bcd2Dec
BCDResult = BCDResult + 1
if BCDResult > 59 then BCDResult = 0
Pause 2
gosub Dec2Bcd
RTCMin = BCDResult
I2cwrite SDApin,SCLpin,$D0,$01,[RTCMin]
endif
return
Bookmarks