# Trying to catch IR pulses swordfish basic

Status
Not open for further replies.

#### be80be

##### Well-Known Member
Pic basic pro has pulsin command that can count pulses. Like from remote controllers. Atom did some code for the sony remote He used delays to time how fast it checked for change in state.

So I tried this code i made and it look like it is catching the code. I just wanted some thoughts about it. Thanks
Code:
{
*****************************************************************************
*  Name    : IRsony.bas                                                   *
*  Author  : burt ratliff                                 *
*  Notice  : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS]              *
*  Date    : 6/9/2009                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F1320
Clock = 8 // tells the compiler the FOSC speed
Config OSC = INTIO2, WDT = OFF, LVP = OFF, MCLRE = OFF
Include "Utils.bas"
Include "INTOSC8.bas"
Dim IrIn As PORTB.0
Dim led As PORTA.0
Dim IrTime As Byte
dim code as word
SetAllDigital
TRISB =%00000001
PORTB =%00000000
TRISA =%00000000
PORTA =%00000000
IrTime = 0
While true
IrIn =0            //sets IRmodule to low
Repeat
IrIn = 1           //makes sure we start high
Until IrIn = 0     // If low we are at start of a pulse

Repeat IrIn = 0    //repeats till the end of the start pulse
DelayUS(200)
Inc (IrTime)
Until IrTime >= 10    //make sure its start is2.4mS
If IrTime >= 10 Then
EndIf
IrTime = 0        // reset to get code
repeat
if IrIn =0 then                // checks for 0
delayus(200)
Inc (IrTime)                       //
endif
until IrTime >= 3                     //checks to see if it is .600mS long
repeat
If IrIn = 1 and IrTime >=3 then        //
code = 0                          // writes to code a 0
elseif IrIn =0 and IrTime >=3 then     // checks for a 1
delayus(200)
inc (IrTime)
endif
until IrTime >=6                      //makes sure it's 1.2mS
if IrIn =1 and IrTime >=6 then
code =1                          // writes to code a 1
endif
porta.0 = code                      // just outputting to led to see how it working
Wend
End
It will run on the junebug it blinks led1 and 6 as output.

Last edited:

#### be80be

##### Well-Known Member
The code catches the pulses but it outputting it slower then it is reading lol. Or it's just catching the start pulse and that it. Back to the drawing board.

#### Pommie

##### Well-Known Member
Hi Bert,

