Code:
'----------------[ Define special DEFINES ]--------------
' These I2C settings must be defined in the Main Program
'DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
'DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication
'--------------[ Declare Variables, Aliases & Constants ]----------------
' Global variables, constants & aliases used in both Main program and
' INCLUDE files that are declared in Main files are commented out here
'Alarm1 VAR PORTB.2 ' Alarm1 input from DS1337 INTA (pin-3)
'Alarm2 VAR PORTB.3 ' Alarm2 input from DS1337 INTB (pin-7)
IN2_ext_pwr VAR PORTC.6 ' Alias to control power to Ultrasonic sensor
rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
rng1 VAR w0.byte1 ' MSB of range measurement when right justified
IN2_SCL VAR PORTB.1 ' I2C clock pin
IN2_SDA VAR PORTB.0 ' I2C data pin
w0 var word ' W0 is the word value to store the range data
'rtcdevice CON $D0 ' Device address for DS1337 Real-time-clock (RTC)
srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder
'Other variables in DS1337 setup below that DON'T have IN2_ prefix
' Local variables & constants only used in this Include file
' Other variables in DS1337 setup below that have IN2_ prefix
'---------------[ Initialize this INCLUDE module ]-----------------------
'---------------SETUP FOR USING DS1337 Real Time Clock-------------------
' Setup Hardware for uart...already done by DS1337_Setup
'DEFINE HSER_BAUD 115200
'DEFINE HSER_RCSTA 90h
'DEFINE HSER_TXSTA 24h
'DEFINE HSER_CLROERR 1
' Alias of Clock register addresses
IN2_ContReg CON $0E ' CONTROL register address
IN2_StatusReg CON $0F ' STATUS register address
'RTC_INT_FLG VAR INTCON3.1 'Alias for RB2 INTA interrupt flag from RTC
' Initialize Hardware
' ===================
'Set registers
'OSCCON.7 = 0 ' Clear IDLEN bit to allow SLEEP mode
TRISA = 0 ' PORTA reserved as outputs for LCD use
TRISB =%00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
TRISC = 0 ' RC0 used for test LED
GOTO Alarm ' Jump over Subroutines, so they don't try to execute
'-------------------[ Begin Alarm Interrupt Handler ]--------------------
Alarm:
High PORTC.0 ' Turn on Test LED to indicate Alarm interrupt entered
HIGH IN2_ext_pwr ' Turn on power to the SRF02 during interrupt service
' in order to use the I2C bus without distortion.
PAUSE 3000 ' Delay to let I2C bus stabilize after SRF02 turn on
'DEFINE WRITE_USED 1 'Required if only writing WORD variables in v2.6
' Process the RTC alarm
' Read the current time from the DS1337
I2CREAD IN2_SDA,IN2_SCL,$D0,SecReg,[sec,MINs,hr,day,date,mon,yr]
PAUSE 10
'Write time of latest alarm to EEPROM
WRITE 21,hr
PAUSE 20
WRITE 22,MINs
PAUSE 20
WRITE 23,sec
Pause 20
' Make SRF02 Range Measurement
I2CWRITE IN2_SDA,IN2_SCL,$E0,0,[80]' Request start of ranging in inches
pause 100 ' Wait for ranging to finish
I2CREAD IN2_SDA,IN2_SCL,$E0,2,[rng1,rng0] ' Get ranging results
pause 10
WRITE 253,rng1 ' Write range to EEPROM
PAUSE 20
Write 254,rng0
PAUSE 20
' Read the ALARM settings from the DS1337
I2CREAD IN2_SDA, IN2_SCL, $D0, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CRead IN2_SDA, IN2_SCL, $D0, Alm2min,[A2MINs,A2hr,A2day]
PAUSE 10
' Clear the Alarm flags to be ready for next day's interrupt
' Clear STATUS register
I2CWrite IN2_SDA,IN2_SCL,$D0,IN2_StatusReg,[$00]
Pause 1000
WRITE 255, MINs
PAUSE 20
LOW IN2_ext_pwr ' Turn off power to the SRF02 at end of interrupt
' This statement will distort the I2C bus and ext_pwr
' must be HIGH before any attempts are made to use the
' I2C bus.
PAUSE 1000 ' Delay to permit I2C bus to stabilize after SRF02 off
RETURN
' Resume Main Program
'------------------{ End of Alarm Interrupt Handler }--------------------
Bookmarks