Sunday, May 29, 2022

tinkercat timer interrupt clear on compare

flash led every second with timer interrupt
volatile byte lightState = LOW;
int period;

void setup()
{
  pinMode(9, OUTPUT);
  
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00001101; //Set mode to be clear on compare. set scaler to be clock/1024
  OCR1A = 15624; //period = (OCR1A/B + 1)*scaler*62.5ns = 1s
  TIMSK1 = 0b00000010; //enable timer counter
  sei();
  Serial.begin(9600);
}

ISR (TIMER1_COMPA_vect){
  lightState = digitalRead(9);
  digitalWrite(9, not lightState);
  period = TCNT1 *0.0000625*1024;
  Serial.print("timer periord:");
  Serial.println(period);
}

void loop()
{
}

reference:





No comments:

Post a Comment