I wrote this module last year. It does pretty much the same as yours except it has a function to receive a pulse.
Code:
{
*****************************************************************************
*  Name    : IR.BAS                                                         *
*  Author  : Mike Webb.                                                     *
*  Notice  : Copyright (c) 2008 None                                        *
*          : No  Rights Reserved                                            *
*  Date    : 4/03/2008                                                      *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Module IR

Function GetPulse()As Byte
GetPulse=0
While(PORTB.0=1)
Wend
While(PORTB.0=0)
DelayUS (100)
GetPulse=GetPulse+1
Wend
End Function

Public Sub ReadIR(ByRef pCMD As Byte,ByRef pDEV As Byte)
Dim I As Byte
Dim Pulse As Byte
Dim Error As Boolean
ADCON1.4 = 1                //ensure Portb.0 is digital
TRISB.0=1                   //and is input
Repeat
error=false
pCMD=0
pDEV=0
Repeat
Pulse=GetPulse()
Until pulse>20              //wait for start pulse > 2.0mS
For I=0 To 6                //get 7 bit command code
If error=true then      //if it errored then
Break               //exit the for next loop
endif
pCMD=pCMD>>1
Pulse=GetPulse()        //get pulse length
If Pulse > 15 Then      //if greater than 15 is error
error=true
ElseIf pulse > 9 Then   //if between 10 and 15 is a 1 bit
pCMD.bits(6)=1
ElseIf pulse < 4 Then   //if less than 4 is error
error=true
EndIf
Next
For I=0 To 4                //get 5 bit device code
If error=true then
Break
endif
pDEV=pDEV>>1
Pulse=GetPulse()        //as above
If Pulse > 15 Then
error=true
ElseIf pulse > 9 Then
pDEV.bits(4)=1
ElseIf pulse < 4 Then
error=true
EndIf
Next
Until error=false
End Sub

Mike.

#### be80be

##### Well-Known Member
Hi Mike
Here what I catching. I think i need some error checking Like your code Mike. Thanks for the post. I don't no if I'm getting better at this or not But I think I am lol. I'm printing your code out so i can get a good look at> Here what i get with the logic tool.

Last edited:

#### be80be

##### Well-Known Member
All three of them are from the sony remote but the timing is off so the only thing it's for sure catching is the start pulse and some of the 1's and 0's after but my code has no error checking. I just was trying to catch pulses. If you click the run on the logic tool and press the remote at the right time it catches the button code. But that's hard to do.

#### Nigel Goodwin

##### Super Moderator
All three of them are from the sony remote but the timing is off so the only thing it's for sure catching is the start pulse and some of the 1's and 0's after but my code has no error checking. I just was trying to catch pulses. If you click the run on the logic tool and press the remote at the right time it catches the button code. But that's hard to do.

It's absolutely dead easy to do - set the logic tool to the correct timebase and sync (negative going), and simply press the Sony button, it captures the entire waveform perfectly.

#### AtomSoft

##### Well-Known Member
you are setting it up wrong then i have captured from my remote no issue... you have to setup your receiver output pin to CH3.

Once you do set it to capture on LOW and press RUN and then you should be able to press the button at anytime you want and capture it like this:

EDIT: remember the low pulses are the data...

#### Attachments

• ir.png
16.3 KB · Views: 374
Last edited:

#### be80be

##### Well-Known Member
Lol I'm not reading from the IR module I'm reading what the pic catches and would be saving I'm just resending what it catches And I just use the sony remote to have a signal to catch. Maybe you can see if I draw you a pic.
This is what you get from the IR module

I no how that works
What I posted Is what the pic catches Like I said I was not checking for errors If I press run and the remote key at the right time it catches it right If not it errors and just get's the start LOL

Last edited:

#### AtomSoft

##### Well-Known Member
Dim IrIn As PORTB.0
Dim led As PORTA.0

lol backwards maybe?

Dim IrIn As PORTA.0
Dim led As PORTB.0

#### be80be

##### Well-Known Member
Look one more time I Have it right I posted it over I put it on the wrong port the first time lol
lol backwards maybe?
if it was backward I wouldn't have got any output lol

Last edited:

#### AtomSoft

##### Well-Known Member
im looking at it now and it still seems backwards lol

The Infrared on schematic is RA0
The Logic is RB0 on schematic.

Dim IrIn As PORTB.0
Dim led As PORTA.0

#### AtomSoft

##### Well-Known Member
ok are you saying IRIN is the LOGIC?

Anyway its easier to just :

Code:
While true
led =  IrIn
End

#### be80be

##### Well-Known Member
Whats that going to do just send it back out LOL.
Atom I post it right. But any way I'm not just reading from the logic tool. I don't want to just send it back out I want to catch it so i can save it. I just sent what the pic is receiving out portA.0 to see if it matches. It's not going to do me any good to just send it back out a pin and not save it. Can't save it if you don't catch it LOL. Mike showed me what I was missing I had no error checking in my code The code works. And so dos my setup. It just don't get all bits if it errors I fixed that.

#### Attachments

• mylo.PNG
8.3 KB · Views: 107

#### AtomSoft

##### Well-Known Member
oh ok You just confused me like heck with IRin being IRout really lol

Ok glad to here it works...

Status
Not open for further replies.

Replies
6
Views
2K
Replies
68
Views
2K
Replies
0
Views
1K
Replies
26
Views
3K
Replies
18
Views
2K