;Tutorial 5_3 ;Read SIRC IR and toggle LED display, save settings in EEPROM data memory. ;Nigel Goodwin 2002 ;adapted for alarm by cyb0rg777 LIST p=16F628a ;tell assembler what chip we are using #INCLUDE "P16F628a.inc" ;include the defaults for the chip ;ERRORLEVEL 0, -302 ;suppress bank selection messages ERRORLEVEL -224, 1 __CONFIG 0x3F18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count ;used in looping routines count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine LoX Bit_Cntr Cmd_Byte Dev_Byte Flags Flags2 tmp1 ;temporary storage tmp2 tmp3 lastdev lastkey Tumbler endc IO_PORT Equ PORTB IO_TRIS Equ TRISB ;bits of IO_PORT SIREN Equ 0x03 ;siren output LED Equ 0x04 ;led output TRICK Equ 0x05 ;unimplimented ;bits of input on port b Vib_sense Equ 0x00 ;interrupt vibration sensor IR_In Equ 0x07 ;input assignment for IR data ;ign_sense Equ 0x04 ;ignition sensor ;bits of flags ErrFlag Equ 0x00 StartFlag Equ 0x01 ;flags used for received bit One Equ 0x02 Zero Equ 0x03 New Equ 0x04 ;flag used to show key released ;bit for alarm set Alarm Equ 0x00 ;flag set if alarm is active ;other junk TV_ID Equ 0x01 ;TV device ID But1 Equ 0x00 ;numeric button ID's But2 Equ 0x01 But3 Equ 0x02 But4 Equ 0x03 But5 Equ 0x04 But6 Equ 0x05 But7 Equ 0x06 But8 Equ 0x07 But9 Equ 0x08 ProgUp Equ d'16' ProgDn Equ d'17' VolUp Equ d'18' VolDn Equ d'19' Pwr Equ d'21' Mute Equ d'20' comb0 Equ But1 comb1 Equ But2 comb2 Equ But3 comb3 Equ But4 ;Do NOT USE A LINKER FILE!!! ORG 0x0000 goto Start ;interrupts start here ORG 0x0004 bcf INTCON,1 ;INTF - Clear flag bit btfss Flags2,Alarm retfie ;alarm is off nothing to do bsf IO_PORT,SIREN ;turn the siren on retfie Start movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) clrf PORTB bsf INTCON,7 ;GIE – Global interrupt enable (1=enable) bsf INTCON,4 ;INTE - RB0 interrupt enable (1=enable) bcf INTCON,1 ;INTF - Clear flag bit just in case Initialise clrf count clrf PORTB clrf Flags clrf Flags2 clrf Dev_Byte clrf Cmd_Byte SetPorts bsf STATUS, RP0 ;select bank 1 movlw b'10000001' movwf IO_TRIS bcf STATUS, RP0 ;select bank 0 Main clrf Tumbler Nextcomb movf Tumbler,w ;check if all codes are correct sublw b'00001111' btfsc STATUS, Z ;loop until tumbler = b'00000000' goto Toggle ;reset if call ReadIR ;read IR signal call ProcKeys ;do something with commands received goto Main Toggle movf Flags2,w movwf tmp3 btfsc tmp3,Alarm ;toggle alarm call Alarm_off btfss tmp3,Alarm call Alarm_on call Delay255 goto Main ;loop for ever ProcKeys movlw TV_ID ;check for TV ID code subwf Dev_Byte, w btfss STATUS , Z goto Main ;return if not correct device code movf Tumbler,w ;Check which key should be next sublw b'00000000' btfsc STATUS,Z goto Key1 ;try next key if order is correct movf Tumbler,w sublw b'00000001' btfsc STATUS,Z goto Key2 ;try next key if order is correct movf Tumbler,w sublw b'00000011' btfsc STATUS,Z goto Key3 ;try next key if order is correct movf Tumbler,w sublw b'00000111' btfsc STATUS,Z goto Key4 ;try next key if order is correct goto Main ;not implimented yet Key1 movlw comb0 ;test for code1 subwf Cmd_Byte, w btfss STATUS , Z goto Main movlw b'00000001' ;set tumbler to start movwf Tumbler bcf Flags, New ;and cancel new flag goto Nextcomb Key2 movlw comb1 ;test for code2 subwf Cmd_Byte, w btfss STATUS , Z goto Main bsf Tumbler,1 ;and cancel new flag bcf Flags, New ;and cancel new flag goto Nextcomb Key3 movlw comb2 ;test for button 1 subwf Cmd_Byte, w btfss STATUS , Z goto Main bsf Tumbler,2 ;and cancel new flag bcf Flags, New ;and cancel new flag goto Nextcomb Key4 movlw comb3 ;test for button 1 subwf Cmd_Byte, w btfss STATUS , Z goto Main bsf Tumbler,3 bcf Flags, New ;and cancel new flag goto Nextcomb ;alarm routines Alarm_on bsf Flags2,Alarm bsf IO_PORT,LED ;change LED retlw 0x00 Alarm_off ;may be possible to first reset alarm if it is tripped and second turn alarm off bcf Flags2,Alarm bcf IO_PORT,SIREN ; btfsc IO_PORT,LED ;turn off led bcf IO_PORT,LED retlw 0x00 ;IR routines ReadIR call Read_Pulse btfss Flags, StartFlag goto ReadIR ;wait for start pulse (2.4mS) Get_Data movlw 0x07 ;set up to read 7 bits movwf Bit_Cntr clrf Cmd_Byte Next_RcvBit2 call Read_Pulse btfsc Flags, StartFlag ;abort if another Start bit goto ReadIR btfsc Flags, ErrFlag ;abort if error goto ReadIR bcf STATUS , C btfss Flags, Zero bsf STATUS , C rrf Cmd_Byte , f decfsz Bit_Cntr , f goto Next_RcvBit2 rrf Cmd_Byte , f ;correct bit alignment for 7 bits Get_Cmd movlw 0x05 ;set up to read 5 bits movwf Bit_Cntr clrf Dev_Byte Next_RcvBit call Read_Pulse btfsc Flags, StartFlag ;abort if another Start bit goto ReadIR btfsc Flags, ErrFlag ;abort if error goto ReadIR bcf STATUS , C btfss Flags, Zero bsf STATUS , C rrf Dev_Byte , f decfsz Bit_Cntr , f goto Next_RcvBit rrf Dev_Byte , f ;correct bit alignment for 5 bits rrf Dev_Byte , f rrf Dev_Byte , f retlw 0x00 ;end of ReadIR ;read pulse width, return flag for StartFlag, One, Zero, or ErrFlag ;output from IR receiver is normally high, and goes low when signal received Read_Pulse clrf LoX btfss IO_PORT, IR_In ;wait until high goto $-1 clrf tmp1 movlw 0xC0 ;delay to decide new keypress movwf tmp2 ;for keys that need to toggle Still_High btfss IO_PORT, IR_In ;and wait until goes low goto Next incfsz tmp1,f goto Still_High incfsz tmp2,f goto Still_High bsf Flags, New ;set New flag if no button pressed goto Still_High Next nop nop nop nop nop ;waste time to scale pulse nop ;width to 8 bits nop nop nop nop nop nop incf LoX, f btfss IO_PORT, IR_In goto Next ;loop until input high again ; test if Zero, One, or Start (or error) Chk_Pulse clrf Flags TryError movf LoX, w ; check if pulse too small addlw d'255' - d'20' ; if LoX <= 20 btfsc STATUS , C goto TryZero bsf Flags, ErrFlag ; Error found, set flag retlw 0x00 TryZero movf LoX, w ; check if zero addlw d'255' - d'60' ; if LoX <= 60 btfsc STATUS , C goto TryOne bsf Flags, Zero ; Zero found, set flag retlw 0x00 TryOne movf LoX, w ; check if one addlw d'255' - d'112' ; if LoX <= 112 btfsc STATUS , C goto TryStart bsf Flags, One ; One found, set flag retlw 0x00 TryStart movf LoX, w ; check if start addlw d'255' - d'180' ; if LoX <= 180 btfsc STATUS , C goto NoMatch bsf Flags, StartFlag ; Start pulse found retlw 0x00 NoMatch ; pulse too long bsf Flags, ErrFlag ; Error found, set flag retlw 0x00 ;end of pulse measuring routines ;Delay routines Delay255 movlw 0xff ;delay 255 mS goto d0 Delay100 movlw d'100' ;delay 100mS goto d0 Delay50 movlw d'50' ;delay 50mS goto d0 Delay20 movlw d'20' ;delay 20mS goto d0 Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock) d0 movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 ;end of Delay routines end