Cantafford
Member
Hello,
I'm trying to use the timer0 interrupt of ATMega328P MCU to flash an LED every second.
I have used this calculator and concluded that for a 20Mhz crystal 195 timer ticks will give an interrupt at every 0.01 seconds. So I'm using a variable counter up to 100 to flash the LED when it reaches 100(so one second has elapsed).
I have written this code:
Now when I simulate this my led flashes every 30 seconds instead of every second. Now I can increase it's frequency by just reducing the number of ticks but that's just playing around with it no precision at all.
The code above is from a tutorial and in that tutorial the LED flashes exactly at one second interval. The code is basically identical. However I'm simulating the application in proteus whereas that guy who made that tutorial is implementing it in hardware. Can this be an issue from Proteus simulator? I've been told in the past that it has some problems when it comes to modelling the oscillators(or something like that). And if is indeed a proteus problem any way I can fix it so the oscillator and the program are synced? Thank you for reading.
Here is the proteus schematic:
I'm trying to use the timer0 interrupt of ATMega328P MCU to flash an LED every second.
I have used this calculator and concluded that for a 20Mhz crystal 195 timer ticks will give an interrupt at every 0.01 seconds. So I'm using a variable counter up to 100 to flash the LED when it reaches 100(so one second has elapsed).
I have written this code:
Code:
/*
* AVRGCC1.c
*
* Created: 10/19/2016 5:20:34 PM
* Author: Paul
*/
#include <avr/io.h>
#include <avr/interrupt.h> // quick way to include interrupts in the code
int extraTime = 0;
int main(void)
{
DDRB = 0x01; // setting the LED as an output
TCCR0A = (1 << WGM01); // Set CTC Bit
OCR0A = 195; // number of ticks we need for our specific time
TIMSK0 = (1 << OCIE0A);
TCCR0B = (1 << CS02) | (1 << CS00); // use 1024 prescaler
sei(); // setting the interrupt???(setting the i bit)
while(1)
{
//TODO:: Please write your application code
}
}
ISR(TIMER0_COMPA_vect) // the interrupt service routine(see list of vectors for all interrupt sources!!!!
{
extraTime++;
if(extraTime > 100)
{
PORTB ^= (1<<PORTB0); // toggle LED
extraTime = 0;
}
}
Now when I simulate this my led flashes every 30 seconds instead of every second. Now I can increase it's frequency by just reducing the number of ticks but that's just playing around with it no precision at all.
The code above is from a tutorial and in that tutorial the LED flashes exactly at one second interval. The code is basically identical. However I'm simulating the application in proteus whereas that guy who made that tutorial is implementing it in hardware. Can this be an issue from Proteus simulator? I've been told in the past that it has some problems when it comes to modelling the oscillators(or something like that). And if is indeed a proteus problem any way I can fix it so the oscillator and the program are synced? Thank you for reading.
Here is the proteus schematic:
