Arduino NANO and TIMER0

hello,
I am building a sketch to generate an interruption every 5 micro second, so that I can generate other time bases from this base of 5us.
I set up the following code:

ISR(TIMER0_COMPA_vect) {
  digitalWrite(6,!digitalRead(6));
}

void setup() {
  Serial.begin(115200);
  pinMode(6, OUTPUT);
  digitalWrite(6,LOW);

  cli();                    // Disable global interrupts
  TCCR0A |= (1 << WGM01);   // Set up Timer0 for CTC mode
  TCCR1B |= (0 << CS02)|(0 << CS01)|(1 << CS00);  // Prescaler 1
  OCR0A = 80 - 1;           // Set the compare value (OCR0A)
  TIMSK0 |= (1 << OCIE0A);  // Enable output compare interrupt
  sei();                    // Enable global interrupts
}

void loop() {
  ...
}

The standard frequency of ATMEGA328P is 16 MHz, so I selected prescaler 1 for the system's clock frequency currency to be as low as possible to 5 microseconds.
Calculating the comparison value (OCR0A), so that the timer generates one interruption every 5 microseconds.
If the system clock is 16 MHz, each clock cycle lasts 1 /16 MHz = 0.0625 microseconds.
An overflow of the timer will be required every 5 microseconds, then 5 / 0.0625 = 80 clock cycles.
As count starts from zero, so: OCR0A = 80 - 1

As an end result, it is generating a pulse of approximately 1,020ms, which has a period of 2,040ms.
This value is very different from expected !!
What did I do wrong or I can't configure it correctly?!

Another doubt I have about the timer is: what is the smallest interruption I can generate in Atmega 428p timers?!

ty

I forgot to mention that I used an oscilloscope in pin 6 to check the time values!

I see you set TCCR1B , but that is the register for timer1 not for timer0

You can try this:

/**
  * URL: https://dbuezas.github.io/arduino-web-timers/#mcu=ATMEGA328P&timer=0&timerMode=CTC&topValue=OCR0A&OCR0A=79&interruptA=on&clockPrescalerOrSource=64
  * Mode     : CTC
  * Period   : 5 us
  * Frequency: 200 kHz
  * Outputs  : none
  */
void setup(){
  noInterrupts();
  TCCR0A = 
    1 << WGM01;
  TCCR0B = 
    1 << CS00;
  TIMSK0 = 
    1 << OCIE0A;
  OCR0A = 79;
  interrupts();
}
ISR(TIMER0_COMPA_vect) {
    /* on OCR0A match */
}

void loop() {
}

Generated with this web timer calculator

Don't expect to be able to do a lot in that Interrupt. You have only 80 CPU clockcycles before the next interrupt occurs.

Much faster to directly access the port instead of using digitalWrite() and digitalRead(), and with an atmega328 you can toggle an output by writing a 1 to the corresponding input port register.

ISR(TIMER0_COMPA_vect) {
  PIND = 0b01000000; //toggle digital pin 6
}
1 Like

dear hmeijdam Edison,
You are correct, but this error occurred when I moved the code for Note Pad, to erase the lines that were not pertinent to my doubt!
Even with the error corrected, the value was out of desired, however,
When I used the information of David_2018, it generated the exact time of 5us.

thank you dear hmeijdam Edison
thank you so much by the tip dear David_2018, works great!!!
You could tell me, where I could read and study tips like this ... I would like to learn more such tips like this!

In the Atmegs328 datashhet

Are you indirectly telling me that "PIND" is a mnemonic of the low level language (machine language) of the 328P?!

PIND is a reference to the hardware input register for port D on the atmega328.

This page explains more about directly accessing the input/output ports, but I don't think it mentions the toggling ability of PINx:
https://docs.arduino.cc/retired/hacking/software/PortManipulation/

dear david_2018, I went to check out the link that you passed and to my surprise, I realized that this command only READ and does NOT write!!!
You can check in attached image.


But, it is working perfectly now, even with this command !!!

I would like to take the opportunity and ask about this same possibility of reading, but now the analog port!
There is also a faster way to access them?!

That's why it's better to read the manufacturer's datasheet, rather than various links on the Internet.
Having read the Atmega328 datasheet, you would have learned that writing a one to PINx register is a special case


Chapter 13 of Atmega328 Datasheet

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.