RX page







  

 

RX Code

 LIST p=16F628  ;tell assembler what chip we are using
 include "P16F628.inc" ;include the defaults for the chip
 ERRORLEVEL 0, -302  ;suppress bank selection messages
 __config 0x3D18   ;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    ;this is a gpr, it can be any.
   Bit_Cntr  ;all these are gpr’s
   Cmd_Byte
   Dev_Byte
   Flags
   Flags2
   tmp1   ;temporary storage
   tmp2
   tmp3
   Mark20

   endc

LED_PORT Equ PORTB   ;assigning ports
LED_TRIS Equ TRISB

IR_PORT  Equ PORTA
IR_TRIS  Equ TRISA
IR_In  Equ 0x02   ;input assignment for IR data

OUT_PORT Equ PORTB   ;assign LED output ports
LED0  Equ 0x00
LED1  Equ 0x01
LED2  Equ 0x02
LED3  Equ 0x03
LED4  Equ 0x04
LED5  Equ 0x05
LED6  Equ 0x06
LED7  Equ 0x07

EEPROM_Addr Equ 0x00   ;address of EEPROM byte used
   
ErrFlag  Equ 0x00   ;these are flag locations
StartFlag Equ 0x01   ;flags used for received bit
One   Equ 0x02   ;represent bit received criterias
Zero  Equ 0x03

New   Equ 0x07   ;flag used to show key released

TV_ID  Equ 0x01   ;TV device ID

But1  Equ 0x00   ;numeric button ID's
But2  Equ 0x01   ;these are numbers,
 not address
But3  Equ 0x02   ;address locations
But4  Equ 0x03   ;NOT SURE IF I'VE DONE THIS CORRECTLY?
But5  Equ 0x04   ;ASSIGNED THE CORRECT BUTTON ID'S?
But6  Equ 0x05
But7  Equ 0x06
But8  Equ 0x07
But9  Equ 0x08

  org  0x0000
  goto Start

        org     0x0004   ;interupt startup address MPLAB HAS A PROBLEM 
        retfie                ; WITH THIS...0004

Start movlw 0x07
  movwf CMCON   ;turn comparators off (make it like a 16F84)

Initialise clrf count   ;clear all important registers
  clrf PORTA
  clrf PORTB
  clrf Flags
        clrf    Dev_Byte
  clrf Cmd_Byte 
  movlw d'20'   ;sets up 5 second timing marker
  movwf Mark20
  
 


SetPorts bsf  STATUS, RP0  ;select bank 1
  movlw 0x00   ;make all LED pins outputs
  movwf LED_TRIS
  movlw b'11111111'  ;make all IR port pins inputs
  movwf IR_TRIS   
  bcf  STATUS, RP0  ;select bank 0

  
Main
  call ReadIR   ;read IR signal
  call ProcKeys  ;do something with commands received

  goto Main   ;loop for ever

ProcKeys
  btfss Flags2, New
  retlw 0x00   ;return if not new keypress
  movlw TV_ID   ;check for TV ID code
  subwf   Dev_Byte,w
  btfss   STATUS, Z
  retlw 0x00   ;return if not correct code

  movlw But1   ;test for button 1
  subwf   Cmd_Byte, w  ;moves code into w and sub’s versus
  btfss   STATUS, Z  ;cmd_byte, if zero, test which key.
  goto Key1   ;try next key if not correct code

  bsf LED_PORT,LED0  ;turn on LED
  call  LED_5sec  ;Routine, led on for 5 seconds
  bcf LED_PORT,LED0  ;turn off LED
  bcf Flags2, New  ;and cancel new flag 
  retlw 0x00

Key1 movlw But2   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key2   ;try next key if not correct code

  bsf LED_PORT, LED1  ;turn on LED
  call LED_5sec  ;5 seconds logic 1
  bcf LED_PORT, LED1  ;turn off LED
  bcf Flags2, New  ;and cancel new flag 
  retlw 0x00

Key2  movlw But3   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key3   ;try next key if not correct code

  bsf LED_PORT, LED2  ;turn on LED
  call LED_5sec  ;5 seconds logic 1
  bcf LED_PORT, LED2  ;turn off LED
  bcf Flags2, New  ;and cancel new flag
  retlw 0x00

Key3  movlw But4   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key4   ;try next key if not correct code

  bsf LED_PORT, LED3  ;turn on LED
  call LED_5sec  ;5 seconds logic 1
  bcf LED_PORT, LED3  ;turn off LED
  bcf Flags2, New  ;and cancel new flag
  retlw 0x00

Key4 movlw But5   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key5   ;try next key if not correct code

  bsf  LED_PORT, LED4  ;turn on LED
  call LED_5sec   ;5 seconds logic 1
  bcf  LED_PORT,LED4  ;turn off LED
  bcf  Flags2, New   ;and cancel new flag
  retlw 0x00

Key5 movlw But6   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key6   ;try next key if not correct code

  bsf LED_PORT,LED5  ;turn on LED
  call LED_5sec  ;5 seconds logic 1
  bcf LED_PORT, LED5  ;turn off LED
  bcf Flags2, New  ;and cancel new flag
  retlw 0x00

Key6  movlw But7   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  goto Key7   ;try next key if not correct code

  bsf LED_PORT, LED6  ;turn on LED
  btfsc tmp3, LED6
  bcf LED_PORT, LED6  ;turn off LED
  bcf Flags2, New  ;and cancel new flag
  retlw 0x00

Key7  movlw But8   ;test for button 1
  subwf   Cmd_Byte, w
  btfss   STATUS, Z
  retlw 0X00

  bsf LED_PORT, LED7  ;turn on LED
  call LED_5sec  ;5 seconds logic 1
  bcf LED_PORT, LED7  ;turn off LED
  bcf Flags2, New  ;and cancel new flag
  retlw 0x00

;IR routines

ReadIR  

call Read_Pulse  ;this loop will only ever progress
  btfss Flags, StartFlag ;with setting of 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   IR_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   IR_PORT, IR_In  ;and wait until goes low
             goto    Next
  incfsz tmp1,f
  goto Still_High
  incfsz tmp2,f
  goto Still_High
  bsf Flags2, 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   IR_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

;5 second timeloop routine

LED_5sec
  call Delay255   ;uses existing 255ms delay
  decfsz Mark20,f  ;has this happened 20 times
  goto LED_5sec  ;to create a 5 second delay?
  movlw d'20'
  movwf Mark20
  retlw 0x00
  
;end of 5 second on led delay routine

  end