denny9167
Member level 1
I have an RC5 read function, that I have come up with that is simplified compared to most, any input appreciated
Code:
void readRC5() {
uint8_t address = 0;
uint8_t command = 0;
__bit toggle = 0;
uint8_t count;
// Wait for the IR signal to go low, indicating the start bit
while(IR_PIN == 1);
// Reset and start the timer
TMR1 = 0;
TMR1ON = 1;
// Wait for the start bit period
while(IR_PIN == 0);
if (TMR1 > 2000 || TMR1 < 700) {
TMR1ON = 0;
return; // Error: Invalid timing for the first start bit
}
// Wait for the second start bit
while(IR_PIN == 1);
TMR1 = 0;
while(IR_PIN == 0);
if (TMR1 > 2000 || TMR1 < 700) {
TMR1ON = 0;
return; // Error: Invalid timing for the second start bit
}
// We have both start bits
// Now read the toggle bit
toggle = IR_PIN;
// Read the 5-bit address
for (count = 0; count < 5; count++) {
TMR1 = 0; // Reset timer1 and begin counting
while (IR_PIN == 1); // Wait for the pin to go low for the next bit
while (TMR1 <= 1730); // Wait for next bit period
address <<= 1; // Shift the address to the left to make room for the next bit
if (IR_PIN == 0) // If the IR pin is low, it means its a 1, in IR,logic 1 is now inverted, high to low.
address |= (0x01);// Set the LSB of address
else
address &= ~(0x01); // Clear the bit
}
// Read the 6-bit command
for (count = 0; count < 6; count++) {
TMR1 = 0; // Reset timer1 and begin counting
while (IR_PIN == 1); // Wait for the pin to go low for the next bit
while (TMR1 <= 1730); // Wait for next bit period
command <<= 1; // Shift the command to the left to make room for the next bit
if (IR_PIN == 0) // If the IR pin is low, it means the bit is 1
command |= (0x01); // Set the LSB of command
else
command &= ~(0x01); // Clear the bit
}
// Ensure the timer is stopped
TMR1ON = 0;
// Now address and command contain the decoded RC5 data
// Process the address and command as needed
}