pepijndevos
Member
On my Teensy 3.1 I have one interrupt that runs a lot which sets a flag for the main loop to look at some value.
So the pin goes low, the flag gets set, data is read, the pin goes back up.
This works great, except when other interrupts fire.
So I turn the encoder, and the value gets updated.
But these two things together makes the first one miss sometimes. So then the flag is not set, no data is read, the pin stays low. BAD.
What can I do to not miss interrupts?
Code:
// optical sensor
pinMode(9, INPUT);
attachInterrupt(9, pointer_moved, FALLING);
So the pin goes low, the flag gets set, data is read, the pin goes back up.
Code:
volatile boolean mouse_has_data = true;
void pointer_moved() {
mouse_has_data = true;
}
This works great, except when other interrupts fire.
Code:
// rotary encoder
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
attachInterrupt(16, read_encoder, CHANGE);
attachInterrupt(17, read_encoder, CHANGE);
So I turn the encoder, and the value gets updated.
Code:
volatile int scroll_steps = 0;
void read_encoder() {
int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
uint8_t dir;
old_AB <<= 2; //remember previous state
old_AB |= ( GPIOB_PDIR & 0x03 ); //add current state
scroll_steps += ( enc_states[( old_AB & 0x0f )]);
}
But these two things together makes the first one miss sometimes. So then the flag is not set, no data is read, the pin stays low. BAD.
What can I do to not miss interrupts?