| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
Introduction to IR with the STAMP IIThis article will look at using a very simple IR circuit to communicate with your Parallax STAMP II chip. This article is heavily based on some recently released Parallax documentation (see Related Links below). This article will detail infra-red basics and how to utilize the Stamp II's powerful pulsin and freqout commands.
Infra-redInfra-red (IR, infrared) is a term given to the light waves at the longer end of colour spectrum (just beyond red). Infra-red has been used for a long time in remote controls, distance detectors and other similar applications. Below is a simple diagram showing you how to set up the Stamp II to send and receive IR signals:
freqout 7, 1, 38500You can use the freqout commands in varying frequencies to actually detect how far away an object it (roughly, and only to a maximum range of about 15-20 centimetres depending on the environment). Again, the source code and explanation is best left to Parallax who covered it in their excellent IR weekend project. What we are more interested in is the BS2's abilities to read and decode IR signals. IR signals are encoded by pulsing data in specified time slots; for example, a 1.2ms IR burst could signify a binary-1, and a 0.6ms burst a binary-0. This encoding system is the basis for the scheme used by a lot of SONY® TV remote controls. All pulses from a SONY® remote control start with a 2.4ms starting pulse. For example:
CheckStartPulse:
for counter = 0 to 15
pulsin IR_det_pin,active_low,IR_pulse(0)
if IR_pulse(0) > 900 then display_start_bit
next
''''''''''''''''''''''''''''''
' Additional code skipped '
''''''''''''''''''''''''''''''
ProcessIRPulses:
check_for_stop_bit:
pulsin IR_det_pin,active_high,IR_pulse(0)
if IR_pulse(0) > 1400 and IR_pulse(0) <> 0 then continue
goto check_for_stop_bit
continue:
pulsin IR_det_pin,active_low,IR_pulse(0)
pulsin IR_det_pin,active_low,IR_pulse(1)
pulsin IR_det_pin,active_low,IR_pulse(2)
pulsin IR_det_pin,active_low,IR_pulse(3)
pulsin IR_det_pin,active_low,IR_pulse(4)
pulsin IR_det_pin,active_low,IR_pulse(5)
pulsin IR_det_pin,active_low,IR_pulse(6)
pulsin IR_det_pin,active_low,IR_pulse(7)
pulsin IR_det_pin,active_low,IR_pulse(8)
pulsin IR_det_pin,active_low,IR_pulse(9)
pulsin IR_det_pin,active_low,IR_pulse(10)
pulsin IR_det_pin,active_low,IR_pulse(11)
return
Pulsin is a BS2 command that measures the width of a pulse in 2 nanosecond intervals. The pulsin command also takes a variable that specifies whether to trigger on a binary-0 to binary-1 transition or vice versa. The 0 is passed, pulsin measures the time for the pulse to change from a 0 to a 1 (a trough on the graph), a 1 measures the time from a 1 to 0 (peak on the graph). Obviously, the final parameter is the variable to use to store the value.
Knowing this, the code above is fairly simple. The CheckStartPulse code just iterates looking for the starting pulse, which is assumed to be larger than 1.8ms (although is typically around 2.4ms). Note that the display code is skipped. In ProcessIRPulses, all the initial segment of code does is check to see whether the message has overrun and has begun to repeat. Notice that the initial pulsin call uses "active_high" (defined as 1), this causes the pulsin command to measure the time it takes for a binary-1 to change to a binary-0 (a peak on the graph). Note that value of 1400 is 2.8ms (remember, pulsin returns a value in 2 nanosecond intervals) will put you in the middle of the start pulse, making it optimal to receive the message. This is probably the most complicated part of the code, since it isn't immediately apparent what is happening. As you can see, the rest of the code involves retrieving the remaining 11 bits. Note that there is no code between the calls since any additional instructions may cause bits to be lost, since this is all fairly time-dependent (as little as 20 nanoseconds). Utilizing IR SignalsThe original idea for this was to create a robot that I could control using a remote control, but I ran out of wire to hook up the motors and the IR stuff, so I reverted to remote controlling my PC. To do this, I used a Marmitek X-10 remote control set to mimick a Sony VCR (to use the Play, Stop, Rew, Fwd buttons). I then hooked the BS2 to the my PC via the serial port (see this article), and send the IR message number to the computer. The PC was running a program that monitored the serial port for specific commands and executed the corresponding commands. Overall, the system worked well, although I had problems controlling the DVD software).
Submitted: 11/05/2001 Article content copyright © James Matthews, 2001.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -