$regfile = "m8def.dat"
$crystal = 8000000                                          'Internal RC oscillator 8 MHz

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db7 = Portb.3 , Db6 = Portb.2 , Db5 = Portd.6 , Db4 = Portd.7 , E = Portb.5 , Rs = Portb.4
Config Pinb.0 = Input

Config Timer1 = Timer , Prescale = 256 , Capture Edge = Rising , Compare A No_output = Disconnect
Config Serialout = Buffered , Size = 150

On Capture1 Capture1_int
On Compare1a Compare1a_int

Dim Ready As Byte
Dim Address As Word
Dim Code As Word
Dim Startbitperiod As Byte
Dim Normalbitperiod As Byte

' If you change the XTAL frequency then you will need to alter these 3 values
Startbitperiod = 200
Normalbitperiod = 45

' Set the Compare threshold to detect end of packet after 4 bit times of no activity
Compare1a = Normalbitperiod * 4

Enable Interrupts
Enable Capture1
Enable Compare1a

Cls
Lcd "Sony IR Receiver"

Do
 If Ready <> 0 Then
 Ready = 0

 Lowerline
 Lcd "Add." ; Address ; " Code: " ; Code ; " "

 End If
Loop

End

Dim Period As Word
Dim Command As Long
Dim Bitcount As Byte
Dim Shiftcount As Byte

Compare1a_int:
' Uncomment the line below to see the bit patterns to debug new
' command sizes not allowed for in the code
'
' Print "B " ; Bitcount ; " C " ; Bin(command)

 Ready = 1

 If Bitcount = 12 Or Bitcount = 15 Or Bitcount = 20 Then
 Shiftcount = 31 - Bitcount
 Shift Command , Right , Shiftcount
 Else
 Ready = 0

 End If

 Code = Command And &H0000007F

 Shift Command , Right , 7

 Address = Command

 Bitcount = 0

 Return

Capture1_int:
 Timer1 = 0

 Period = Capture1

 If Period >= Startbitperiod Then
 Command = 0
 Bitcount = 0

 Else
 If Period > Normalbitperiod Then
 Command = Command Or &H80000000
 End If

 Shift Command , Right

 Incr Bitcount
 End If

' Uncomment this line to print out the bit times see how to tweak the
' Period thresholds if you change the XTAL frequency
'
' Print "P " ; Period ; " B " ; Bitcount

 Return