Code:
Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
DEFINE OSC 48
;--- if you un-comment these, you must comment the ones in the .inc file--
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
'**--SETUP FOR USING 2x16 LCD THAT IS INSTALLED IN EASYPIC6----------
'( commented out during testing of DS1337 operations)
'********************************************************************
' LCD DEFINES FOR USE WIH 2x16 LCD..commented out during testing
' DEFINE LCD Control Constants **
'====================================
'I CON 254 ' Control Byte
'Clr CON 1 ' Clear the display
'Line1 CON 128 ' Point to beginning of line 1
'Line2 CON 192 ' Point to beginning of line 2
'**-------------SETUP FOR USING DS1337 Real Time Clock------------------
' Setup Hardware for uart
' ==================
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1
' Aliased Variables for CLock
' ====================
Alarm1 VAR PORTB.4 ' Output of Alarm1 signal
Alarm2 VAR PORTB.5 ' Output of Alarm2 signal
DS_SCL VAR PORTB.1 ' I2C clock pin
DS_SDA VAR PORTB.0 ' I2C data pin
RTC CON %11010000 ' RTC device write address(byte addressing)
'contrl CON %00011001 ' Sets RS1 & RS2,
' clears INTCN to enable Alarm1 & 3.2768kHz
' outputs separately.
' -------------- RTC Address definitions
SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)
'Alarm 1 Address definitions
'=================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)
'Alarm 2 Address definitions
'==================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
' Alias of Clock register addresses
'========================
ContReg CON $0E ' CONTROL register address
StatusReg CON $0F ' STATUS register address
' Clock Variables
'=========
sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year
'ALARM1 VARIABLES
'==============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day
'ALARM2 VARIABLES
'==============
A2MINs VAR BYTE ' minutes
A2hr VAR BYTE ' hours
A2day VAR BYTE ' day
' Initialize Hardware
' -------------------
'Set registers
TRISA =%00000000
TRISB =%00000000
TRISC =%00000000
TRISD =%00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
INTCON2.7 = 0
' Initialize Display ' commented out during DS1337 testing
'Pause 1000 ' Delay to let LCD start up
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," RTC TEST " ' Display "RTC TEST" on 1st line
'Pause 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'PAUSE 1000
GoSub SetTimeAndDate
' Main Program Loop
' -----------------
Main:
' Read the current time and ALARM settings from the DS1337
I2CRead DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
Pause 20
I2CREAD DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 20
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day] ' No Alarm2 during test
'PAUSE 20
' Display Time and ALARM2 settings on the LCD display 'No LCD during test
GoTo Main
End ' Insure program stops if reaches here
' START LIST OF SUBROUTINES
'***********************
'DisplayTest: 'Commented out during test of DS1337 code
'============
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," TEST " ' Display "TEST" on 1st line
'PAUSE 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'Pause 1000
'RETURN
SetTimeAndDate:
'============
' Initialize clock variables to 0
yr=0:date =0:mon =0:day=0:hr=0:MINs=0:sec=0
A1sec=0:A1MINs=0:A1hr=0:A1Day=0
A2MINs=0:A2hr=0:A2day=0
' The BCD constants below set the RTC to: 19:20:30 on 06-02-2008
yr=$08
mon=$06
date=$02
day=$01
hr=$19
MINs=$20
sec=$30
'Define ALARM1 FOR 19:21:00 & to alarm when hours, minutes, & secs match.
' This requires AIM1=AIM2=AIM3=0 and AIM4=1
' (A1xxx + $80 or A1xxx + 128d sets bit 7 in AIM1,A1M2,A1M3,A1M4)
A1hr = $19 ' for AIM1 = 0
'Bit6 must be 0 for 24 hour clock
A1MINs = $21 'for AIM2 = 0
A1sec = $00 'for AIM3 = 0
A1day = $01 + $80 '$01 + $80 = $81 = 129d = $10000001 for AIM4 = 1
'DY/_DT Bit6 = 0 for using Day of month alarm
'Define ALARM2 FOR 19:22:00 'Commented out during DS1337 testing of 1Hz
'A2hr=$19
'A2MINs=$22
'A2day=$01 'DY/_DT Bit6 = 0 for using Day of month alarm
'Set the main Time
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
Pause 20
'Set the Alarm1 Time
I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 20
'Set the Alarm2 Time ' Alarm2 Commented out during testing
'I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day,contrl]
'Pause 20
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
' Clear STATUS and set CONTROl registers
I2CWrite DS_SDA,DS_SCL,RTC,StatusReg,[$00] ' Clear STATUS register
PAUSE 20
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00011001]' Starts the oscillator, sets the SQW/OUT, enables INTA
Pause 20
Return
Bookmarks