PDA

View Full Version : RC5 decode on a 10F + Question



ultiblade
- 10th September 2008, 12:38
Hi all,

I needed to build a philips RC5 IR decoder, in order to be able to control some other circuitry. When a predefined key is received an decoded the corresponding IO is made high.

However, the example source code's if found could not fit a 10F202.

I used a TSOP(TK)1836 connected to GPIO.0, and in order to "see" the values received the pic sends it's data trough a max232 to my pc. This is only for debug purposes, and must be removed later (removing the moddefs include and serout will free up lots ofprogram memory).



'==== Set fuses ================================================== ==============
@ device pic10F202,wdt_off,mclr_off,protect_off

'==== Set includes ================================================== ===========
INCLUDE "modedefs.bas" ' Include for Serout

'==== Set XTAL ================================================== ===============
DEFINE OSC 4 ' int XTAL @ 4 Mhz

'==== Set variables ================================================== ==========
Y VAR word ' To Hold the 12-bit RC5 code


'==== Set IO ================================================== =================
IR_PIN VAR GPIO.0 ' GPIO.0 input pin reading IR data
SERIAL var GPIO.1 ' GPIO.1 SEROUT 9600-8-n-1 output
TRISIO = %00001001 ' Set TRIS register input's & output's
OPTION_REG.7 = 0 ' Internal pull-ups = on

'==== Signal information ================================================== =====
'Given the information found on http://www.sbprojects.com/knowledge/ir/rc5.htm
'The incoming RC5 signal will look like this :
'
'bit 1 2 3 4 5 6 7 8 9 10 11 12 13 14
' | | | | | | | | | | | | | | |
'uSec 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778

'==== Main program ================================================== ===========
MAIN:
IF IR_PIN = 1 THEN GOTO MAIN ' Wait for the first bit to arive
y.13 = IR_PIN ' Incoming signal
pauseus 889 ' In order to se this 0 we are in the second
' period of the first 1.778 msec, so we will
' wait another 889 usec to enter the 2nd period.

pauseus 1600 ' Almost at the end of the second period, look for
y.12 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.11 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.10 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.9 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.8 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.7 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.6 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.5 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.4 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.3 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.2 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.1 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.0 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

y = ~y ' Invert y word
if Y.lowbyte = 255 then goto ERROR ' If all lowbytes are 0xFF then noise is received

