unsigned char DecodeBit(unsigned char time)
This will determine if the bit is a 0 or a 1,
by whichever standard is in being used.
Returns a 1 if a logic 1 bit, 0 otherwise.
unsigned char ret = 0;
unsigned char val = 0;
val = time;
//Can be used to “fuzzy”
//the time for rounding
if (whichOne == NEC)
if ((val <= NEC_ONE+FUZZY) &&
(val >= NEC_ONE-FUZZY))
ret = 1;
else if ((val <= NEC_ZERO+FUZZY) &&
(val >= NEC_ZERO-FUZZY))
ret = 0;
else if (whichOne == SONY)
if ((val <= SONY_ONE+FUZZY) &&
(val >= SONY_ONE-FUZZY))
ret = 1;
else if ((val <= SONY_ZERO+FUZZY) &&
(val >= SONY_ZERO-FUZZY))
ret = 0;
Figure 6. A selection of IR remotes.
There is more magic in DecodeCode() that shows how
to recognize the start of a transmission and how to end
one. This little routine above simply shows the decoding of
a single data bit. Notice the + and – FUZZY settings. IR
specs allow for about a 10% slop in the standard times.
This FUZZY setting gives us that. You can experiment with
how large you want that FUZZY to be since it is a #define
at the top of the program. I’ve found that a setting of 2
My code will “auto” detect NEC
code if you hold the button down.
This is because NEC uses the repeat
code frame that is distinct from any
other transmission. You don’t have to
do that if you don’t want to, and
you’ll need to reset the PIC to get it to
pay attention to SONY codes again
regardless. I have two modes of
operation that can be selected by the
setting of the TEST define. If this is set
to 1, then the program will only print
out the times. This is useful for when
you are trying to understand a new
format. If TEST is set to anything else,
then the program will decode either
SONY or NEC, and print out the
device and control codes when they
are received. The entire program
source can be found on the SERVO
It is called IRdecoder.c.
Figure 6 shows my collection of IR remotes that I used
to test my program. I found interesting departures from the
established standards in some of them; no doubt you will, too.
I’ve given you a powerful tool that you can use to
discover IR codes and a basic template that you can use
to embed the ability to control your robot using a common
household device: the IR remote. Have fun and be creative.
If you have any questions about this program or how I
“figured it all out,” send your questions to roboto@servo
magazine.com — I’m happy to answer! SV
NEWS FLASH! At the last possible moment I discovered
this site. It is an excellent compendium of various IR
remote formats: www.rhoads.nu/bjorn/hp48/remote.
SERVO 07.2008 21