View Single Post
  #1   Spotlight this post!  
Unread 13-01-2008, 13:45
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 588
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Programming the Infrared board

Hi -

I wrote a quick sample program that reads the infrared board connected to 4 digital inputs. The problem is that the pulses are 100ms from the board so it's easy to miss one if your program is in a Wait.

The quick solution was to register a repeating timer - so every 30 milliseconds the code checks the four ports (4, 5, 6, and 7 in this case) and if the value changed from off to on, it sets a corresponding bit in a char variable. This all happens in the interrupt service routine for the timer. Then you can read the values in your code that may not be checking as often.

This particular program is build for Vex, but everything but the header files is the same.

Code:
#include "BuiltIns.h"
#include "ifi_picdefs.h"
#include "Vex_ifi_aliases.h"

volatile static unsigned char irCommands = 0;
volatile static unsigned char previous = 0;

// Timer interrupt service routine
// Poll the remote receiver input ports every 30ms and check if a recognized code
// came in. This will work even if the main program is in a Wait statement or doing
// other things. The IR receiver pulses for 100ms when a valid code is received.

// This will potentially show multiple hits
void CheckIR(void)
{
	unsigned char current = 0;
	unsigned char changed;
	if (rc_dig_in05) current |= 1;
	if (rc_dig_in06) current |= 2;
	if (rc_dig_in07) current |= 4;
	if (rc_dig_in08) current |= 8;
	
	changed = previous ^ current;
	irCommands |= (~previous) & current;
}

// Sample test program to 
void main(void)
{
	RegisterRepeatingTimer(30, CheckIR);
	while (1)
	{
		Wait(4000);
		printf("_____________________\r\r");
		if (irCommands & 1) 
		{
			irCommands &= ~1;
			printf("Command 1\r");
		}
		if (irCommands & 2)
		{
			irCommands &= ~2;
			printf("Command 2\r");
		}
		if (irCommands & 4)
		{
			irCommands &= ~4;
			printf("Command 3\r");
		}
		if (irCommands & 8)
		{
			irCommands &= ~8;
			printf("Command 4\r");
		}
	}
}
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote