Code:
'Test program for 16F727 with IR sensor (PNA4612M)
'****************************************************************
Include "MODEDEFS.BAS" ' Include Shiftin/out modes
DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTC ' Set LCD Register Select port
DEFINE LCD_RSBIT 5 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTC ' Set LCD Enable port
DEFINE LCD_EBIT 6 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
'DEFINE ADC_BITS 8 ' Set number of bits in ADC result
'DEFINE ADC_CLOCK 3 ' Set clock source for ADC (rc = 3)
'DEFINE ADC_SAMPLEUS 100 ' Set ADC sampling time in microseconds
DEFINE OSC 8 ' 8 MHz Osc.
define I2C_SCLOUT 1 ' Set I2C Clock to drive high instead of needing pullup
'?????????????????????????????????????????????????????????????????
@ __config _CONFIG1, _DEBUG_OFF & _PLL_EN & _BORV_2_5 & _BOR_ON & _CP_OFF & _MCLRE_OFF & _PWRT_EN & _WDT_ON & _INTOSCIO
@ __config _CONFIG2, _VCAP_RA6
OSCCON = %00100000 'Set Osc to 8MHz.
OPTION_REG = %10101111
CPSCON0.0 = 1
TRISA= %00000000 'Set 'em all to outputs
TRISB= %11111111 'Set portB to inputs
TRISC= %00000000 'Set portC all outputs
ANSELA= %00000000 ' Set all pins to digital...
ANSELB= %00000000 ' Set all pins to digital
ADCON0 = %00100001
'??????????????????????????????????????????????????????????????????????
' Alias pins - LCD
backlight var PORTC.2 'LCD backlight
RW var PORTC.7 'LCD R/W
'Alias pins - I.R. Input
IRinput var PORTB.0
'Allocate IR Sensor variables
Bitcnt var byte 'Loop counter
Header var Word 'Hold the length value of the first PULSIN to check for header.
P_Val var word 'Hold the length value of each consecutive bit in it's turn.
Packet var word 'Stores the raw code built up from 12 cycles of counting P_Val
IR_Dev var byte 'Stores the code # for the DEVICE being addressed (decoded from Packet)
IR_But var Byte 'Stores the code # for the BUTTON pressed. (decoded from Packet)
clear
pause 200 'Give the LCD a chance to wake up
Lcdout $fe,1 'reset LCD
HPWM 1,127,200 'Set the backlight brightness
LCDOUT $fe,2, " test" 'print "test" and pause for a second.
pause 1000
LCDOUT $fe,1 'clear the display
' Receive a signal from a Sony remote control,
' and return with the 7-bit BUTTON code in the variable
' IR_BUT and the 5-bit DEVICE code in the variable IR_DEV.
' If no header then IR_DEV, IR_BUT will hold 255
' MAIN PROGRAM LOOP
Again:
Gosub IRIN ' Receive an IR signal
If IR_Dev=255 then goto Again ' Check for valid header
If IR_Dev=0 then goto Again ' If not a valid device code then look again
If IR_But=0 then LCDOUT $fe,2, "0, Channel 1 "
If IR_But=1 then LCDOUT $fe,2, "1, Channel 2 "
If IR_But=2 then LCDOUT $fe,2, "2, Channel 3 "
If IR_But=3 then LCDOUT $fe,2, "3, Channel 4 "
If IR_But=4 then LCDOUT $fe,2, "4, Channel 5 "
If IR_But=5 then LCDOUT $fe,2, "5, Channel 6 "
If IR_But=6 then LCDOUT $fe,2, "6, Channel 7 "
If IR_But=7 then LCDOUT $fe,2, "7, Channel 8 "
If IR_But=8 then LCDOUT $fe,2, "8, Channel 9 "
If IR_But=9 then LCDOUT $fe,2, "9, Channel 0 "
If IR_But=11 then LCDOUT $fe,2, "11 Enter "
If IR_But=14 then LCDOUT $fe,2, "14 Guide "
If IR_But=16 then LCDOUT $fe,2, "16 Channel Up "
If IR_But=17 then LCDOUT $fe,2, "17 Channel Down "
If IR_But=18 then LCDOUT $fe,2, "18 Volume Up "
If IR_But=19 then LCDOUT $fe,2, "19 Volume Down "
If IR_But=20 then LCDOUT $fe,2, "20 Mute "
If IR_But=21 then LCDOUT $fe,2, "21 Power "
If IR_But=37 then LCDOUT $fe,2, "37 TV/VCR input "
If IR_But=42 then LCDOUT $fe,2, "42 Record "
If IR_But=51 then LCDOUT $fe,2, "51 Right "
If IR_But=52 then LCDOUT $fe,2, "52 Left "
If IR_But=54 then LCDOUT $fe,2, "54 Sleep "
If IR_But=58 then LCDOUT $fe,2, "58 Info "
If IR_But=59 then LCDOUT $fe,2, "59 Repeat "
If IR_But=64 then LCDOUT $fe,2, "64 Play "
If IR_But=65 then LCDOUT $fe,2, "65 Pause "
If IR_But=67 then LCDOUT $fe,2, "67 Scan + "
If IR_But=68 then LCDOUT $fe,2, "68 Scan - "
If IR_But=77 then LCDOUT $fe,2, "77 Stop "
If IR_But=91 then LCDOUT $fe,2, "91 PIP Toggle "
If IR_But=95 then LCDOUT $fe,2, "95 PIP Swap "
If IR_But=96 then LCDOUT $fe,2, "96 Menu, Quit "
If IR_But=101 then LCDOUT $fe,2,"101 Select "
If IR_But=116 then LCDOUT $fe,2,"116 Channel Up "
If IR_But=117 then LCDOUT $fe,2,"117 Channel Down "
Goto Again ' Repeat until nauseated...
IRIN:
IR_Dev=255:IR_But=255 ' Preset the Return variables
Pulsin IRinput,0,Header ' Measure the header length.
If Header < 450 then Return' If the result is less than 450 toss it out
If Header > 520 then Return' If the result is greater than 520 toss it out
' Receive the 12 data bits and convert them into a packet
For Bitcnt=0 to 11 ' Create a loop of 12
Pulsin IRinput,0,P_Val ' Receive the IR pulses
If P_Val >= 190 then ' If >= 190 then we've received a 1
Packet.0[Bitcnt]=1 ' So set the appropriate bit
Else
Packet.0[Bitcnt]=0 '...or clear the appropriate bit
Endif
Next
IR_But=Packet & %01111111 'Extract the 7 BUTTON bits
IR_Dev=(Packet >>7) & %00011111 'Right shift and extract the 5 DEVICE bits
lcdout $fe,1, dec IR_But, " ", $fe,$c0, dec IR_Dev, " ", dec header
return
end
----
Bookmarks