You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//FOR SPEED PROFILING WITH OSCILLOSCOPE
//-Don't forget that a direct-port-access write like this takes 2 clock cycles, or 0.125 us, so you have to subtract
//2 clock cycles (NOT 4) from whatever time period you profile with the oscilloscope.
#define PROFILE_PIN2_HIGH PORTD |= _BV(2) //write Arduino pin D2 HIGH
#define PROFILE_PIN2_LOW PORTD &= ~_BV(2) //write Arduino pin D2 LOW
is written incorrectly. |= and &= are NOT atomic operations, so technically interrupts should be disabled and enabled again. Implement a macro which uses a do while loop to automatically do that for me, utilizing the following:
uint8_t SREG_bak = SREG;
cli();
// do your pin change
SREG = SREG_bak;
NOTE: do this ANYWHERE in the code you use &= or |=, as NONE of those operations are automatically atomic, so you need to add atomic access guards for all of them to force atomicity.
The text was updated successfully, but these errors were encountered:
ElectricRCAircraftGuy
changed the title
Need to disable interrupts when setting output pins
Need to disable interrupts when setting output pins or modifying registers in order to enforce atomic access
Feb 21, 2019
This:
is written incorrectly.
|=
and&=
are NOT atomic operations, so technically interrupts should be disabled and enabled again. Implement a macro which uses ado while
loop to automatically do that for me, utilizing the following:See here for an example:
Source: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring_digital.c
NOTE: do this ANYWHERE in the code you use
&=
or|=
, as NONE of those operations are automatically atomic, so you need to add atomic access guards for all of them to force atomicity.The text was updated successfully, but these errors were encountered: