Advertisement
Guest User

IRbeacon Code

a guest
Jan 22nd, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pic 16 4.88 KB | None | 0 0
  1. LIST   P=PIC12F683
  2. #include <p12F683.inc>
  3.  
  4.  
  5. ;External reset, no code protection, watchdog timer off, internal clock
  6. __CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
  7.  
  8.  
  9. ;This Code tells the pic to constantly emit an IR bitstream on pin IR_PIN
  10. ;every 50ms+255ms(if DELAY1_PIN is low)+255ms(if DELAY2_PIN) is low.
  11. ;The IR bitstream is in the SONY TV format.
  12. ;The device ID is configurable by changing TV_ID.
  13. ;The IR data payload is configurable by changing BEACON_ID.
  14. ;An LED flashes when data is set, this pin can be configured by changing LED_PIN.
  15. ;Remember, for all pins you change to update GPIO_CONFIG and PULLUP_CONFIG.
  16.  
  17. ;Variable Definitions
  18. cblock 0x20
  19.     count1
  20.     counta
  21.     countb
  22.     count
  23.     Delay_Count
  24.     Bit_Cntr
  25.     Data_Byte
  26.     Dev_Byte
  27.     Pulse
  28. endc
  29.  
  30.  
  31.  
  32. ;Configurable constants
  33. GPIO_CONFIG     Equ b'111100'   ;GP0/GP1 outputs (0 = output), GP2/GP4/GP5 inputs
  34. PULLUP_CONFIG   Equ b'110100'   ;Enable pullups on GP2/GP4/GP5
  35. DELAY1_PIN      Equ 5           ;Pin that controls short delays
  36. DELAY2_PIN      Equ 4           ;Pin that controls longer delays
  37. IR_PIN          Equ 0           ;Use GP0 as the IR transmitter pin
  38. LED_PIN         Equ 1           ;Use GP1 as the LED
  39. TV_ID           Equ 0x01        ;Device ID bits to send (TV ID ATM)
  40. BEACON_ID       Equ d'16'       ;Use ProgUp button ID as the beacon ID for now
  41.  
  42.  
  43. org 0
  44. setupPins
  45.     BCF         STATUS,RP0      ; Switch -> Bank 0
  46.    
  47.     CLRF        GPIO            ;Init GPIO - GPIO set all outputs to 0
  48.    
  49.     BSF         STATUS,RP0      ; Select Registers at Bank 1
  50.     CLRF        ANSEL           ;set analog config bits to zero - all pins are digital I/O
  51.    
  52.     MOVLW       GPIO_CONFIG     ;GP0/GP1 outputs, GP2/GP4/GP5 inputs
  53.     MOVWF       TRISIO          ;write bitset to GPIO control register(TRISIO)
  54.    
  55.     BCF         OPTION_REG,0x7  ;Enable input pullup resistors
  56.     MOVLW       PULLUP_CONFIG   ;Load pullup config
  57.     MOVWF       WPU             ;write config to register
  58.    
  59.     MOVLW       0x60            ;0x70 = 8Mhz,0x60 = 4Mhz
  60.     MOVWF       OSCCON          ; Set the internal clock speed to 4 Mhz
  61.  
  62. main
  63.     BCF         STATUS,RP0      ; Switch -> Bank 0
  64.  
  65.     BTFSS       GPIO,DELAY1_PIN ;Test to see if we should delay 255ms
  66.     CALL        Delay255
  67.    
  68.     BTFSS       GPIO,DELAY2_PIN ;Test to see if we should delay 255ms
  69.     CALL        Delay255
  70.  
  71.     BSF         GPIO,LED_PIN    ;Turn on the LED
  72.     movlw       BEACON_ID       ;Load the data to send via IR
  73.     call        Transmit        ;Actually bitbang the IR
  74.     BCF         GPIO,LED_PIN    ;Turn off the LED
  75.    
  76.     CALL        Delay50         ;Delay 50ms as a minimum
  77.  
  78.    
  79.     GOTO        main
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. ;Delay routines
  92. Delay255    movlw   0xff        ;delay 255 mS
  93.         goto    d0
  94. Delay100    movlw   d'100'      ;delay 100mS
  95.         goto    d0
  96. Delay50     movlw   d'50'       ;delay 50mS
  97.         goto    d0
  98. Delay27     movlw   d'27'       ;delay 27mS
  99.         goto    d0
  100. Delay20     movlw   d'20'       ;delay 20mS
  101.         goto    d0
  102. Delay5      movlw   0x05        ;delay 5.000 ms (4 MHz clock)
  103. d0      movwf   count1
  104. d1      movlw   0xC7
  105.         movwf   counta
  106.         movlw   0x01
  107.         movwf   countb
  108. Delay_0     decfsz  counta, f
  109.         goto    $+2
  110.         decfsz  countb, f
  111.         goto    Delay_0
  112.  
  113.         decfsz  count1  ,f
  114.         goto    d1
  115.         retlw   0x00
  116.  
  117. ;end of Delay routines
  118.  
  119.  
  120.  
  121.  
  122. ; IR Routines
  123. Transmit        MOVWF   Data_Byte               ;move W to Data_Byte
  124.                 MOVLW   0x07                    ;set 7 DATA bits out
  125.                 MOVWF   Bit_Cntr
  126.                 call    TX_Start                ;send start bit
  127. Ser_Loop        RRF     Data_Byte , f           ;send one bit
  128.                 BTFSC   STATUS    , C
  129.                 call    TX_One
  130.                 BTFSS   STATUS    , C
  131.                 call    TX_Zero
  132.                 DECFSZ  Bit_Cntr  , f           ;test if all done
  133.                 GOTO    Ser_Loop
  134.                                                 ;now send device data
  135.                 movlw   TV_ID
  136.                 movwf   Dev_Byte                ;set device to TV
  137.                 MOVLW   0x05                    ;set 5 device bits out
  138.                 MOVWF   Bit_Cntr
  139. Ser_Loop2       RRF     Dev_Byte , f            ;send one bit
  140.                 BTFSC   STATUS    , C
  141.                 call    TX_One
  142.                 BTFSS   STATUS    , C
  143.                 call    TX_Zero
  144.                 DECFSZ  Bit_Cntr  , f           ;test if all done
  145.                 GOTO    Ser_Loop2
  146.                 retlw   0x00
  147.  
  148.  
  149.  
  150. TX_Start
  151.         movlw   d'92'
  152.         call    IR_pulse
  153.         movlw   d'23'
  154.         call    NO_pulse
  155.         retlw   0x00
  156.  
  157. TX_One
  158.         movlw   d'46'
  159.         call    IR_pulse
  160.         movlw   d'23'
  161.         call    NO_pulse
  162.         retlw   0x00
  163.  
  164. TX_Zero
  165.         movlw   d'23'
  166.         call    IR_pulse
  167.         movlw   d'23'
  168.         call    NO_pulse
  169.         retlw   0x00
  170.  
  171. IR_pulse               
  172.         MOVWF   count       ;  Pulses the IR led at 38KHz
  173. irloop
  174.         BSF GPIO,   IR_PIN
  175.         NOP         ;
  176.         NOP         ;
  177.         NOP         ;
  178.         NOP         ;
  179.         NOP         ;
  180.         NOP         ;
  181.         NOP         ;
  182.         BCF GPIO,   IR_PIN
  183.         NOP         ;
  184.         NOP         ;
  185.         NOP         ;
  186.         NOP         ;
  187.         NOP         ;
  188.         NOP         ;  
  189.         NOP         ;
  190.         NOP         ;  
  191.         NOP         ;
  192.         NOP         ;
  193.         NOP
  194.         NOP
  195.         NOP         ;
  196.         NOP         ;
  197.         DECFSZ  count,F
  198.         GOTO    irloop 
  199.         RETLW   0
  200.  
  201. NO_pulse               
  202.         MOVWF   count       ;  Doesn't pulse the IR led
  203. irloop2     BCF GPIO,   IR_PIN
  204.         NOP         ;
  205.         NOP         ;
  206.         NOP         ;
  207.         NOP         ;
  208.         NOP         ;
  209.         NOP         ;
  210.         NOP         ;
  211.         NOP         ;
  212.         NOP         ;
  213.         NOP         ;
  214.         NOP         ;
  215.         BCF     GPIO,   IR_PIN
  216.         NOP         ;
  217.         NOP         ;  
  218.         NOP         ;
  219.         NOP         ;  
  220.         NOP         ;
  221.         NOP         ;
  222.         NOP
  223.         NOP
  224.         NOP         ;
  225.         NOP         ;
  226.         DECFSZ  count,F
  227.         GOTO    irloop2
  228.         RETLW   0
  229.  
  230. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement