'Program SONY_RX.BAS 'Reads signals from a Sony IR remote control, 'and returns the button code in the variable IR_But. 'the device code in the variable IR_DEV, 'The infrared receiver module used is the Sharp IS1U60. 'The infrared receiver module for this experiment should, 'be a type that is set for a 40kHz carrier frequency. 'If another type is used. some reduction in range may be noticed. 'The remote control used, may be either a Sony manufactured unit, 'or one of the universal remotes that can be configured for Sony equipment. 'This is important since we are dealing with a specific signal protocol. 'With Sony’s SIRC specification, a start pulse is initially sent to 'indicate the beginning of a frame of data. This pulse is approx 2.4 ms in length. 'Following this, are 5-bits of data, which represent the DEVICE code, 'which signifies the target device (TV, VCR, etc.). 'Then an additional 7-bit BUTTON code 'Data bits are sent with the most significant bit first (MSB). Include "Modedefs.Bas" Define OSC 4 ' Set Xtal Frequency ' ** Declare the Variables ** Header Var Word ' Header pulse length Packet Var Word ' 12-bit IR information P_Val Var Byte ' The bit length 60us = 0, 120us = 1 IR_But Var Byte ' The BUTTON code returned IR_Dev Var Byte ' The DEVICE code returned Sony_LP Var Byte ' Temporary loop variable Red_LED Var PortA.0 ' Assign the green LED to this pin Green_LED Var PortA.1 ' Assign the red LED to this pin IR_Sensor Var PortA.2 ' Assign the IR Sensor to this pin ' ** THE MAIN PROGRAM LOOP STARTS HERE ** Again: Low Green_LED:Low Red_LED ' Extinguish both LED's Gosub IRIN ' Receive an IR signal If IR_Dev=255 then goto Again ' Check if header was valid Serout PortA.3,N9600,[IR_Dev,IR_But] ' Transmit the 2 bytes serially If IR_Dev<>0 then goto Again ' If not a TV DEVICE code then look again If IR_But=116 then High Green_LED ' If channel up, then light the green LED If IR_But=117 then High Red_LED ' If channel down, then light the green LED Pause 100 ' Delay for 100ms Goto Again ' Do it forever ' 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 a header was not detected then IR_DEV and IR_BUT will hold 255 IRIN: IR_Dev=255:IR_But=255 Pulsin PortA.2,0,Header ' Measure the header length. If Header < 200 then Return ' Verify a good header If Header > 270 then Return ' If not valid then exit ' Receive the data bits (MSB first), and convert them into a 12-bit packet ' A one should return approx 120, actual timing is 1200us ' A zero should return approx 60, actual timing is 600us ' We split the difference and say that < 90 is a 0 and >= 90 is a 1 ' These values are for use with a 4mHz crystal For Sony_Lp=0 to 11 ' Do 12-bits Pulsin PortA.2,0,P_Val ' Receive the IR bit pulse If P_Val >= 90 then ' If it's >= 90 then we've received a 1 Packet.0[Sony_Lp]=1 ' So set the appropriate bit of PACKET Else ' Else Packet.0[Sony_Lp]=0 ' Clear the appropriate bit of PACKET Endif Next ' Close the loop ' Split the 7-bit BUTTON code, and the 5-bit DEVICE code IR_But=Packet & %01111111 ' Mask the 7 BUTTON bits IR_Dev=(Packet >>7) & %00011111 ' Move down and mask, the 5 DEVICE bits Return