PDA

View Full Version : 40KHz? Are you receiving me?



CumQuaT
- 28th September 2009, 01:45
Hi everyone,

Has anyone used a PIC (coded with PicBasic Pro) to accurately receive a 40KHz infrared signal before? I'm not sure wether it is do-able or not, so I figured I'd ask you fine people to see if anyone's done it before and if so, what would the code be?

Just to give more info on what I'm doing, the signal I'm trying to receive is as follows:

3ms on
6ms off
3ms on
2ms off
1ms on
2ms off
1ms on
2ms off
1ms on
2ms off
1ms on
2ms off
1ms on
2ms off
1ms on
2ms off
1ms on

So yeah, any help would be awesome. Thanks.

DaveC3
- 28th September 2009, 02:39
Here is a IR remote code I used way back for a Remote Robot using a Sony remote control.



Hope it helps




define LOADER_USED 1


DEFINE OSC 20
start:
ADCON1 = 7
'angleVar VAR BYTE ' set up constants with the minimum and maximum pulsewidths
'SERVO1 CON 450
'SERVO2 CON 1000 ' set up a constant with the time between pulses:
refreshPeriod CON 20
A VAR BYTE ' set an initial pulsewidth:
'angleVar = minAngle
TRISB = 0
TRISA = %00000100
P_VAL VAR WORD
IR_DEV VAR BYTE
IR_BUT VAR BYTE
HEADER VAR WORD
BITCNT VAR BYTE
PACKET VAR WORD
DIRINF VAR BYTE

SEROUT2 PORTC.6,32,["START " , 10,13]
main:
GOSUB IRIN
SELECT CASE IR_BUT
CASE $10
GOSUB LEFT
CASE $11
GOSUB RIGHT
CASE $12
GOSUB FWD

CASE $13
GOSUB RE

CASE $14
GOSUB ST

CASE ELSE

END SELECT


GoTo main
'Recieve a signel from a Sony remote control
' and return with a 7 bit button code
' IR_BUT and a 5 bit device code IR_DEV in the variable IR_DEV
' if no header then IR_DEV and IR_BUT hold 255

IRIN: IR_DEV = 255:IR_BUT = 255
PULSIN PORTA.2,0,HEADER 'Measure header
IF HEADER < 1000 THEN RETURN
IF HEADER > 1350 THEN RETURN ' VERIFY GOOD HEADER IF BAD RETURN
FOR BITCNT = 0 TO 11
PULSIN PORTA.2,0,P_VAL
IF p_VAL >= 450 THEN
PACKET.0[BITCNT] = 1
ELSE
PACKET.0[BITCNT] = 0
ENDIF
NEXT
'Split the 7 bit button code and the 5 bit device code
IR_BUT = PACKET & %01111111

IR_DEV = (PACKET>>7) & %00011111
SEROUT2 PORTC.6,32,["BUTTON =", HEX IR_BUT, 10,13]
RETURN


FWD:
FOR A = 1 TO 20
Low PORTB.7
Low PORTB.6
' pulse the pin
PulsOut PORTB.7, 450
PulsOut PORTB.6, 1000
' pause for as long as needed:
Pause refreshPeriod
NEXT A

RETURN

RE:
FOR A = 1 TO 20
Low PORTB.7
Low PORTB.6
' pulse the pin
PulsOut PORTB.7, 1000
PulsOut PORTB.6, 450
' pause for as long as needed:
Pause refreshPeriod
NEXT A
RETURN
ST:
Low PORTB.7
Low PORTB.6
'PAUSE 1000
RETURN

LEFT:
FOR A = 1 TO 20
Low PORTB.7
Low PORTB.6
' pulse the pin
PulsOut PORTB.7, 1000
PulsOut PORTB.6, 1000
' pause for as long as needed:
Pause refreshPeriod
NEXT A
RETURN


RIGHT:
FOR A = 1 TO 20
Low PORTB.7
Low PORTB.6
' pulse the pin
PulsOut PORTB.7, 450
PulsOut PORTB.6, 450
' pause for as long as needed:
Pause refreshPeriod
NEXT A
RETURN

Kamikaze47
- 28th September 2009, 02:44
You should be able to use the PULSIN command to measure the length of the pulses, but we will need more info to give further help.

I assume you have an IR receiver hooked up to one of your I/O pins?

Is the IR signal coming from a remote control? If so, what is the remote for, and what brand is it? Do you know the encoding scheme its using?

comwarrior
- 28th September 2009, 10:07
ET phone home?

ardhuru
- 28th September 2009, 10:29
From the figures you have given, it looks like the modulation frequency might be 40 kHz; the actual data is slower. So, as long as your IR receiver module can handle 40 kHz, decoding the data (pulse widths and spaces), which is in milli seconds, should most certainly be within the scope of PBP.

Depending on the range you expect, even a generic 36 - 38 kHz IR module should work fine at 40 kHz.

Anand

CumQuaT
- 29th September 2009, 00:51
Hi all,

Thanks for the responses. Sorry it takes me so long to respond. I'm in Australia, so with the time differences all of your responses come in while I'm asleep.

I'm using a 40KHz phototransistor to receive, yes, and interfacing it to a PIC16F628A oscillating at 4MHz.

Thanks for the example code, DaveC3. It's not a remote control that I'm trying to read from, but I imagine it's a similar process across the board. I'll pick that code apart and see what I can come up with.