Code:
Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
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
'=============================
'DEFINE LCD_DREG PORTB ' Use PortB for LCD Data
'DEFINE LCD_DBIT 0 ' Use lower(4) 4 bits of PortB
' PORTB.0 thru PORTB.3 connects to
' LCD DB4 thru LCD DB-7 respectively
'DEFINE LCD_RSREG PORTB ' PORTB for RegisterSelect (RS) bit
'DEFINE LCD_RSBIT 4 ' PORTB.4 pin for LCD's RS line
'DEFINE LCD_EREG PORTB ' PORTB for Enable (E) bit
'DEFINE LCD_EBIT 5 ' PORTB.5 pin for LCD's E line
'DEFINE LCD_BITS 4 ' Using 4-bit bus
'DEFINE LCD_LINES 2 ' Using 2 line Display
'DEFINE LCD_COMMANDUS 5000 ' Command Delay (uS)
'DEFINE LCD_DATAUS 200 ' Data Delay (uS)
' 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 %00000000 ' Starts oscillar, sets the SQW/OUT to
' 1Hz pulse, no Alarm interrupt enabled.
'contrl CON %00000001 ' Starts oscillator, sets the SQW/OUT to
' 1 HZ pulse, & enables INTA interrupt from
' A1F.
' w/ logic level low (see data sheet pg 10)
'contrl CON %00000010 ' Starts oscillator, sets the SQW/OUT to
' 1 HZ pulse, enables INTA interrupt from
' A2F.
contrl CON %00000011 ' Starts the oscillator, sets the SQW/OUT
' to 1 Hz, enables INTA interrupt from A2F
' or INTA interrupt from A1F.
'contrl CON %000xx100 ' Starts oscillator, no interrupts enabled.
'contrl CON %000xx101 ' Starts oscillator, enables INTA interrupt
' from A1F.
'contrl CON %000xx110 ' Starts oscillator, enables INTB interrupt
' from A2F.
'contrl CON %00000111 ' Starts oscillator, enables INTA interrupt
' from A1F or INTB from A2F.
' -------------- 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
' Display functions commented out due to RB0 & RB1 conflict with I2C
'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 10
I2CREAD DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
pause 10
' Display Time and ALARM2 settings
'LCDOut I,Clr:Pause 50
'LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
'PAUSE 500
'LCDOut I,Line2,"Alarm: ",HEX2 A2hr, ":", HEX2 A2MINs, ":", HEX2 $0
'Pause 500 ' Allow Time for user to view
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00] ' Clear STATUS register
PAUSE 10
GoTo Main
End
' 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 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 constants below set the RTC to: 16:30:00 on 11-28-2009
yr=$09
mon=$11
date=$28
day=$02
hr=$16
mins=$30
sec=$00
'Define ALARM1 FOR 16:31:00 & to alarm once per second
' (A1xxx + $80 or A1xxx + 128d sets bit 7 in AIM1,A1M2,A1M3,A1M4 for 1Hz)
A1hr = $16 + $80 '$16 + $80 = $96 = 150d = %10010110
'Bit6 must be 0 for 24 hour clock
A1MINs = $31 + $80 '$31 + $80 = $B1 = 177d = %10110001
A1sec = $00 + $80 '$00 + $80 = $80 = 128d = $10000000
A1day = $28 + $80 '$28 + $80 = $A8 = 168d = $10101000
'DY/_DT Bit6 = 0 for using Day of month alarm
'Define ALARM2 FOR 16:32:00 'Commented out during DS1337 testing of 1Hz
'A2hr=$16
'A2MINs=$32
'A2day=$28 '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 10
'Set the Alarm 1 Time
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00000001] 'Clears INTCN, RS1
'& RS2 to enable 1Hz pulse &
' INTA interrupt.
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
' Sets Alarm1 once per second
Pause 10
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
Pause 10
'Set the Alarm 2 Time ' Commented out during test of DS1337 1Hz
'I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day,contrl]
'Pause 10
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
' Sets Alarm2 once per second
'A2MINs.7 =1 for A2M2 = 1
'A2hr.7 =1 for A2M3 = 1
'A2day.7 =1 for A2M4 = 1
'Set DT/DT bit 6 of alarm reg to date by writting 0 to it
I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC,Alm2Day ,[$01]
Pause 10
'I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[contrl]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00] ' Clear STATUS register
PAUSE 10
Return
Bookmarks