systick_isr() does not work fully

yoonghm

Active member
Code:
volatile int status = 0;

void systick_isr() {
  systick_millis_count++;   // Original ISR code
  status = !status;
}

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, status);
  Serial.print(systick_millis_count);
  Serial.print(" - ");
  Serial.println(status);

  delay(1000);
}

Observation
The value of status does not change until systick_millis_count becomes 160400 (about 2 minutes 40 seconds later)
The value of status changes again at 319402.

The code was tested on Teensy 3.1 and Teensy 3.6. Both boards exhibit the same behaviour.
 
The interrupt fires every 1ms, toggling status. The delay 1000 is an even number, so until some execution time offset has accumulated, status is toggled an even number of times, so it's the same inside loop().

Change delay to 999 and you will see status toggle.
 
Thanks, i did not realize that the systick is 1ms.

Code:
volatile int status = 0;

void systick_isr() {
  systick_millis_count++;   // Original ISR code
  if (systick_millis_count % 1000 == 0) {
    digitalWrite(LED_BUILTIN, status);
    status = !status;
  }
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  
}
 
Last edited:
Back
Top