;M0DGQ ;QUAD LIGHTING CONTROLLER MK2 ;SWITCHES ON FOUR LIGHTS FOR A PRESET TIME ;TOGGLE FUNCTION ON BUTTONS SO LIGHT CAN BE SWITCHED OFF ;FROM SAME BUTTON BEFORE ELAPSED TIME IF NEEDED ;INCREASED DELAY IN SWITCH BOUNCE ROUTINE TO 55mS DUE TO CRAP SWITHES ; ;NOW CONTROLS FOUR LIGHTS ;**************************************************************************************************************** LIST p=16F628A ;tell assembler what chip we are using INCLUDE "P16F628A.inc" ;include the defaults for the chip __CONFIG 0x3F18 ;sets the configuration settings ;INT 4mhZ OSC ;**************************************************************************************************** CBLOCK 0x20 COUNT1 ;GENERAL PURPOSE REGISTERS FOR DELAY ROUTINES COUNT2 COUNT3 COUNT4 COUNT5 COUNT6 COUNT7 COUNT8 COUNT9 WREG_TEMP STATUS_TEMP ENDC ;************************************************************************************************* ORG 0x00 ;ORG SETS THE ORIGIN, 0x0000 FOR THE 16F628, GOTO START ;THIS IS WHERE THE PROGRAM STARTS RUNNING ORG 0x04 ;INTERUPT SERVICE ROUTINE TRIGGERED BY RB4-7 PORT CHANGE OF STATE GOTO ISR ;GOTO INTERUPT SERVICE ROUTINE ;***************************************************************************************************** #DEFINE LED_1 PORTB,0 ;LIGHT 1 OUTPUT #DEFINE LED_2 PORTB,1 ;LIGHT 2 OUTPUT #DEFINE LED_3 PORTB,2 ;LIGHT 3 OUTPUT #DEFINE LED_4 PORTB,3 ;LIGHT 4 OUTPUT #DEFINE BUT_1 PORTB,5 ;ENABLE LIGHT 1 BUTTON #DEFINE BUT_2 PORTB,6 ;ENABLE LIGHT 2 BUTTON #DEFINE BUT_3 PORTB,7 ;ENABLE LIGHT 3 BUTTON #DEFINE BUT_4 PORTB,4 ;ENABLE LIGHT 4 BUTTON ;*********************************************************************************************** START errorlevel -302, -305 ;GET RID OF ANOYING COMPILIER ERROR MESSAGE MOVLW 0x07 MOVWF CMCON ;TURN COMPARATORS OFF CLRF PORTA ;CLEAR PORT CLRF PORTB ;CLEAR PORT BANKSEL TRISA ;SELECT BANK 1 MOVLW b'11111111' ;SET PORT A MOVWF TRISA MOVLW b'11110000' ;SET PORTB BITS 7-4 INPUTS,REST OUTPUTS MOVWF TRISB BANKSEL PORTA ;SWITCH TO BANK 0 MOVLW b'10000000' MOVWF OPTION_REG ;DISABLE WEAK PULL UPS CANCEL BCF INTCON,RBIF ;CLEAR RB INTERUPT FLAG BSF INTCON,RBIE ;ENABLE RB4-7 INTERUPTS TO SENSE ANY LIGHT ENABLE BUTTONS BSF INTCON,GIE ;ENABLE GLOBAL INTERUPT ;*************************************************************************************************** MAIN ;IF ANY LEDS ARE ENABLED VIA INTERUPT THEN ENABLE TWO HOUR TIMER BTFSC LED_1 ;TEST LED_1 GOTO TIMER BTFSC LED_2 ;TEST LED_2 GOTO TIMER BTFSC LED_3 ;TEST LED_3 GOTO TIMER BTFSC LED_4 ;TEST LED_4 GOTO TIMER GOTO MAIN ;NO LEDS ENABLED SO BACK TO MAIN TIMER CALL DELAY_1 ;START TWO HOUR TIMER BCF LED_1 ;TWO HOURS ELAPSED SO CLEAR ALL LEDS BCF LED_2 BCF LED_3 BCF LED_4 GOTO MAIN ;BACK TO MAIN ;******************************************************************************************************************************************************************************************* ISR ; ISR INITATED BY CHANGE ON RB4 - RB7. BCF INTCON,GIE ;DISABLE GIE AND RBIE SO BCF INTCON,RBIE ;NO MORE RBIE INTERUPTS ;CONTEXT SAVING MOVWF WREG_TEMP ;SAVE W IN WREG_TEMP MOVF STATUS,W ;SAVE STATUS IN STATUS_TEMP BANKSEL STATUS_TEMP ;SELECT CORRECT BANK FOR STATUS REG IN ISR !!! if you do not do this status reg may not have original value restored!********* MOVWF STATUS_TEMP ;SAVE STATUS ;******************************************************************* ;TEST FOR WHICH BUTTON HAS BEEN PRESSED ;SWITCH BOUNCE ROUTINE BUTTON_1 BTFSS BUT_1 ;CHECK IF BUTTON ONE PRESSED GOTO BUTTON_2 ;NO, GOTO BUTTON_2 CALL DELAY_2 ;WAIT FOR SWITCH BOUNCE BTFSS BUT_1 ;CHECK BUTTON STATE GOTO RTN ;INVALID PRESS, GO TO RTN BTFSC BUT_1 ;VALID PRESS,HAS BUTTON BEEN RELEASED YET? GOTO $-1 ;NO CALL DELAY_2 ;YES, WAIT FOR SWITCH BOUNCE BTFSC BUT_1 ;BOUNCE OVER? GOTO $-4 ;NO ;LED ON / OFF TOGGLE FUNCTION BTFSS LED_1 ;IS LED ALREADY ON? GOTO $+3 ;NO,GO THREE INSTUCTIONS DOWN BCF LED_1 ;YES SO CLEAR IT GOTO RTN ;RTN BSF LED_1 ;SET LED_1 ;********************************************************************* ;SWITCH BOUNCE ROUTINE BUTTON_2 BTFSS BUT_2 ;CHECK IF BUTTON TWO PRESSED GOTO BUTTON_3 ;NO GOTO BUTTTON THREE CALL DELAY_2 ;WAIT FOR SWITCH BOUNCE BTFSS BUT_2 ;CHECK BUTTON STATE GOTO RTN ;INVALID PRESS, GO TO RTN BTFSC BUT_2 ;VALID PRESS,HAS BUTTON BEEN RELEASED YET? GOTO $-1 ;NO CALL DELAY_2 ;YES, WAIT FOR SWITCH BOUNCE BTFSC BUT_2 ;BOUNCE OVER? GOTO $-4 ;NO ;TOGGLE FUNCTION BTFSS LED_2 ;IS LED ALREADY ON? GOTO $+3 ;NO,GO THREE INSTUCTIONS DOWN BCF LED_2 ;YES SO CLEAR LED_2 GOTO RTN ;RTN BSF LED_2 ;SET LED_2 ;*********************************************************************** ;SWITCH BOUNCE ROUTINE BUTTON_3 BTFSS BUT_3 ;CHECK IF BUTTON ONE PRESSED GOTO BUTTON_4 ;NO GOTO BUTTTON FOUR CALL DELAY_2 ;WAIT FOR SWITCH BOUNCE BTFSS BUT_3 ;CHECK BUTTON STATE GOTO RTN ;INVALID PRESS, GO TO RTN BTFSC BUT_3 ;VALID PRESS,HAS BUTTON BEEN RELEASED YET? GOTO $-1 ;NO CALL DELAY_2 ;YES, WAIT FOR SWITCH BOUNCE BTFSC BUT_3 ;BOUNCE OVER? GOTO $-4 ;NO ;TOGGLE FUNCTION BTFSS LED_3 ;IS LED ALREADY ON? GOTO $+3 ;NO,GO THREE INSTUCTIONS DOWN BCF LED_3 ;YES SO CLEAR LED_3 GOTO RTN ;RTN BSF LED_3 ;SET LED_3 ;********************************************************************** BUTTON_4 BTFSS BUT_4 ;CHECK IF BUTTON ONE PRESSED GOTO RTN ;NO GOTO RTN CALL DELAY_2 ;WAIT FOR SWITCH BOUNCE BTFSS BUT_4 ;CHECK BUTTON STATE GOTO RTN ;INVALID PRESS, GO TO RTN BTFSC BUT_4 ;VALID PRESS,HAS BUTTON BEEN RELEASED YET? GOTO $-1 ;NO CALL DELAY_2 ;YES, WAIT FOR SWITCH BOUNCE BTFSC BUT_4 ;BOUNCE OVER? GOTO $-4 ;NO ;TOGGLE FUNCTION BTFSS LED_4 ;IS LED ALREADY ON? GOTO $+3 ;NO,GO THREE INSTUCTIONS DOWN BCF LED_4 ;YES SO CLEAR IT GOTO RTN ;RTN BSF LED_4 ;SET LED_4 RTN ;CONTEXT RESTORE MOVF STATUS_TEMP,W ;RESTORE STATUS REGISTER TO PREVIOUS VALUE - THIS DOES NOT AFFECT ANY BITS MOVWF STATUS ;PUT INTO STATUS REGISTER SWAPF WREG_TEMP,F ;SWAP W REG NIBBLES SWAPF WREG_TEMP,W ;SWAP NIBBLES AGAIN AND PUT INTO W REGISTER - THIS RESTORES W REGISTER TO PREVIOUS VALUE WITHOUT AFFECTING STATUS BITS BTFSC LED_1 ;CHECK IF ANY LEDS ENABLED, IF NOT THEN CLEAR DELAY_1 COUNTERS AND GOTO CANCEL. GOTO RTN2 ;IF ANY LED ENABLED THEN BRANCH AROUND TO RTN2 BTFSC LED_2 GOTO RTN2 BTFSC LED_3 GOTO RTN2 BTFSC LED_4 GOTO RTN2 CLRF COUNT1 ;CLEAR DELAY_1 COUNTERS CLRF COUNT2 CLRF COUNT3 CLRF COUNT4 GOTO CANCEL RTN2 BCF INTCON,RBIF ;CLEAR RB INTERUPT FLAG BSF INTCON,RBIE ;ENABLE RB INTERUPT RETFIE ;RETURN FROM INTERUPT - THIS INSTRUCTION ALSO SETS INTCON,GIE BIT ;**************************************************************************************************************************************************************************************************** DELAY_1 ;TWO HOUR COUNTER MOVLW .250 MOVWF COUNT1 MOVLW .250 MOVWF COUNT2 MOVLW .250 MOVWF COUNT3 MOVLW .144 MOVWF COUNT4 LOOP1 DECFSZ COUNT1,1 GOTO LOOP1 DECFSZ COUNT2,1 GOTO LOOP1 DECFSZ COUNT3,1 GOTO LOOP1 DECFSZ COUNT4,1 GOTO LOOP1 RETURN ;******************************************************************************************************************************************************************************************************* DELAY_2 ;55mS DELAY FOR SWITCH BOUNCE MOVLW .100 MOVWF COUNT5 MOVLW .35 ;72 MOVWF COUNT6 LOOP2 DECFSZ COUNT5,1 GOTO LOOP2 DECFSZ COUNT6,1 GOTO LOOP2 RETURN ;*************************************************************************************************************************************************************************************************************** END ;***************************************************************************************************************************************************************************************************************