'==== DEBUG , output received data serial ======================================
Serout serial,T9600,["STX= ",#y.13,#Y.12,#Y.11,10,13]
Serout serial,T9600,["SYS= ",#y.10,#y.9,#y.8,#y.7,#y.6,13,10]
Serout serial,T9600,["CMD= ",#Y.5,#y.4,#y.3,#y.2,#y.1,#Y.0,13,10]

'==== End of main loop reset and look again ====================================
ERROR:
Y=0 ' Clear key codes
PAUSE 250 ' debounce
GOTO MAIN ' Return to main loop


My problem now is:
The variable Y is a WORD the has bit's 0 to 13 stored with info.
How do i break up the word in to 3 sections ?

bits 0 to 2 '000' are for the S1, S2 and toggle bit
bits 3 to 7 '00000' are for the System address
bits 8 to 13 '000000' are for the command bits

I want to end up with 2 variables SYSTEM(5 bits) and COMMAND(6 bits)

Anyone ?
(Also, comments on my code are very welcome !!!)

Thanks !

skimask
- 10th September 2008, 13:48
My problem now is:
The variable Y is a WORD the has bit's 0 to 13 stored with info.
How do i break up the word in to 3 sections ?

bits 0 to 2 '000' are for the S1, S2 and toggle bit
bits 3 to 7 '00000' are for the System address
bits 8 to 13 '000000' are for the command bits

I want to end up with 2 variables SYSTEM(5 bits) and COMMAND(6 bits)


Simple bit level manipulation as described in the PBP manual...i.e.
temp var byte
temp.bit0 = y.bit0
temp.bit1 = y.bit1
....and so on and so on...

ultiblade
- 10th September 2008, 14:00
Ahhh, a classic case of RTFM ! You are so right !

I tried it but it seems i cannot declare another byte.

CMD VAR BYTE
ERROR: Unable to fit variable CMD

Even with the modedefs include removed .....

Is there a way to free up some program memory ?

The 10F202 has 24 bytes of data memory, so why can't i fit 1 word and 1 byte ?


Thanks !

skimask
- 10th September 2008, 14:18
Yes, get rid of the MODEDEFS...
Other tips that might help out...
Use DEBUG instead of SEROUT
Instead of inline code, grab the bits in a loop.
Not sure if it'll save anything, you've got pauseus and pause. Maybe just use a big pauseus rather than both commands.
Treat your Y variable as a word initially, then when you changing it up, treat it as 2 bytes, y.high and y.low. Might have to declare a couple of smaller bit variables (maybe scavenge a bit from a register from a module you're not using, like maybe a timer that's not running) while doing the bit swapping.
This might be one of those 'codius minimus' projects...DT? Anyone else?

ultiblade
- 10th September 2008, 15:01
Hi Skimask,

Thanks for your fast reply, will try the things you indicated.

I also found that commenting out the RR1, RM1 and flag section in pbpPIC12.ram gives
an extra 3 bytes !

Thanks !

Darrel Taylor
- 10th September 2008, 21:35
I don't think I'd ever try PBP with a 10F ... just not enough room to spare.
Much better to use ASM.

Here's one from Bruce.

PIC10F200 Automated IR Light Switch
http://www.picbasic.co.uk/forum/showthread.php?t=3261

Doesn't do what you want, but it shows what you can do with a 10F200 and ASM.
<br>

Darrel Taylor
- 10th September 2008, 23:46
ultiblade,

I just compiled your code for a 10F202 with PBP 2.50, and it all fit's. No Errors.
I did not change the .RAM file.

Which version of PBP do you have?
<br>

ultiblade
- 11th September 2008, 08:13
I don't think I'd ever try PBP with a 10F ... just not enough room to spare.
Much better to use ASM.

Here's one from Bruce.

PIC10F200 Automated IR Light Switch
http://www.picbasic.co.uk/forum/showthread.php?t=3261

Doesn't do what you want, but it shows what you can do with a 10F200 and ASM.
<br>

Amazing ! Missed that, i was only searching for 'RC5' examples ...
Maybe coming wintertime will bring me a new hobby, learning Assembly !

I'm using pbp2.47. Yesterday i made some code changes, but it still won't fit without
the ram file change. I commented out only the FLAG parameter, that does the trick.

ultiblade
- 11th September 2008, 08:20
Here's the code until now, works perfect with a RC5 remote. It now act's like a sort of a IR RC5 to binary encoder. Just press the number buttons 0 to 7 on the remote and the 3 IO outputs will be set accordingly ... (Just one minor thing, still remains : the FLAG section has to be commented out in the pbppic12.ram file, for me to be able to fit TMP as byte)




'==== Set fuses ================================================== ===
@ device pic10F202,wdt_off,mclr_off,protect_off


'==== Set XTAL ================================================== ===
DEFINE OSC 4 ' int XTAL @ 4 Mhz

'==== Set variables ==================================================
Y VAR word ' To Hold the 12-bit RC5 code
TMP var BYTE

'==== Set IO ================================================== =====
IR_PIN VAR GPIO.3 ' GPIO.0 input pin reading IR data
'SERIAL var GPIO.1 ' GPIO.1 SEROUT 9600-8-n-1 output
RES1 var GPIO.0 ' Binary 1
RES2 var GPIO.1 ' Binary 2
RES3 var GPIO.2 ' Binary 4

OPTION_REG.7 = 0 ' Internal pull-ups = on
TRISIO = %00001000 ' Set TRIS register input's & output's
OPTION_REG.5 = 0 ' Set Timer inc. on internal inst.
GPIO = %00000000 ' Set all digital low

'==== Signal information ===============================================
'Given the information found on http://www.sbprojects.com/knowledge/ir/rc5.htm
'bit 1 2 3 4 5 6 7 8 9 10 11 12 13 14
' | | | | | | | | | | | | | | |
'uSec 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778 1778

'==== Main program ==================================================
MAIN:
IF IR_PIN = 1 THEN GOTO MAIN ' Wait for the first bit to arive
y.13 = IR_PIN ' Incoming signal
pauseus 889 ' In order to se this 0 we are in the second
' period of the first 1.778 msec, so we will
' wait another 889 usec to enter the 2nd period.

pauseus 1600 ' Almost at the end of the second period, look for
y.12 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.11 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.10 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.9 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.8 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.7 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.6 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.5 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.4 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.3 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.2 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.1 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

pauseus 1600 ' Almost at the end of this period, look for
y.0 = IR_pin ' a high or low signal
pauseus 178 ' Time to the end of this period

y = ~y ' Invert y word

if Y.lowbyte = 255 then goto ERROR ' If all lowbytes are 0xFF then noise is received


'==== Parse received values =========================================
TMP.1 = Y.13
TMP.0 = Y.12
if not tmp = 3 then goto error

TMP.4 = Y.10
TMP.3 = Y.9
TMP.2 = Y.8
TMP.1 = Y.7
TMP.0 = Y.6
if not tmp = 0 then goto error

TMP.2 = Y.5
TMP.1 = Y.4
TMP.0 = Y.3
if tmp > 0 then goto error

TMP.2 = Y.2
TMP.1 = Y.1
TMP.0 = Y.0

OPTION_REG.5 = 0 ' Internal pull-ups = on
res3 = tmp.2
res2 = tmp.1
res1 = tmp.0

'==== End of main loop reset and go from top ============================
ERROR:
Y=0
tmp=0 ' Clear key codes
PAUSE 250 ' debounce
GOTO MAIN ' Return to main loop

END


And again: THANK YOU guys !

Best Regards, UltiBlade