Proton BASIC Compiler - Re: Ir Remote interfaced Fan Speed Controller


  • PicŪ Basic


  • Re: Ir Remote interfaced Sony SIRC decoder

    With the need for a Sony remote, the BASIC code below decodes Sony SIRC.

    Because it's written in BASIC, it can be adjusted for your requirements. The code can be optimised by the Pulsin_Maximum declare which will adjust how long Pulsin waits when no signal is received.

    The code will need different values for the Pulsin results depending on the crystal used on the device. The code in the program is suitable for a 4MHz crystal.

    Code:
    ' Sony Infrared receiver written in Proton BASIC
    '
        Device = 16F877
        Declare Xtal= 4
    '
    ' Setup the LCD interface
    '
        Declare LCD_DTPin = PORTD.4
        Declare LCD_RSPin = PORTE.0
        Declare LCD_ENPin = PORTE.1
        Declare LCD_Interface = 4
        Declare LCD_Lines = 2
        Declare LCD_Type = 0
        Declare LCD_CommandUs = 2000
        Declare LCD_DataUs = 50
    
        Declare Pulsin_Maximum = 1000                   ' Adjust how long PulseIn waits
    
        Dim Sony_wHeader As Word                        ' Header pulse length
        Dim Sony_wPacket As Sony_wHeader                ' 12-bit IR information
        Dim Sony_bPValue As Byte                        ' The bit length
        Dim Sony_bButton As Byte                        ' The Button code
        Dim Sony_bDevice As Byte                        ' The Device code
        Dim Sony_bLoop As Byte                          ' Loop variable
    
        Symbol IR_tSensor = PORTC.0                     ' Assign the IR Sensor
    '---------------------------------------------------------------------------------
    
        GoTo Main                                       ' Jump over the subroutine
    
    '---------------------------------------------------------------------------------
    ' Receive a signal from a Sony remote control
    ' Input     : None
    ' Output    : The button value is returned in Sony_bButton
    '           : The device is returned in Sony_bDevice
    '           : Sony_bDevice will return holding 255 if an invalid header was received
    '
    $define Sony_In() GoSub _SonyIn_
    
    _SonyIn_:
        Sony_bDevice = 255
        Sony_bButton = 255
        If IR_tSensor = 0 Then Return                       ' Return if we're already inside a packet
        Sony_wHeader = PulsIn IR_tSensor,Low                ' Measure the header
        If Sony_wHeader < 200 Then Return                   ' \
        If Sony_wHeader > 270 Then Return                   ' / These values will need to be changed depending on what Osc is used
        Sony_bLoop = 0
        Repeat                                              ' Implement a loop for the 12 bits
            Sony_bPValue = PulsIn IR_tSensor,Low
            Sony_wPacket.11 = 0                             ' Default to a clear bit (zero-bit)
            If Sony_bPValue >= 90 Then Sony_wPacket.11 = 1  ' This value will need to be changed depending on what Osc is used
            Sony_wPacket = Sony_wPacket >> 1                ' Shift the bits right 1 place
            Inc Sony_bLoop                                  ' Increment the loop counter
        Until Sony_bLoop >= 11                              ' Close the loop after 12 bits
    '
    ' Split the 7-bit Button, and the 5-bit Device code
    '
        Sony_bButton = Sony_wPacket & $7F                   ' Mask the Button code bits
        Sony_wPacket = Sony_wPacket << 1                    ' Move bit 7 into bit 8
        Sony_bDevice = Sony_wPacket.Byte1 & $1F             ' Mask the Device code bits
        Return
    
    '---------------------------------------------------------------------------------
    ' The main program starts here
    '
    Main:
        Declare All_Digital = 1
        Cls
        Input IR_tSensor                                    ' Make the sensor pin an input
        While                                               ' Create an infinite loop
            Sony_In()                                       ' Receive an IR signal
            If Sony_bDevice = 255 Then Continue             ' Was the header valid?
            Print At  1 ,1,Dec Sony_bDevice,"  "            ' \
            Print At 2,1,Dec Sony_bButton,"  "              ' / Yes So print the button and device values
        Wend                                                ' Do it forever
    This article was originally published in forum thread: Ir Remote interfaced Fan Speed Controller started by Roshan View original post