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?!
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
}
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!
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