por menegola » 09 Set 2011 16:52
Ola pessoal.
Baxie si site da microchip firmware na compilação inumeros erros.
Alguem poderia me explicar?
Uso MPLAB 8,66
;**********************************************************************
; *
; Filename: x10hc.asm *
; Date: 10/15/02 *
; File Version: 1.00 *
; *
; Author: Jon Burroughs *
; Company: Microchip Technology *
; *
; *
;**********************************************************************
; Software License Agreement *
; *
; The software supplied herewith by Microchip Technology *
; Incorporated (the "Company") for its PICmicro® Microcontroller is *
; intended and supplied to you, the Company’s customer, for use *
; solely and exclusively on Microchip PICmicro Microcontroller *
; products. The software is owned by the Company and/or its *
; supplier, and is protected under applicable copyright laws. All *
; rights are reserved. Any use in violation of the foregoing *
; restrictions may subject the user to criminal sanctions under *
; applicable laws, as well as to civil liability for the breach of *
; the terms and conditions of this license. *
; *
; THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, *
; WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED *
; TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
; PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, *
; IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR *
; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. *
; *
;**********************************************************************
; *
; Files required: *
; 16F877.lkr or 16F877a.lkr *
; p16F877.inc or p16F877a.inc *
; *
; lcd.asm *
; x10lib.asm *
; x10lib.inc *
; *
;**********************************************************************
; *
; Notes: This is the main file for operating the PIC *
; Home Controller X-10 demo application. *
; *
;**********************************************************************
list p=16F877 ; list directive to define processor
;list p=16F877a ; or this if using the 16F877a
#include <p16F877.inc> ; processor specific variable definitions
;#include <p16F877a.inc> ; or this if using the 16F877A
__CONFIG _BODEN_OFF & _WRT_ENABLE_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC & _LVP_OFF & _CP_OFF & _CPD_OFF & _DEBUG_OFF
errorlevel -302 ; suppress "register not in bank0, check page bits" message.
;**********************************************************************
; Include files listed below: *
;**********************************************************************
#include "x10lib.inc" ;X10 constant definitions
;**********************************************************************
; Define external functions *
;**********************************************************************
extern LCDINIT
extern LCDCLEAR
extern LCDPUTCHAR
extern LCDPUTCMD
extern L1homeLCD
extern L2homeLCD
;**********************************************************************
; X-10 board signal/pin definitions *
;**********************************************************************
#define CDS PORTA, 0 ;light sensor analog input
#define DB0 PORTD, 0 ;data lines for LCD
#define DB1 PORTD, 1
#define DB2 PORTD, 2
#define DB3 PORTD, 3
#define DB4 PORTD, 4
#define DB5 PORTD, 5
#define DB6 PORTD, 6
#define DB7 PORTD, 7
#define RS PORTE, 0 ;control lines for LCD
#define RW PORTE, 1
#define E PORTE, 2
#define RTS PORTC, 4 ;serial interface (this is provision only)
#define CTS PORTC, 5
#define TXD PORTC, 6
#define RXD PORTC, 7
#define TRIAC TRISA, 5 ;clear bit to turn on triac
; (PORTA bit 5 must be set)
;----------------------------------------------------------------------
;Data EEPROM Storage Locations
;----------------------------------------------------------------------
EEHouse EQU 0x01
EEUnit EQU 0x02
EEOnOffHours EQU 0x10
EEOnMins EQU 0x20
EEOffMins EQU 0x30
_EEPROM code 0x2100 ;EEPROM data start location
fill 0x00, 0x100 ;initialize EEPROM locations to 0
;----------------------------------------------------------------------
;Display Messages are assigned indexes that are used in DisplayTable
;----------------------------------------------------------------------
M_Welcome EQU .0
M_SelectFunction EQU .1
M_SetSystemTime EQU .2
M_SetSystemAddr EQU .3
M_ProgramUnit EQU .4
M_SetHours EQU .5
M_SetMin EQU .6
M_SetHouse EQU .7
M_SetUnit EQU .8
M_Okay EQU .9
M_ProgramOnTime EQU .10
M_ProgramOffTime EQU .11
M_Clear EQU .12
M_TurnOn EQU .13
M_TurnOff EQU .14
M_SetLightSensor EQU .15
M_OnAtDusk EQU .16
M_OffAtDawn EQU .17
;----------------------------------------------------------------------
;Definitions for L1Update, L2Update, and PromptUpdate routines
;----------------------------------------------------------------------
L1Welcome EQU .0
L1SelectFunction EQU .1
L1SetSystemTime EQU .2
L1SetSystemAddr EQU .3
L1ProgramUnit EQU .4
L1ProgramOnTime EQU .5
L1ProgramOffTime EQU .6
L1SetLightSensor EQU .7
L2SetSystemTime EQU .0
L2SetSystemAddr EQU .1
L2ProgramUnit EQU .2
L2DisplayTime EQU .3
L2DisplaySetTime EQU .4
L2DisplayAddr EQU .5
L2SetLightSensor EQU .6
L2OnAtDusk EQU .7
L2OffAtDawn EQU .8
P_SetHrs EQU .0
P_SetMin EQU .1
P_Okay EQU .2
P_SetUnit EQU .3
P_SetHouse EQU .4
P_DisplayYN EQU .5
UP EQU .1 ;Button assignments on PORTB
DOWN EQU .2
MENU EQU .3
ENTER EQU .4
EXIT EQU .5
;**********************************************************************
; Variables *
;**********************************************************************
gprnobnk udata_shr
_WREG res 1 ; used to save WREG during interrupt
_STATUS res 1 ; used to save STATUS during interrupt
_PCLATH res 1 ; used to save PCLATH during interrupt
_FSR res 1 ; used to save FSR during interrupt
Hours res 1
Mins res 1
Secs res 1
Ticks res 1 ;Tick is 25ms
ControlFlags res 1
#define ButtonPress ControlFlags, 0
#define MinElapsedFlag ControlFlags, 1
#define CheckTimesFlag ControlFlags, 2
#define Match ControlFlags, 3
#define LightActivate ControlFlags, 4
#define CheckLightFlag ControlFlags, 5
#define DuskFlag ControlFlags, 6
#define DawnFlag ControlFlags, 7
DisplayFlags res 1
#define UpdateDisplayFlag DisplayFlags, 0
#define L1UpdateFlag DisplayFlags, 1
#define PromptUpdateFlag DisplayFlags, 3
#define Okay DisplayFlags, 4
#define PM DisplayFlags, 5
#define TempPM DisplayFlags, 6
#define X10AddressSent DisplayFlags, 7
MenuState res 1
MenuSubState res 1
L1 res 1
L2 res 1
Prompt res 1
grp0 udata
Last_PORTB res 1
Debounced res 1
Toggled res 1
LastDebounced res 1
Held res 1
TimeLimit res 1
TempA res 1 ;ISR temporary variable
TempB res 1 ;ISR temporary variable
Index1 res 1 ;general purpose Index variable
Index2 res 1 ;general purpose Index variable
TempHours res 1
TempMins res 1
BCDH res 1
BCDL res 1
House res 1
Unit res 1
TempHouse res 1
TempUnit res 1
UpCount res 1
DownCount res 1
VALUE res 1
ADDR res 1
T1SimCount res 1
On_Flags1 res 1
On_Flags2 res 1
Off_Flags1 res 1
Off_Flags2 res 1
Temp_Flags res 1
_TempHours res 1
_TempMins res 1
_TempUnit res 1
TempLevel res 1
AmbientLevel res 1
DawnCount res 1
DuskCount res 1
NextDuskHours res 1
NextDuskMins res 1
NextDawnHours res 1
NextDawnMins res 1
Dimmer res 1
;**********************************************************************
; External declarations *
;**********************************************************************
extern ZeroCrossing, InitX10Tx, DecodeHouseOrUnit, EndX10Envelope, InitX10Variables
extern X10Flags, TxHouse, TxUnit, TxFunction, RxHouse, RxUnit, RxFunction, RxHouse2, RxKey2 ;global variables
;----------------------------------------------------------------------
; Startup Code
;----------------------------------------------------------------------
STARTUP code
nop ; 0x0000 (This nop necessary for ICD operation)
goto Start ; 0x0001
nop ; 0x0002
nop ; 0x0003
;****************************************************************************************************
ISRcode code 0x0005
;****************************************************************************************************
;----------------------------------------------------------------------
; ISR-
; Interrupt Service Routine. Determine cause of interrupt and perform appropriate action.
;----------------------------------------------------------------------
; Interrupt Sources:
; TMR0: Times duration of 1ms 120kHz burst and onset of 2nd & 3rd phase bursts.
; TMR1: Time-keeping clock, also generates 1 interrupt every 25ms for keyscan.
; RBO/INT: INTF is set upon each zero-crossing,
; used to synchronize transmission and
; reception of X-10 signals (120kHz bursts).
; TMR2: Used with CCP1 to generate 120kHz PWM output,
; Also used with postscaler to generate dimmer timing increments
; of .125us for 32 interrupts/half-cycle.
;
;----------------------------------------------------------------------
ISR
;PUSH
movwf _WREG ; Save WREG
movf STATUS,W ; Save STATUS
clrf STATUS
movwf _STATUS
movf PCLATH, W
movwf _PCLATH
movf FSR, W
movwf _FSR
pagesel ZeroCrossing
btfsc INTCON, INTF ;External interrupt?
call ZeroCrossing ; Yes, then handle ZeroCrossing
pagesel ScanKeys
banksel PIR1
btfsc PIR1, TMR1IF ;Timer 1 interrupt?
call ScanKeys ;Yes, then call ScanKeys (which will also call UpdateClock)
pagesel EndX10Envelope
btfsc INTCON, T0IF ;Timer 0 interrupt?
call EndX10Envelope ;Yes, then EndX10Envelope
pagesel UpdateDisplay
btfsc UpdateDisplayFlag ;If display update flag is set,
call UpdateDisplay ; update display.
pagesel ControlX10Units
call ControlX10Units
;POP
clrf STATUS ; select bank 0
movf _FSR, W ; restore the FSR
movwf FSR
movf _PCLATH, W
movwf PCLATH
movf _STATUS, W ;Swap original status register value into W
movwf STATUS ;Restore status register from W register
swapf _WREG, F ;Swap _WREG nibbles and return to _WREG
swapf _WREG, W ;Swap _WREG to W to restore original W value
; without affecting STATUS.
retfie ;This re-enables global interrupts
;****************************************************************************************************
PROG1 code
;****************************************************************************************************
goto $ ;This is a trap to prevent runaway code execution
Start
pagesel InitButtons
call InitButtons ;initialize button inputs
pagesel InitVariables
call InitVariables ;initialize variables
pagesel InitX10Variables
call InitX10Variables
pagesel InitTimer0
call InitTimer0 ;set up Timer0
pagesel InitTimer1 ;selected when using external 32kHz osc as TMR1 clock source
call InitTimer1
pagesel InitPWM
call InitPWM ;set up PWM as 120KHz oscillator
pagesel InitADC
call InitADC ;set up analog-to-digital converter for light sensor
pagesel InitTRIAC
call InitTRIAC ;initialize TRIAC to off condition
pagesel InitZeroXDetect
call InitZeroXDetect ;set up external interrupt as zero-crossing detector
pagesel LCDINIT
call LCDINIT ;initialize the LCD
pagesel InitMainMenuState
call InitMainMenuState ;initialize the state machine to MainMenu state
bcf MinElapsedFlag
pagesel UpdateDisplay
call UpdateDisplay ;update display
banksel On_Flags1 ;clear the on and off flags for the X-10 units
clrf On_Flags1
clrf On_Flags2
clrf Off_Flags1
clrf Off_Flags2
pagesel EnableInterrupts
call EnableInterrupts ;enable interrupts
;****************************************************************************************************
;****************************************************************************************************
; Main Loop Code
; This is the main loop which continually performs the following tasks:
; -if a key press has occured, update the state
; -if no key press has occured for over a minute, return to main menu
; -if the a clock minute has elapsed, check the unit on and off times
; -if an X10 message has been received, control the TRIAC
; *****-if display update flag has been set, update display
; *****-send X-10 control messages as necessary
;
; The various control flags are set from within the Interrupt Service Routine
;****************************************************************************************************
;****************************************************************************************************
Main
pagesel UpdateState
btfsc ButtonPress ;If any key press has occurred,
call UpdateState ; update the state.
pagesel InitMainMenuState
btfsc MinElapsedFlag ;If a minute has elapsed without a key press,
call InitMainMenuState ; update the state.
pagesel CheckOnOffTimes
btfsc CheckTimesFlag ;If the check times flag is set
call CheckOnOffTimes ; check the on and off times.
banksel X10Flags
pagesel ControlTriac
btfsc X10RxFlag ;If an X10 message has been received
call ControlTriac ; control TRIAC as necessary.
pagesel Main
goto Main
;****************************************************************************************************
;ISRRoutines
;****************************************************************************************************
;----------------------------------------------------------------------
;DimmerControl
;----------------------------------------------------------------------
DimmerControl
;not implemented
return
;----------------------------------------------------------------------
;UpdateClock -
; Macro executed within ScanKeys routine.
; update TMR1H, Hours, Secs, Mins, and set the UpdateDisplayFlag.
;----------------------------------------------------------------------
UpdateClock macro ;Implemented as macro to save space on stack
movlw 0xFC ;preload Timer 1 for 25ms interrupt (see InitTimer1 for calculation)
movwf TMR1H
banksel Ticks
incf Ticks, F ;Each tick = 25ms. 40 ticks = 1 sec.
movlw .40
; movlw .15 ;to make clock run faster for testing purposes
subwf Ticks,W
btfss STATUS,Z
goto EndUpdateClock
clrf Ticks
incf Secs,F
movlw .60
subwf Secs,W
btfss STATUS,Z
goto EndUpdateClock
clrf Secs
incf Mins,F
;*******************************************
;Every time Mins get incremented, set a flag to check On/Off Times and check Light Level
bsf CheckTimesFlag
bsf CheckLightFlag
;*******************************************
movlw .60
subwf Mins,W
btfss STATUS,Z
goto EndUpdateClock
clrf Mins
incf Hours,F
movlw .12 ;If hours increments to 12
subwf Hours,W
btfss STATUS,Z ;Update PM
goto RollHours
btfsc PM ;if PM was set
goto SetAM ; set to AM
bsf PM ; otherwise, set to PM
goto RollHours
SetAM
bcf PM
RollHours
movlw .13 ;If hours increments to 13
subwf Hours,W
btfss STATUS,Z
goto EndUpdateClock
movlw .01
movwf Hours ;then make hours = 1
EndUpdateClock
;*******************************************
;Set the update display flag
bsf UpdateDisplayFlag
bcf PromptUpdateFlag
;*******************************************
endm
;----------------------------------------------------------------------
;MinuteElapsed
; Macro called from ScanKeys routine.
;
; If 1 minute elapses without a button press, set Minute Elapsed Flag (MinElapsedFlag)
;----------------------------------------------------------------------
MinuteElapsed macro ;Implemented as macro to save space on stack
btfss ButtonPress ;check to see if button has been pressed
goto CheckTimeLimit ;if not, check time limit
movf Secs, W ;if so, set new TimeLimit:
btfsc STATUS, Z ; if Seconds is 0, set new TimeLimit to 59
movlw .60
movwf TimeLimit
decf TimeLimit, F ; otherwise, set new TimeLimit to Seconds - 1
goto EndMinuteElapsed
CheckTimeLimit
movf Secs, W ;
subwf TimeLimit, W ;
btfsc STATUS,Z ;if Seconds = TimeLimit
bsf MinElapsedFlag ;set the minute elapsed flag
btfss STATUS,Z ;otherwise,
EndMinuteElapsed
bcf MinElapsedFlag ;clear the minute elapsed flag
endm
;----------------------------------------------------------------------
;ButtonToggle
; Macro called from ScanKeys routine.
; Checks to see if each button has been pressed and then released, if so
; then corresponding bit of Toggled is clear, otherwise it's set.
;
;
;----------------------------------------------------------------------
ButtonToggle macro ;Implemented as macro to save space on stack
btfsc LastDebounced, MENU ;If button was pressed
goto $+5
btfss Debounced, MENU ;and is now not pressed
goto $+3
bcf Toggled, MENU ;clear the button's Toggled bit.
goto $+2
bsf Toggled, MENU ;Otherwise, set the bit.
btfsc LastDebounced, UP ;If button was pressed
goto $+5
btfss Debounced, UP ;and is now not pressed
goto $+3
bcf Toggled, UP ;clear the button's Toggled bit.
goto $+2
bsf Toggled, UP ;Otherwise, set the bit.
btfsc LastDebounced, DOWN ;If button was pressed
goto $+5
btfss Debounced, DOWN ;and is now not pressed
goto $+3
bcf Toggled, DOWN ;clear the button's Toggled bit.
goto $+2
bsf Toggled, DOWN ;Otherwise, set the bit.
btfsc LastDebounced, ENTER ;If button was pressed
goto $+5
btfss Debounced, ENTER ;and is now not pressed
goto $+3
bcf Toggled, ENTER ;clear the button's Toggled bit.
goto $+2
bsf Toggled, ENTER ;Otherwise, set the bit.
btfsc LastDebounced, EXIT ;If button was pressed
goto $+5
btfss Debounced, EXIT ;and is now not pressed
goto $+3
bcf Toggled, EXIT ;clear the button's Toggled bit.
goto $+2
bsf Toggled, EXIT ;Otherwise, set the bit.
movf Toggled, W
sublw 0xFE ;If any button has toggled, subtracting FE results in Carry
btfsc STATUS, C
bsf ButtonPress ;if any button toggled, set flag
endm
;----------------------------------------------------------------------
;ButtonHeld
; Macro called from ScanKeys routine.
; Checks to see if a button has been held for N consecutive KeyScan calls.
; If it has, then the ButtonHeld flag for that button is cleared. The flag
; is set on the subsequent call of ButtonHeld.
;
; This routine only checks Up and Down buttons.
;
;----------------------------------------------------------------------
ButtonHeld macro ;Implemented as macro to save space on stack
banksel UpCount
bsf Held, UP
bsf Held, DOWN
btfsc Debounced, UP ;Is Up button pressed?
goto ClearUpCount ;if not, clear the held counter for the Up button
incf UpCount, F ;if so, increment held counter for Up button
movlw .10 ;and check to see if it equals 10
subwf UpCount, W
btfss STATUS, Z ;if UpCount=5,
goto DownButtonHeld
bcf Toggled, UP
bcf Held, UP ; clear the Held bit (active low)
; bsf Debounced, UP ; and set the Debounced bit (so Toggle is not detected)
; bsf Last_PORTB, UP
goto DownButtonHeld
ClearUpCount
clrf UpCount ;
DownButtonHeld
btfsc Debounced, DOWN ;Is Down button pressed?
goto ClearDownCount ;if not, clear the held counter for the Down button
incf DownCount, F ;if so, increment held counter for Down button
movlw .10 ;and check to see if it equals 10
subwf DownCount, W
btfss STATUS, Z ;if DownCount=5,
goto EndButtonHeld
bcf Toggled, DOWN
bcf Held, DOWN ; clear the Held bit (active low)
; bsf Debounced, DOWN ; and set the Debounced bit (so Toggle is not detected)
; bsf Last_PORTB, DOWN
goto EndButtonHeld
ClearDownCount
clrf DownCount
EndButtonHeld
btfss Held, UP ;if the Up flag has been clear, clear the UpCount
clrf UpCount
btfss Held, DOWN ;if the Down flag has been clear, clear the DownCount
clrf DownCount
movf Held, W
sublw 0xFE ;If any button held, subtracting FE results in Carry
btfsc STATUS, C
bsf ButtonPress ;if any button held, set flag
endm
;----------------------------------------------------------------------
;ScanKeys
; UP = Debounced_PORTB, 1
; DOWN = Debounced_PORTB, 2
; MENU = Debounced_PORTB, 3
; ENTER = Debounced_PORTB, 4
; EXIT = Debounced_PORTB, 5
; ON_OFF = Debounced_PORTB, 5
;
; Buttons are active low. If a button is low for two scans in a row,
; it is considered to be debounced.
;
; When a button that was pressed is released, it is considered to be Toggled.
; When a button is held for N iterations of ScanKeys, it is considered to be Held.
;
;----------------------------------------------------------------------
ScanKeys
banksel PIR1
bcf PIR1, TMR1IF
UpdateClock
banksel PORTB
movf PORTB, W ;read PORTB
iorwf Last_PORTB, W ;Compare with PORTB from previous key scan
;If any bit is pulled low for 2 scans in a row,
;the button associated with that bit is considered
;to be pressed.
iorlw b'11000001' ;Mask the bits that aren't buttons (make them 1's)
movwf Debounced ;Save the debounced values.
movf PORTB, W ;Save PORTB to compare in next scan.
movwf Last_PORTB
ButtonToggle ;Check to see which pressed buttons have been released
ButtonHeld ;Check to see which buttons have been held down
MinuteElapsed ;Check to see if a minute has elapsed since last button pressed
movf Debounced, W ;save debounced to last debounced for next time
movwf LastDebounced
return
;****************************************************************************************************
;INITIALIZATION ROUTINES
;****************************************************************************************************
;----------------------------------------------------------------------
; InitTimer1-
;----------------------------------------------------------------------
;Timer1 is used with an external 32kHz crystal oscillator and configured
;to generate an interrupt 1/25ms.
; TMR1 interrupt= 1/32kHz * prescaler * (FFFFh+1-preload)
; 1/32kHz * 1 * (FFFFh+1-FCE0h) = 25msec
; - Initialize Timer1 with FCE0h
; - Configure Timer1
; - Prescale 1:1
; - Enable Oscillator
; - DO NOT SYNCHRONIZE
; - External Clock Source
; - Enable Timer1
; - Clear Timer1 Overflow Interrupt Flag
; - Enable Timer1 Overflow Interrupt
;----------------------------------------------------------------------
InitTimer1
BANKSEL TMR1H ;Initialize Timer1 with FCE0h
movlw 0xFC
movwf TMR1H
movlw 0xE0
movwf TMR1L
movlw B'00001111' ;configure Timer 1
movwf T1CON
bcf PIR1,TMR1IF ;Clear Timer1 Overflow Interrupt Flag
BANKSEL PIE1
bsf PIE1,TMR1IE ;Enable Timer1 Overflow Interrupt
return
;----------------------------------------------------------------------
; InitTimer0
;----------------------------------------------------------------------
; Timer0 is used to time the duration of the 1ms 120kHz burst and the
; onset of phase2 and phase3 bursts if implemented.
; Fosc = 7.6800MHz, TMR0 clock source is instruction cycle clock, Prescaler = 64 so:
; - 1ms delay with TMR0 preload of 0xE2 (.226)
; - 1.7667ms delay with TMR0 preload of 0xCB (.203)
;----------------------------------------------------------------------
InitTimer0
banksel OPTION_REG
movlw b'10010101' ;no RBPU, internal instruction cycle clock, 1:64 prescaler
movwf OPTION_REG
bcf INTCON, T0IF ;Clear Timer1 Overflow Interrupt Flag
; bsf INTCON, T0IE ;Enable Timer0 Overflow Interrupt
return
;----------------------------------------------------------------------
; InitZeroXDetect
;----------------------------------------------------------------------
InitZeroXDetect
banksel OPTION_REG
bsf OPTION_REG, INTEDG ;interrupt on rising edge
bsf INTCON, INTE ;Enable INT External interrupt on RB0
return
;----------------------------------------------------------------------
; InitTRIAC
;----------------------------------------------------------------------
InitTRIAC
banksel TRISA
bsf TRIAC ;make TRIAC pin an input, this is off-state for triac
banksel PORTA
bsf PORTA, 5 ;set PORTA TRIAC pin high
return
;----------------------------------------------------------------------
;InitPWM
; - Output on RC2 is initially disabled
; - Initialize PWM to produce 120kHz oscillation for X-10 transmitter
; - Main oscillator is 7.680MHz, Tosc = 1/7.680MHz
; - PWM period = 1/120kHz = 8.333us
; - PWM period = [(PR2) + 1] * 4 * Tosc * (TMR2 prescale value)
; = [(0) + 1] * 4 * 1/7.680MHz * (16) = 8.333us
; - PR2 = 0, TMR2 prescaler = 16
; - 50% PWM duty cycle = 8.333us/2 = DCxB9:DCxB0 * Tosc * (TMR2 prescale value)
; DCxB9:DCxB0 = 8.333us/2/Tosc/(TMR2 prescale value) = 2
; - postscaler = 15 generates TMR2 interrupt every 15 * 8.333us= .125ms
; for approximately 32 interrupts/half-cycle for dimmer control.
;----------------------------------------------------------------------
InitPWM
banksel TRISC
bsf XOUT ;disable output
banksel CCP1CON
clrf CCP1CON ;CCP module is off
banksel TMR2
clrf TMR2 ;Clear Timer2
banksel PR2
clrf PR2 ;set the PWM period to 1/120kHZ by setting PR2 to zero
banksel CCPR1L
clrf CCPR1L ;set duty cycle to 50% by writing 2 = b'0000000010' to DCxB9:DCxB0
clrf INTCON
banksel PIE1
bcf PIE1, CCP1IE ;disable CCP1 interrupts
banksel PIR1
bcf PIR1, CCP1IF ;clear CCP1 interrupt flag
movlw b'00101100'
banksel CCP1CON
movwf CCP1CON ;configure the CCP1 module for PWM operation
movlw b'00000110'
banksel T2CON
movwf T2CON ;set the TMR2 prescaler to 16, postscaler to 15, and enable TMR2
; banksel PIR1
; bcf PIR1, T2CON ;clear Timer2 interrupt flag
; banksel PIE1
; bsf PIE1, T2CON ;enable Timer2 interrupts for dimmer control
; banksel TRISC
; bcf XOUT ;enable output
return
;----------------------------------------------------------------------
; InitADC
;----------------------------------------------------------------------
InitADC
banksel ADCON0 ;ADC conversion clock is Fosc/32
movlw b'10000001' ;Analog channel is RA0
movwf ADCON0 ;ADC is on
banksel ADCON1 ;ADC result is left justified
movlw b'00001110' ;RA0 is analog input, all other channels are digital
movwf ADCON1 ;
return
;----------------------------------------------------------------------
; ReadADC
;----------------------------------------------------------------------
ReadADC
banksel ADCON0
bsf ADCON0, GO
btfsc ADCON0, GO ;loop until conversion is complete
goto $-1
nop
movf ADRESH, W
movwf AmbientLevel
return
;----------------------------------------------------------------------
; InitVariables
;----------------------------------------------------------------------
InitVariables
banksel Secs
clrf Secs ;initialize clock variables
clrf Mins
movlw .12
movwf Hours
clrf MenuSubState ;initialize state variables
clrf MenuState
bsf UpdateDisplayFlag ;set flags to update display
bsf L1UpdateFlag
bcf PromptUpdateFlag
bcf ButtonPress ;initialize ScanKey variables
movlw 0xFF
movwf Debounced
movwf LastDebounced
movwf Toggled
movwf Held
clrf L1 ;L1 initialized to Welcome Message
movlw L2DisplayTime ;L2 initialized to display time
movwf L2
movlw EEHouse ;Initialize House address to value stored in EEPROM
pagesel ReadData
call ReadData
banksel House
movwf House
movlw .16 ;Make sure House is a value 0-15
subwf House, W
btfsc STATUS, C
clrf House
movwf TempHouse
movlw EEUnit ;Initialize Unit address to value stored in EEPROM
pagesel ReadData
call ReadData
banksel Unit
movwf Unit
;movlw .16 ;Make sure Unit is a value 0-15
;subwf Unit, W
;btfsc STATUS, C
;clrf Unit
movwf TempUnit
return
;----------------------------------------------------------------------
; EnableInterrupts
; - First, clear peripheral interrupt flags
; - Enable Peripheral Interrupts
; - Enable Global Interrupts
;----------------------------------------------------------------------
EnableInterrupts
BANKSEL PIR1
clrf PIR1
clrf PIR2
bsf INTCON, PEIE ;Enable Peripheral Interrupts
bsf INTCON, GIE ;Enable Global Interrupts
return
;----------------------------------------------------------------------
;InitButtons
;----------------------------------------------------------------------
InitButtons
BANKSEL TRISB
movlw 0xFF ;Set TRISB to all inputs
movwf TRISB
return
;****************************************************************************************************
;Triac Control Routines
;****************************************************************************************************
;----------------------------------------------------------------------
;TurnTriacOn- Turns Triac On
;----------------------------------------------------------------------
TurnTriacOn macro
banksel PORTA
bsf PORTA, 5 ;set PORTA TRIAC pin high
banksel TRISA
bcf TRIAC ;set TRIAC pin to on-state
endm
;----------------------------------------------------------------------
;TurnTriacOff- Turns Triac Off
;----------------------------------------------------------------------
TurnTriacOff macro
banksel TRISA
bsf TRIAC ;set TRIAC pin to off-state
endm
;----------------------------------------------------------------------
;ControlTriac- Checks received X10 messages and performs appropriate TRIAC action
;----------------------------------------------------------------------
ControlTriac
; bcf INTCON, GIE
ReceiveX10Message
;Was last house address received the correct house address?
movf RxHouse, W ;Check House address
xorwf House, W
pagesel EndControlTriac
btfss STATUS, Z
goto EndControlTriac ;no match
banksel X10Flags
pagesel EndControlTriac
btfss RxCommandFlag ;Was a command received?
goto EndControlTriac ; No, then done.
AllUnitsOff?
movf RxFunction, W ;AllUnitsOff?
xorlw AllUnitsOff
pagesel AllLightsOn?
btfss STATUS, Z
goto AllLightsOn?
TurnTriacOff
AllLightsOn?
movf RxFunction, W ;AllLightsOn?
xorlw AllLightsOn
pagesel AllLightsOff?
btfss STATUS, Z
goto AllLightsOff?
TurnTriacOn
AllLightsOff?
movf RxFunction, W ;AllLightsOff?
xorlw AllLightsOff
pagesel CheckUnitAddress
btfss STATUS, Z
goto CheckUnitAddress
TurnTriacOff
CheckUnitAddress
movf RxUnit, W ;Check Unit address
xorwf Unit, W
pagesel EndControlTriac
btfss STATUS, Z
goto EndControlTriac ;no match
On?
movf RxFunction, W ;On?
xorlw On
pagesel Off?
btfss STATUS, Z
goto Off?
TurnTriacOn
Off?
movf RxFunction, W ;Off?
xorlw Off
pagesel EndControlTriac
btfss STATUS, Z
goto EndControlTriac
TurnTriacOff
;Bright?
;Dim?
EndControlTriac
; bsf INTCON, GIE
return
;****************************************************************************************************
MenuNav code 0x0800
;****************************************************************************************************
goto $ ;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;UpdateState
;
;----------------------------------------------------------------------
UpdateState
movlw HIGH(UpdateState)
movwf PCLATH
;*********
; bcf INTCON, INTE ;disable X-10 service
;*********
movf MenuState, W
addwf PCL, F
goto US_MainMenuState
goto US_SelectFunctionState
goto US_SetSystemTimeState
goto US_SetSystemAddrState
goto US_ProgramUnitState
goto US_ProgramOnTimeState
goto US_ProgramOffTimeState
goto US_SetLightSensorState
US_MainMenuState
pagesel MainMenuState
goto MainMenuState
US_SelectFunctionState
pagesel SelectFunctionState
goto SelectFunctionState
US_SetSystemTimeState
pagesel SetSystemTimeState
goto SetSystemTimeState
US_SetSystemAddrState
pagesel SetSystemAddrState
goto SetSystemAddrState
US_ProgramUnitState
pagesel ProgramUnitState
goto ProgramUnitState
US_ProgramOnTimeState
pagesel ProgramOnTimeState
goto ProgramOnTimeState
US_ProgramOffTimeState
pagesel ProgramOffTimeState
goto ProgramOffTimeState
US_SetLightSensorState
pagesel SetLightSensorState
goto SetLightSensorState
EndUpdateState
bcf ButtonPress
bsf UpdateDisplayFlag
;*********
; bsf INTCON, INTE ;re-enable X-10 service
;*********
return
;----------------------------------------------------------------------
;MainMenuState
;
;----------------------------------------------------------------------
MainMenuState
movlw L2DisplayTime ;Update second line
movwf L2
banksel Toggled
btfss Toggled, MENU
goto MMS_MENU
btfss Toggled, ENTER
goto MMS_ENTER
btfss Toggled, EXIT
goto MMS_EXIT
MMS_MENU
pagesel InitSelectFunctionState
call InitSelectFunctionState
goto EndMMS
MMS_ENTER
TurnTriacOn
goto EndMMS
MMS_EXIT
TurnTriacOff
goto EndMMS
EndMMS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;SelectFunctionState
; User interface to select function.
; UP- scrolls forward through functions
; DOWN- scrolls backward through functions
; MENU- scrolls forward through functions
; ENTER- selects function
; EXIT- returns to Main Screen
;----------------------------------------------------------------------
SelectFunctionState
movlw HIGH(SelectFunctionState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_SFS0
goto call_SFS1
goto call_SFS2
goto call_SFS3
call_SFS0
pagesel SFS0
goto SFS0
call_SFS1
pagesel SFS1
goto SFS1
call_SFS2
pagesel SFS2
goto SFS2
call_SFS3
pagesel SFS3
goto SFS3
ExitSFS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndSFS
pagesel InitMainMenuState
call InitMainMenuState
EndSFS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;SFS0
;----------------------------------------------------------------------
SFS0
pagesel SFS0
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SFS0_UP
btfss Toggled, DOWN
goto SFS0_DOWN
btfss Toggled, MENU
goto SFS0_MENU
btfss Toggled, ENTER
goto SFS0_ENTER
goto EndSFS0
SFS0_UP
SFS0_MENU
movlw L2SetSystemAddr ;Update second line
movwf L2
movlw .1
movwf MenuSubState
goto EndSFS0
SFS0_DOWN
movlw L2ProgramUnit ;Update second line
movwf L2
movlw .3
movwf MenuSubState
goto EndSFS0
SFS0_ENTER
pagesel InitSetSystemTimeState
call InitSetSystemTimeState
EndSFS0
pagesel ExitSFS
goto ExitSFS
;----------------------------------------------------------------------
;SFS1
;----------------------------------------------------------------------
SFS1
pagesel SFS1
banksel Toggled
btfss Toggled, UP
goto SFS1_UP
btfss Toggled, DOWN
goto SFS1_DOWN
btfss Toggled, MENU
goto SFS1_MENU
btfss Toggled, ENTER
goto SFS1_ENTER
goto EndSFS1
SFS1_UP
SFS1_MENU
movlw L2SetLightSensor ;Update second line
movwf L2
movlw .2
movwf MenuSubState
goto EndSFS1
SFS1_DOWN
movlw L2SetSystemTime ;Update second line
movwf L2
movlw .0
movwf MenuSubState
goto EndSFS1
SFS1_ENTER
pagesel InitSetSystemAddrState
call InitSetSystemAddrState
EndSFS1
pagesel ExitSFS
goto ExitSFS
;----------------------------------------------------------------------
;SFS2
;----------------------------------------------------------------------
SFS2
pagesel SFS2
banksel Toggled
btfss Toggled, UP
goto SFS2_UP
btfss Toggled, DOWN
goto SFS2_DOWN
btfss Toggled, MENU
goto SFS2_MENU
btfss Toggled, ENTER
goto SFS2_ENTER
goto EndSFS2
SFS2_UP
SFS2_MENU
movlw L2ProgramUnit ;Update second line
movwf L2
movlw .3
movwf MenuSubState
goto EndSFS2
SFS2_DOWN
movlw L2SetSystemAddr ;Update second line
movwf L2
movlw .1
movwf MenuSubState
goto EndSFS2
SFS2_ENTER
pagesel InitSetLightSensorState
call InitSetLightSensorState
EndSFS2
pagesel ExitSFS
goto ExitSFS
;----------------------------------------------------------------------
;SFS3
;----------------------------------------------------------------------
SFS3
pagesel SFS3
banksel Toggled
btfss Toggled, UP
goto SFS3_UP
btfss Toggled, DOWN
goto SFS3_DOWN
btfss Toggled, MENU
goto SFS3_MENU
btfss Toggled, ENTER
goto SFS3_ENTER
goto EndSFS3
SFS3_UP
SFS3_MENU
movlw L2SetSystemTime ;Update second line
movwf L2
movlw .0
movwf MenuSubState
goto EndSFS3
SFS3_DOWN
movlw L2SetLightSensor ;Update second line
movwf L2
movlw .2
movwf MenuSubState
goto EndSFS3
SFS3_ENTER
pagesel InitProgramUnitState
call InitProgramUnitState
EndSFS3
pagesel ExitSFS
goto ExitSFS
;----------------------------------------------------------------------
; SetSystemTimeState:
; user interface to set system time (hours and minutes)
; SubMenuState=0, initialize variables
; SubMenuState=1, adjust system hours:
; UP- increments hours
; DOWN- decrements hours
; ENTER- goes to adjust minutes
; EXIT- returns to Select Function screen, without changing time
; SubMenuState=2, adjust system minutes:
; UP- increments minutes
; DOWN- decrements minutes
; ENTER- goes to confirm new time
; EXIT- returns to SetSystemHours screen, without changing time
; SubMenuState=3, confirm time:
; UP- select between Yes and No
; DOWN- select between Yes and No
; ENTER- if Yes is selected, new time is saved, exit to Main screen
; if No is selected, new time is not saved, exit to Main
; EXIT- returns to Select Function screen, without changing time
;
;----------------------------------------------------------------------
SetSystemTimeState
movlw HIGH(SetSystemTimeState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_SSTS0
goto call_SSTS1
goto call_SSTS2
call_SSTS0
pagesel SSTS0
goto SSTS0
call_SSTS1
pagesel SSTS1
goto SSTS1
call_SSTS2
pagesel SSTS2
goto SSTS2
ExitSSTS
pagesel EndSSTS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndSSTS
pagesel InitMainMenuState
call InitMainMenuState
EndSSTS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;SSTS0
;----------------------------------------------------------------------
SSTS0
pagesel SSTS0
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSTS0_UP
btfss Toggled, DOWN
goto SSTS0_DOWN
btfss Toggled, ENTER
goto SSTS0_ENTER
goto EndSSTS0
SSTS0_UP
pagesel AdjustHoursUp
call AdjustHoursUp
pagesel EndSSTS0
goto EndSSTS0
SSTS0_DOWN
pagesel AdjustHoursDown
call AdjustHoursDown
pagesel EndSSTS0
goto EndSSTS0
SSTS0_ENTER
movlw .1
movwf MenuSubState
movlw P_SetMin
movwf Prompt
bsf PromptUpdateFlag
EndSSTS0
pagesel ExitSSTS
goto ExitSSTS
;----------------------------------------------------------------------
;SSTS1
;----------------------------------------------------------------------
SSTS1
pagesel SSTS1
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSTS1_UP
btfss Toggled, DOWN
goto SSTS1_DOWN
btfss Toggled, ENTER
goto SSTS1_ENTER
goto EndSSTS1
SSTS1_UP
pagesel AdjustMinutesUp
call AdjustMinutesUp
pagesel EndSSTS1
goto EndSSTS1
SSTS1_DOWN
pagesel AdjustMinutesDown
call AdjustMinutesDown
pagesel EndSSTS1
goto EndSSTS1
SSTS1_ENTER
movlw .2
movwf MenuSubState
movlw P_Okay
movwf Prompt
bsf PromptUpdateFlag
EndSSTS1
pagesel ExitSSTS
goto ExitSSTS
;----------------------------------------------------------------------
;SSTS2
;----------------------------------------------------------------------
SSTS2
pagesel SSTS2
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSTS2_UP
btfss Toggled, DOWN
goto SSTS2_DOWN
btfss Toggled, ENTER
goto SSTS2_ENTER
goto EndSSTS2
SSTS2_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSSTS2
goto EndSSTS2
SSTS2_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSSTS2
goto EndSSTS2
SSTS2_ENTER
btfss Okay
goto SSTS2_notOKAY ;if Okay is not 1, then return without saving the values
movf TempHours, W
movwf Hours
movf TempMins, W
movwf Mins
clrf Secs
bcf PM
btfsc TempPM
bsf PM
SSTS2_notOKAY
pagesel InitMainMenuState
call InitMainMenuState
EndSSTS2
pagesel ExitSSTS
goto ExitSSTS
;----------------------------------------------------------------------
; SetSystemAddrState:
; user interface to set system address (house and units)
; SubMenuState=0, initialize variables
; SubMenuState=1, adjust house address:
; UP- increments house address
; DOWN- decrements house address
; ENTER- goes to adjust unit address
; EXIT- returns to Select Function screen, without changing system address
; SubMenuState=2, adjust system unit address:
; UP- increments system unit address
; DOWN- decrements system unit address
; ENTER- goes to confirm new system address
; EXIT- returns to SetSystemHouse address screen, without changing system address
; SubMenuState=3, confirm system address:
; UP- select between Yes and No
; DOWN- select between Yes and No
; ENTER- if Yes is selected, new system address is saved, exit to Main screen
; if No is selected, new system address is not saved, exit to Main
; EXIT- returns to Select Function screen, without changing system address
;
;----------------------------------------------------------------------
SetSystemAddrState
movlw HIGH(SetSystemAddrState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_SSAS0
goto call_SSAS1
goto call_SSAS2
call_SSAS0
pagesel SSAS0
goto SSAS0
call_SSAS1
pagesel SSAS1
goto SSAS1
call_SSAS2
pagesel SSAS2
goto SSAS2
ExitSSAS
pagesel EndSSAS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndSSAS
pagesel InitMainMenuState
call InitMainMenuState
EndSSAS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;SSAS0
;----------------------------------------------------------------------
SSAS0
pagesel SSAS1
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSAS0_UP
btfss Toggled, DOWN
goto SSAS0_DOWN
btfss Toggled, ENTER
goto SSAS0_ENTER
goto EndSSAS0
SSAS0_UP
pagesel AdjustHouseUp
call AdjustHouseUp
pagesel EndSSAS0
goto EndSSAS0
SSAS0_DOWN
pagesel AdjustHouseDown
call AdjustHouseDown
pagesel EndSSAS0
goto EndSSAS0
SSAS0_ENTER
movlw .1
movwf MenuSubState
movlw P_SetUnit
movwf Prompt
bsf PromptUpdateFlag
EndSSAS0
pagesel ExitSSAS
goto ExitSSAS
;----------------------------------------------------------------------
;SSAS1
;----------------------------------------------------------------------
SSAS1
pagesel SSAS1
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSAS1_UP
btfss Toggled, DOWN
goto SSAS1_DOWN
btfss Toggled, ENTER
goto SSAS1_ENTER
goto EndSSAS1
SSAS1_UP
pagesel AdjustUnitUp
call AdjustUnitUp
pagesel EndSSAS1
goto EndSSAS1
SSAS1_DOWN
pagesel AdjustUnitDown
call AdjustUnitDown
pagesel EndSSAS1
goto EndSSAS1
SSAS1_ENTER
movlw .2
movwf MenuSubState
movlw P_Okay
movwf Prompt
bsf PromptUpdateFlag
EndSSAS1
pagesel ExitSSAS
goto ExitSSAS
;----------------------------------------------------------------------
;SSAS2
;----------------------------------------------------------------------
SSAS2
pagesel SSAS2
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SSAS2_UP
btfss Toggled, DOWN
goto SSAS2_DOWN
btfss Toggled, ENTER
goto SSAS2_ENTER
goto EndSSAS2
SSAS2_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSSAS2
goto EndSSAS2
SSAS2_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSSAS2
goto EndSSAS2
SSAS2_ENTER
btfss Okay
goto SSAS2_notOKAY ;if Okay is not 1, then return without saving the values movf TempHouse, W
banksel TempHouse
movf TempHouse, W
movwf House
movwf VALUE ;Save new house address to EEPROM
movlw EEHouse
pagesel WriteData
call WriteData
banksel TempUnit
movf TempUnit, W
movwf Unit
movwf VALUE ;Save new unit address to EEPROM
movlw EEUnit
pagesel WriteData
call WriteData
SSAS2_notOKAY
pagesel InitMainMenuState
call InitMainMenuState
EndSSAS2
pagesel ExitSSAS
goto ExitSSAS
;----------------------------------------------------------------------
; ProgramUnitState:
; user interface to select unit to control
; First, select unit to program:
; UP- increments unit address
; DOWN- decrements unit address
; ENTER- goes to set on-time
; EXIT- returns to Select Function screen, without changing unit address
;----------------------------------------------------------------------
ProgramUnitState
movlw L2DisplayAddr
movwf L2
bsf UpdateDisplayFlag
bsf L1UpdateFlag
;PUS0 pagesel ProgramUnitState
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto PUS0_UP
btfss Toggled, DOWN
goto PUS0_DOWN
btfss Toggled, ENTER
goto PUS0_ENTER
btfss Toggled, EXIT
goto PUS0_EXIT
goto EndPUS0
PUS0_UP
pagesel AdjustUnitUp
call AdjustUnitUp
pagesel EndPUS0
goto EndPUS0
PUS0_DOWN
pagesel AdjustUnitDown
call AdjustUnitDown
pagesel EndPUS0
goto EndPUS0
PUS0_ENTER
pagesel InitProgramOnTimeState
call InitProgramOnTimeState
pagesel EndPUS0
goto EndPUS0
PUS0_EXIT
pagesel InitMainMenuState
call InitMainMenuState
pagesel EndPUS0
goto EndPUS0
EndPUS0
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
; ProgramOnTimeState:
; SubMenuState=0, set on-time hours:
; UP- increments hours
; DOWN- decrements hours
; ENTER- goes to set on-time minutes
; EXIT- returns to select unit screen, without changing on-time hours
; SubMenuState=1, set on-time minutes:
; UP- increments minutes
; DOWN- decrements minutes
; ENTER- goes to confirm on-time.
; EXIT- returns to select unit screen, without changing on-time minutes
; SubMenuState=2, confirm on-time:
; UP- select between Yes and No
; DOWN- select between Yes and No
; ENTER- if Yes is selected, new on-time is saved, go to set off-time minutes.
; if No is selected, new on-time is not saved, exit to Main
; EXIT- returns to Select Function screen, without changing on-time for selected unit.
;----------------------------------------------------------------------
ProgramOnTimeState
movlw HIGH(ProgramOnTimeState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_POnTS0
goto call_POnTS1
goto call_POnTS2
call_POnTS0
pagesel POnTS0
call POnTS0
pagesel ExitPOnTS
goto ExitPOnTS
call_POnTS1
pagesel POnTS1
call POnTS1
pagesel ExitPOnTS
goto ExitPOnTS
call_POnTS2
pagesel POnTS2
call POnTS2
pagesel ExitPOnTS
goto ExitPOnTS
ExitPOnTS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndPOnTS
pagesel InitProgramUnitState
call InitProgramUnitState
EndPOnTS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;POnTS0
;----------------------------------------------------------------------
POnTS0
pagesel POnTS0
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto POnTS0_UP
btfss Toggled, DOWN
goto POnTS0_DOWN
btfss Toggled, ENTER
goto POnTS0_ENTER
goto EndPOnTS0
POnTS0_UP
pagesel AdjustHoursUpThruZero
call AdjustHoursUpThruZero
pagesel EndPOnTS0
goto EndPOnTS0
POnTS0_DOWN
pagesel AdjustHoursDownThruZero
call AdjustHoursDownThruZero
pagesel EndPOnTS0
goto EndPOnTS0
POnTS0_ENTER
movlw .1
movwf MenuSubState
movlw P_SetMin
movwf Prompt
bsf PromptUpdateFlag
EndPOnTS0
return
;----------------------------------------------------------------------
;POnTS1
;----------------------------------------------------------------------
POnTS1
pagesel POnTS1
banksel Toggled
movf TempHours, F
btfsc STATUS, Z ;have hours been set to zero?
goto EndPOnTS1MinsZero ;if yes then minutes are 00 by default
;check buttons and perform appropriate action
btfss Toggled, UP
goto POnTS1_UP
btfss Toggled, DOWN
goto POnTS1_DOWN
btfss Toggled, ENTER
goto POnTS1_ENTER
goto EndPOnTS1
POnTS1_UP
pagesel AdjustMinutesUp
call AdjustMinutesUp
pagesel EndPOnTS1
goto EndPOnTS1
POnTS1_DOWN
pagesel AdjustMinutesDown
call AdjustMinutesDown
pagesel EndPOnTS1
goto EndPOnTS1
EndPOnTS1MinsZero
clrf TempMins
POnTS1_ENTER
movlw .2
movwf MenuSubState
movlw P_Okay
movwf Prompt
bsf PromptUpdateFlag
EndPOnTS1
return
;----------------------------------------------------------------------
;POnTS2
;----------------------------------------------------------------------
POnTS2
pagesel POnTS2
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto POnTS2_UP
btfss Toggled, DOWN
goto POnTS2_DOWN
btfss Toggled, ENTER
goto POnTS2_ENTER
goto EndPOnTS2
POnTS2_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndPOnTS2
goto EndPOnTS2
POnTS2_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndPOnTS2
goto EndPOnTS2
POnTS2_ENTER
btfss Okay
goto POnTS2_notOKAY ;if Okay is not 1, then return without saving the values
pagesel WriteUnitOnTime
call WriteUnitOnTime
pagesel InitProgramOffTimeState
call InitProgramOffTimeState
pagesel EndPOnTS2
goto EndPOnTS2
POnTS2_notOKAY
pagesel InitProgramOnTimeState
call InitProgramOnTimeState
EndPOnTS2
return
;----------------------------------------------------------------------
; ProgramOffTimeState:
; SubMenuState=0, set off-time hours:
; UP- increments hours
; DOWN- decrements hours
; ENTER- goes to set off-time minutes
; EXIT- returns to select unit screen, without changing off-time hours
; SubMenuState=1, set off-time minutes:
; UP- increments minutes
; DOWN- decrements minutes
; ENTER- goes to confirm off-time.
; EXIT- returns to select unit screen, without changing off-time minutes
; SubMenuState=2, confirm off-time:
; UP- select between Yes and No
; DOWN- select between Yes and No
; ENTER- if Yes is selected, new off-time is saved, go to set off-time minutes.
; if No is selected, new off-time is not saved, exit to Main
; EXIT- returns to Select Function screen, without changing off-time for selected unit.
;----------------------------------------------------------------------
ProgramOffTimeState
movlw HIGH(ProgramOffTimeState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_POffTS0
goto call_POffTS1
goto call_POffTS2
call_POffTS0
pagesel POffTS0
call POffTS0
pagesel ExitPOffTS
goto ExitPOffTS
call_POffTS1
pagesel POffTS1
call POffTS1
pagesel ExitPOffTS
goto ExitPOffTS
call_POffTS2
pagesel POffTS2
call POffTS2
pagesel ExitPOffTS
goto ExitPOffTS
ExitPOffTS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndPOffTS
bsf INTCON, INTE ;Re-enable ZeroX detect when done programming units
pagesel InitProgramUnitState
call InitProgramUnitState
EndPOffTS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;POffTS0
;----------------------------------------------------------------------
POffTS0
pagesel POffTS0
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto POffTS0_UP
btfss Toggled, DOWN
goto POffTS0_DOWN
btfss Toggled, ENTER
goto POffTS0_ENTER
goto EndPOffTS0
POffTS0_UP
pagesel AdjustHoursUpThruZero
call AdjustHoursUpThruZero
pagesel EndPOffTS0
goto EndPOffTS0
POffTS0_DOWN
pagesel AdjustHoursDownThruZero
call AdjustHoursDownThruZero
pagesel EndPOffTS0
goto EndPOffTS0
POffTS0_ENTER
movlw .1
movwf MenuSubState
movlw P_SetMin
movwf Prompt
bsf PromptUpdateFlag
EndPOffTS0
return
;----------------------------------------------------------------------
;POffTS1
;----------------------------------------------------------------------
POffTS1
pagesel POffTS1
banksel Toggled
movf TempHours, F
btfsc STATUS, Z ;have hours been set to zero?
goto EndPOffTS1MinsZero ;if yes then minutes are 00 by default
;check buttons and perform appropriate action
btfss Toggled, UP
goto POffTS1_UP
btfss Toggled, DOWN
goto POffTS1_DOWN
btfss Toggled, ENTER
goto POffTS1_ENTER
goto EndPOffTS1
POffTS1_UP
pagesel AdjustMinutesUp
call AdjustMinutesUp
pagesel EndPOffTS1
goto EndPOffTS1
POffTS1_DOWN
pagesel AdjustMinutesDown
call AdjustMinutesDown
pagesel EndPOffTS1
goto EndPOffTS1
EndPOffTS1MinsZero
clrf TempMins
POffTS1_ENTER
movlw .2
movwf MenuSubState
movlw P_Okay
movwf Prompt
bsf PromptUpdateFlag
EndPOffTS1
return
;----------------------------------------------------------------------
;POffTS2
;----------------------------------------------------------------------
POffTS2
pagesel POffTS2
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto POffTS2_UP
btfss Toggled, DOWN
goto POffTS2_DOWN
btfss Toggled, ENTER
goto POffTS2_ENTER
goto EndPOffTS2
POffTS2_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndPOffTS2
goto EndPOffTS2
POffTS2_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndPOffTS2
goto EndPOffTS2
POffTS2_ENTER
btfss Okay
goto POffTS2_notOKAY ;if Okay is not 1, then return without saving the values
pagesel WriteUnitOffTime
call WriteUnitOffTime
pagesel InitProgramUnitState
call InitProgramUnitState
pagesel EndPOffTS2
goto EndPOffTS2
POffTS2_notOKAY
pagesel InitProgramOffTimeState
call InitProgramOffTimeState
EndPOffTS2
return
;----------------------------------------------------------------------
; SetLightSensorState:
; SubMenuState=0, set threshold
; UP- increments threshold
; DOWN- decrements threshold
; ENTER- goes to confirm threshold
; EXIT- returns to select unit screen, without changing off-time hours
; SubMenuState=1, confirm threshold:
; UP- select between Yes and No
; DOWN- select between Yes and No
; ENTER- if Yes is selected, new threshold is saved
; if No is selected, new threshold is not saved, exit to Main
; EXIT- returns to Select Function screen, without changing threshold
;----------------------------------------------------------------------
SetLightSensorState
movlw HIGH(SetLightSensorState)
movwf PCLATH
movf MenuSubState, W ;Based on MenuSubState, call appropriate subroutine
addwf PCL, F
goto call_SLSS0 ;select unit
goto call_SLSS1 ;turn unit on at dusk?
goto call_SLSS2 ;turn unit off at dawn?
call_SLSS0
pagesel SLSS0
call SLSS0
pagesel ExitSLSS
goto ExitSLSS
call_SLSS1
pagesel SLSS1
call SLSS1
pagesel ExitSLSS
goto ExitSLSS
call_SLSS2
pagesel SLSS2
call SLSS2
pagesel ExitSLSS
goto ExitSLSS
ExitSLSS
banksel Toggled
btfsc Toggled, EXIT ;check exit button
goto EndSLSS
pagesel InitMainMenuState
call InitMainMenuState
EndSLSS
pagesel EndUpdateState
goto EndUpdateState
;----------------------------------------------------------------------
;SLSS0
;----------------------------------------------------------------------
SLSS0
pagesel SLSS0
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SLSS0_UP
btfss Toggled, DOWN
goto SLSS0_DOWN
btfss Toggled, ENTER
goto SLSS0_ENTER
goto EndSLSS0
SLSS0_UP
pagesel AdjustUnitUp
call AdjustUnitUp
pagesel EndSLSS0
bsf PromptUpdateFlag
goto EndSLSS0
SLSS0_DOWN
pagesel AdjustUnitDown
call AdjustUnitDown
pagesel EndSLSS0
bsf PromptUpdateFlag
goto EndSLSS0
SLSS0_ENTER
pagesel ReadUnitOnTime
call ReadUnitOnTime
bcf Okay
btfsc LightActivate
bsf Okay
movlw .1
movwf MenuSubState
movlw L2OnAtDusk
movwf L2
movlw P_DisplayYN
movwf Prompt
bsf PromptUpdateFlag
EndSLSS0
return
;----------------------------------------------------------------------
;SLSS1
;----------------------------------------------------------------------
SLSS1
pagesel SLSS1
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SLSS1_UP
btfss Toggled, DOWN
goto SLSS1_DOWN
btfss Toggled, ENTER
goto SLSS1_ENTER
goto EndSLSS1
SLSS1_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSLSS1
goto EndSLSS1
SLSS1_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSLSS1
goto EndSLSS1
SLSS1_ENTER
pagesel EnableOnAtDusk
call EnableOnAtDusk
pagesel ReadUnitOffTime
call ReadUnitOffTime
bcf Okay
btfsc LightActivate
bsf Okay
movlw .2
movwf MenuSubState
movlw L2OffAtDawn
movwf L2
movlw P_DisplayYN
movwf Prompt
bsf PromptUpdateFlag
EndSLSS1
return
;----------------------------------------------------------------------
;SLSS2
;----------------------------------------------------------------------
SLSS2
pagesel SLSS2
banksel Toggled
;check buttons and perform appropriate action
btfss Toggled, UP
goto SLSS2_UP
btfss Toggled, DOWN
goto SLSS2_DOWN
btfss Toggled, ENTER
goto SLSS2_ENTER
goto EndSLSS2
SLSS2_UP
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSLSS2
goto EndSLSS2
SLSS2_DOWN
pagesel ToggleOkay
call ToggleOkay
bsf PromptUpdateFlag
pagesel EndSLSS2
goto EndSLSS2
SLSS2_ENTER
pagesel EnableOffAtDawn
call EnableOffAtDawn
movlw .0
movwf MenuSubState
movlw L2DisplayAddr
movwf L2
movlw P_SetUnit
movwf Prompt
bsf PromptUpdateFlag
EndSLSS2
return
;----------------------------------------------------------------------
;InitMainMenuState
;----------------------------------------------------------------------
InitMainMenuState
clrf MenuState
clrf MenuSubState
; movlw L1Welcome ;Update display settings
; movwf L1
clrf L1
bsf L1UpdateFlag
movlw L2DisplayTime
movwf L2
return
;----------------------------------------------------------------------
;InitSelectFunctionState
;----------------------------------------------------------------------
InitSelectFunctionState
movlw L1SelectFunction
movwf MenuState ;MainMenuState=SelectFunction
clrf MenuSubState
movlw L1SelectFunction ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2SetSystemTime
movwf L2
return
;----------------------------------------------------------------------
;InitSetSystemTimeState
;----------------------------------------------------------------------
InitSetSystemTimeState
movlw L1SetSystemTime
movwf MenuState
clrf MenuSubState
bsf Okay ;set default Okay to Yes
movlw L1SetSystemTime ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplaySetTime
movwf L2
movlw P_SetHrs
movwf Prompt
bsf PromptUpdateFlag
banksel TempHours
movf Hours, W
movwf TempHours
movf Mins, W
movwf TempMins
bcf TempPM
btfsc PM
bsf TempPM
return
;----------------------------------------------------------------------
;InitSetSystemAddrState
;----------------------------------------------------------------------
InitSetSystemAddrState
movlw L1SetSystemAddr
movwf MenuState
clrf MenuSubState
bsf Okay ;set default Okay to Yes
movlw L1SetSystemAddr ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplayAddr
movwf L2
movlw P_SetHouse
movwf Prompt
bsf PromptUpdateFlag
banksel TempHouse
movf House, W
movwf TempHouse
movf Unit, W
movwf TempUnit
return
;----------------------------------------------------------------------
;InitProgramUnitState
;----------------------------------------------------------------------
InitProgramUnitState
movlw L1ProgramUnit
movwf MenuState
clrf MenuSubState
movlw L1ProgramUnit ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplayAddr
movwf L2
movlw P_SetUnit
movwf Prompt
bsf PromptUpdateFlag
banksel TempHouse
movf House, W
movwf TempHouse
return
;----------------------------------------------------------------------
;InitProgramOnTimeState
;----------------------------------------------------------------------
InitProgramOnTimeState
movlw L1ProgramOnTime
movwf MenuState
clrf MenuSubState
bsf Okay ;set default Okay to Yes
movlw L1ProgramOnTime ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplaySetTime
movwf L2
movlw P_SetHrs
movwf Prompt
bsf PromptUpdateFlag
pagesel ReadUnitOnTime
call ReadUnitOnTime
return
;----------------------------------------------------------------------
;InitProgramOffTimeState
;----------------------------------------------------------------------
InitProgramOffTimeState
movlw L1ProgramOffTime
movwf MenuState
clrf MenuSubState
bsf Okay ;set default Okay to Yes
movlw L1ProgramOffTime ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplaySetTime
movwf L2
movlw P_SetHrs
movwf Prompt
bsf PromptUpdateFlag
pagesel ReadUnitOffTime
call ReadUnitOffTime
return
;----------------------------------------------------------------------
;InitSetLightSensorState
;----------------------------------------------------------------------
InitSetLightSensorState
movlw L1SetLightSensor
movwf MenuState
clrf MenuSubState
bsf Okay ;set default Okay to Yes
movlw L1SetLightSensor ;Update display settings
movwf L1
bsf L1UpdateFlag
movlw L2DisplayAddr
movwf L2
movlw P_SetUnit
movwf Prompt
bsf PromptUpdateFlag
return
;---------------------------------------------------------------