#include <p16f628a.inc>
__CONFIG _WDT_OFF & _PWRTE_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _BOREN_OFF & _LVP_OFF & _DATA_CP_OFF & _CP_OFF
cblock 0x20
nCount1
nCount2
nCarrier
nData
nBits_Data
nIDL
nIDH
nCmdL
nCmdH
endc
Load_Data macro tIDL, tIDH, tCmdL, tCmdH ;Macro to load the working variables with the remotes 32 bit sequence
MOVLW tIDL
MOVWF nIDL
MOVLW tIDH
MOVWF nIDH
MOVLW tCmdL
MOVWF nCmdL
MOVLW tCmdH
MOVWF nCmdH
endm
;Codes for transmission
vDevIDL equ b'01100001'
vDevIDH equ b'10100000'
vPwrL equ b'11110000'
vPwrH equ b'00001111'
vChUL equ b'01010000'
vChUH equ b'10101111'
vChDL equ b'11010000'
vChDH equ b'00101111'
ORG 0x000
goto Main
ORG 0x004
goto Main
Main:
BCF OPTION_REG, 7 ;enable portb internal pull-up resistors
BSF STATUS, 5 ;Switch to bank1
MOVLW b'00001110' ;Load W with 0;
MOVWF TRISB ;Set everything on PORTB.7-4 to output 3-1 Input, and 0 to output
BCF STATUS, 5 ;Switch back to bank0
Start:
BCF PORTB, 6 ;Indicator Lights (need to see if there are misfires)
BCF PORTB, 5 ;All off unless button pressed
BCF PORTB, 4
BTFSS PORTB, 1
CALL SW1
BTFSS PORTB, 2
CALL SW2
BTFSS PORTB, 3
CALL SW3
GOTO Start
;Three buttons
SW1 ;Power Button
BSF PORTB, 6
BCF PORTB, 5
BCF PORTB, 4
Load_Data vDevIDL, vDevIDH, vPwrL, vPwrH
CALL TRANSMIT
return
SW2 ;Channel up
BCF PORTB, 6
BSF PORTB, 5
BCF PORTB, 4
Load_Data vDevIDL, vDevIDH, vChUL, vChUH
CALL TRANSMIT
return
SW3 ;Channel down
BCF PORTB, 6
BCF PORTB, 5
BSF PORTB, 4
Load_Data vDevIDL, vDevIDH, vChDL, vChDH
CALL TRANSMIT
return
TRANSMIT
MOVWF nData ;Loop 8 times until all 8 bits are sent
MOVLW 0x08
MOVWF nBits_Data
call SEND_START
trans_loop1
RLF nIDL, 1
BTFSC STATUS, C ;Check current bit and send data
CALL SEND_1
BTFSS STATUS, C
CALL SEND_0
DECFSZ nBits_Data
GOTO trans_loop1
MOVLW 0x08
MOVWF nBits_Data
trans_loop2
RLF nIDH, 1
BTFSC STATUS, C ;Skip next instruction if C = 0
CALL SEND_1
BTFSS STATUS, C ;Skip next instruction if C = 1
CALL SEND_0
DECFSZ nBits_Data
GOTO trans_loop2
MOVLW 0x08
MOVWF nBits_Data
trans_loop3
RLF nCmdL, 1
BTFSC STATUS, C
CALL SEND_1
BTFSS STATUS, C
CALL SEND_0
DECFSZ nBits_Data
GOTO trans_loop3
MOVLW 0x08
MOVWF nBits_Data
trans_loop4
RLF nCmdH, 1
BTFSC STATUS, C
CALL SEND_1
BTFSS STATUS, C
CALL SEND_0
DECFSZ nBits_Data
GOTO trans_loop4
MOVLW 0x08
MOVWF nBits_Data
return
;0 takes 0.6ms -> 600us 600/26 = 23 = 17h
;1 and 0 turn on for 600us each
SEND_0 ;Total length 1.2ms
MOVLW 0x17 ;600us carrier active
CALL ON_38KHZ
NOP
MOVLW 0x17 ;600us carrier inactive
CALL OFF_38KHZ
NOP
return
SEND_1 ;Total length 2.2ms
MOVLW 0x17 ;Turn on for 600 us
CALL ON_38KHZ
NOP
MOVLW 0x3D ;Turn off for 1.6ms
CALL OFF_38KHZ
NOP
return
SEND_START
MOVLW 0xDC ;5.727ms 1 of 2 high
CALL ON_38KHZ
NOP
MOVLW 0xDC ;5.727ms 2 of 2 high
CALL ON_38KHZ
NOP
MOVLW 0xB3 ;4.653ms low
CALL OFF_38KHZ
NOP
return
ON_38KHZ ;T = 1/38000 = 0.000026316s -> 0.026315789ms ->26.315789474us
MOVWF nCarrier
carrier_loop1
BSF PORTB, 0 ;Turn PORTB.0 on about 35% duty cycle
GOTO $+1 ;Each GOTO takes 2 cycles (command tells it to move one instruction from current pos $)
GOTO $+1
GOTO $+1
GOTO $+1
BCF PORTB, 0 ;Turn PORTB.0 off move this to adjust duty cyle
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
DECFSZ nCarrier, 1
GOTO carrier_loop1
return
OFF_38KHZ
MOVWF nCarrier
carrier_loop2
BCF PORTB, 0 ;Turn PORTB.0 off
NOP
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
GOTO $+1
DECFSZ nCarrier, 1
goto carrier_loop2
return
END