Hi all i have some code for decoding some infrared pulses using an pin change interrupt now i have moved alot further forward with this but only recently noticed a bug in my code where the received data was being obscured slightly which i believe to be due to the way i'm recording the ISR data.
The data sent is as so (2400, 600, 600, 600, 1200, 600, 600, 600, 1200, 600, 600, 600, 1200), after each pulse there is a gap so if you count in odds that is the on pulses, evens are off. Below i am going to give you my serial printout from my code and an early version of my code excuse the mess, this was before i added alot of stuff but thought it better to send some it without unnesersary information.
Reciever Started...
Pulse length: 3013960 - Register: 10010111
Pulse length: 2376 - Register: 10011111
Pulse length: 692 - Register: 10010111
Pulse length: 508 - Register: 10011111
Pulse length: 696 - Register: 10010111
Pulse length: 1108 - Register: 10011111
Pulse length: 692 - Register: 10010111
Pulse length: 548 - Register: 10011111
Pulse length: 624 - Register: 10010111
Pulse length: 1172 - Register: 10011111
Pulse length: 572 - Register: 10010111
Pulse length: 632 - Register: 10011111
Pulse length: 568 - Register: 10010111
Pulse length: 1204 - Register: 10011111
Pulse length: 1849960 - Register: 11111
Pulse length: 28604048 - Register: 10111
Pulse length: 2380 - Register: 11111
Pulse length: 648 - Register: 10111
Pulse length: 552 - Register: 11111
Pulse length: 636 - Register: 10111
Pulse length: 1168 - Register: 11111
Pulse length: 632 - Register: 10111
Pulse length: 568 - Register: 11111
Pulse length: 652 - Register: 10111
Pulse length: 1152 - Register: 11111
Pulse length: 632 - Register: 10111
Pulse length: 568 - Register: 11111
Pulse length: 652 - Register: 10111
you can see to start with i am getting an odd pulse at the beginning and that pulse is a gap pulse where no signal is being recieved this also happens on the next packet i send further down except you have two garbage pulses one where a signal is being recieved the other where there is not. My main issue is this obscures my data as if you look down the first packet and count 600's as 0, 1200's as 1 you will see the pattern is 010101 now look down the second packet and you see 01010 im now missing a bit, i think i could filter this out by excluding pulses longer than x=microseconds but im not sure if that would be a work around rather than modifying the code to stop getting that in the first place, please see if you can get your head around it i have been staring at this screen for the last couple of hours trying.
#define IR_PIN PIND
#define IRPin 2
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //Clear bit in byte at sfr address.
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) //Set bit in byte at sfr address.
volatile long unsigned PrevMicros; //store time of last pulse.
volatile long unsigned eventTime; //Current time.
volatile long unsigned timeSinceLastEvent; //Length of pulse.
int Pulse = 0;
unsigned long bits[15]; //Store lengths here.
byte bin[15]; //store register.
volatile byte portDstatus; //Save port settings.
volatile byte eventFlag; //Set a flag.
void setup() {
Serial.begin(57600);
pinMode(3, INPUT);
digitalWrite(3, HIGH);
pinMode(4, INPUT);
digitalWrite(4, HIGH);
Serial.println("Reciever Started..."); //Let us know its ready.
sbi (PCICR, PCIE2); //enable interrupt PCI2
sbi (PCMSK2, PCINT23); //Set interrupt control bits for pins 7.
sbi (PCMSK2, PCINT22); //Pin 6
sbi (PCMSK2, PCINT21); //Pin 5.
sbi (PCMSK2, PCINT20); //Pin 4.
sbi (PCMSK2, PCINT19); //Pin 3.
}
void loop() {
Get_IR(); //Process any IR.
if(Pulse >= 14 && eventFlag == 0){
for(int i; i < 14; i++){
Serial.print("Pulse length: ");
Serial.print(bits[i]);
Serial.print(" - Register: ");
Serial.println(bin[i], BIN);
}
Pulse = 0;
}
}
int Get_IR(){
if(eventFlag == 1 && (eventTime - PrevMicros > 300) && (eventTime - PrevMicros < 3000)){
timeSinceLastEvent = eventTime - PrevMicros; //Calculate time ISR has been running.
PrevMicros = eventTime; //Remember the time now.
bits[Pulse] = timeSinceLastEvent;
bin[Pulse] = portDstatus;
Pulse++;
eventFlag = 0;
}
}
ISR (PCINT2_vect){
portDstatus = PIND;
eventTime = micros(); //Note time of ISR.
eventFlag = 1;
}