You could just implement the Parallax PULSIN function in PIC18 C or Easy C and use it to read the pulse trains using the PIC18F8520’s CCP (Capture Input) peripheral. Here is some code that uses the CCP hardware to read Sony SIRC IR Signals from a TV Remote Control that worked on a PIC18F452 MCU, which could be modified to run on a PIC18F8520 MCU used on the Vex Controller.
.
.
.
#define SYSTEM_FREQUENCY 20 // System frequency, Fs = 20 MHz
// Compute timer clock frequency which depends on the system frequency. This constant is
// used by DelayUs function.
#define CLOCK_FREQUENCY (SYSTEM_FREQUENCY / 4) // Fc = Fs / 4
#define TIME_SCALE_FACTOR 100 // 100 micro-second granularity for delays
// Define IR remote control constants for the PIC18C452 at the System frequency, Fs of 20 MHz here
#define T 426 // Delay of 0.55 milli-seconds.
#define Zero T // Delay 0.55 milli-seconds for sending a “0”
#define One 1114 // Delay 1.10 milli-seconds for sending a “1”
#define Leader 2497 // Delay 2.20 milli-seconds for sending a leader
#define Sync T // Delay 0.55 milli-seconds for sending a sync
#define T_400_Us (400 / TIME_SCALE_FACTOR * CLOCK_FREQUENCY) // 0.40 milli-second (400 micro-second) delay
#define T_600_Us (600 / TIME_SCALE_FACTOR * CLOCK_FREQUENCY) // 0.60 milli-second (600 micro-second) delay
#define T_1200_Us (1200 / TIME_SCALE_FACTOR * CLOCK_FREQUENCY) // 1.20 milli-second (1200 micro-second) delay
#define T_2400_Us (2400 / TIME_SCALE_FACTOR * CLOCK_FREQUENCY) // 2.40 milli-second (2400 micro-second) delay
#define TRUE 1 // Boolean TRUE value
#define FALSE 0 // Boolean FALSE value
#define INPUT 1 // Input pin direction
#define OUTPUT 0 // Output pin direction
// // Port C Bits
#define IRDetectorPin 17 // IR detector Pin (RC2/CCP1)
// PORT D Bits
#define LeftEmitter PORTDbits.RD0 // Left IR emitter
#define RightEmitter PORTDbits.RD1 // Right IR emitter
//********************************************************************************
// Type definitions
//********************************************************************************
typedef volatile union
{
byte Value;
struct
{
unsigned Bit0:1; // LSB
unsigned Bit1:1;
unsigned Bit2:1;
unsigned Bit3:1;
unsigned Bit4:1;
unsigned Bit5:1;
unsigned Bit6:1;
unsigned Bit7:1; // MSB
} ;
} UnsignedByte_T ;
typedef volatile union
{
word Value;
struct
{
byte Byte0; // LSB
byte Byte1; // MSB
} ;
} UnsignedWord_T ;
//********************************************************************************
// Variable declarations
//********************************************************************************
UnsignedByte_T ByteValue;
UnsignedWord_T WordValue;
byte Byte_1;
word Word_1;
byte TheCommand = 0; // The IR remote control IRCommand read from host
extern char Ch; // Character read from USART
extern byte HexNumber]; // Hex string
byte SonyDevice = 0; // Current Sony IR Device code received from TV remote control
byte SonyCommand = 0; // Current Sony IR Command code received from TV remote control
//********************************************************************************
// Function Prototypes
//********************************************************************************
// IR Remote Control tests
void InitializeSonyRC(void); // Initialize Sony remote control receiver variables
void ReceiveIRCommand(byte *Device, byte *Command); // Receive Sony TV remote control commands and device codes
//********************************************************************************
//* main - Receives Sony IR codes from TV remote control and displays the
//* Command and device code.
//********************************************************************************
void main(void)
{
unsigned int result;
char str[7];
// Configure Capture1
//OpenCapture1(C1_EVERY_4_RISE_EDGE&CAPTURE1_CAPTURE);
// Configure Capture1 for falling edge (1 -> 0 transition)
OpenCapture1(C1_EVERY_FALL_EDGE);
// Configure Capture1 for rising edge (0 -> 1 transition)
//OpenCapture1(C1_EVERY_RISE_EDGE&CAPTURE1_CAPTURE);
// Configure Timer3
OpenTimer3(TIMER_INT_OFF&T3_SOURCE_INT);
// Configure USART for 9600 Baud at 4 MHz
OpenUSART(USART_TX_INT_OFF&USART_RX_INT_OFF&
USART_ASYNCH_MODE&USART_EIGHT_BIT&
USART_CONT_RX, 25);
// Display message to operator
printf(“PIC18C452 Sony IR TV Remote Control Application \r\n”);
while (TRUE)
{
//printf(“trace 1: \r\n”);
CapStatus.Cap1OVF = 0;
while(!PIR1bits.CCP1IF); // Wait for event
result = ReadCapture1(); // read result
if(!CapStatus.Cap1OVF)
{
printf("trace 3: result = ");
hex4(result);
printf("\r\n");
CloseCapture1(); // to USART
}
}
CloseTimer3();
CloseUSART();
}
//******************************************************************************
//* ReceiveIRCommand - Receive the Sony IR command and device codes transmitted
//* from the Sony TV remote control and return them to the caller, using the
//* Sharp IR receiver. The device code is currently ignored in this version.
//******************************************************************************
void ReceiveIRCommand(byte *Device, byte *Command)
{
word IRWord[12]; // Array containing IR signal counts
byte BitValue; // Bit value (1 or 0)
//byte Level = 1; // 1 to 0 transition
byte Level = 0; // 0 to 1 transition
byte i; // Loop index
// Get latest IR command from Sony TV remote
printf(“trace 4: \r\n”);
do
{
pulsin(IRDetectorPin, Level, &IRWord[0]); // Wait for valid start bit at end of leader (2.2 milli-seconds)
}
while (IRWord[0] < Leader);
pause(20); // Wait 20 milli-seconds to allow data to settle
printf(“trace 5: \r\n”);
//do
//{
// pulsin(IRDetectorPin, Level, &IRWord[0]); // Wait for valid start bit at end of leader (2.2 milli-seconds)
//}
//while (IRWord[0] < Leader);
//pause(20); // Wait 20 milli-seconds to allow data to settle
// Get IR data (7-Bits)
pulsin(IRDetectorPin, Level, &IRWord[0]); // Wait for third valid start bit
printf(“trace 6: \r\n”);
// Read each data bit using inline code. for loops are too slow
pulsin(IRDetectorPin, Level, &IRWord[1]); // Get bit 0
pulsin(IRDetectorPin, Level, &IRWord[2]); // Get bit 1
pulsin(IRDetectorPin, Level, &IRWord[3]); // Get bit 2
pulsin(IRDetectorPin, Level, &IRWord[4]); // Get bit 3
pulsin(IRDetectorPin, Level, &IRWord[5]); // Get bit 4
pulsin(IRDetectorPin, Level, &IRWord[6]); // Get bit 5
pulsin(IRDetectorPin, Level, &IRWord[7]); // Get bit 6
printf(“trace 7: \r\n”);
// Determine if bit is one or zero by comparing it to thresholds and set
// corresponding bit in the command word.
*Command = 0;
for (i=0; i<7; i++)
{
if (IRWord* > One)
{
BitValue = 1;
}
else
{
BitValue = 0;
}
// Set the appropriate bit
switch (i)
{
case 0:
{
ByteValue.Bit0 = BitValue;
}
case 1:
{
ByteValue.Bit1 = BitValue;
}
case 2:
{
ByteValue.Bit2 = BitValue;
}
case 3:
{
ByteValue.Bit3 = BitValue;
}
case 4:
{
ByteValue.Bit4 = BitValue;
}
case 5:
{
ByteValue.Bit5 = BitValue;
}
case 6:
{
ByteValue.Bit6 = BitValue;
}
case 7:
{
ByteValue.Bit7 = 0;
}
}
}
*Command = ByteValue.Value;
}*