Hi,
first thank you so much for lookng.
I just got out of arduino and into programming microcontrollers with an avr programmer. I want to go bare bones so I started with writing my own timer header file for the internal or an external clock.
here is the code for the interrupt timer.
volatile uint32_t milliSec=0; //number of milliseconds passed. volatile because it is
// modified in ISR but read in other functions and in main.
static uint16_t __f_=0; //Fractional number of milliseconds passed. global to reduce
//code and static to prevent bugs from main.
uint32_t j=0;
ISR(TIM0_OVF_vect)
{
//toggleBit(&PORTA,1);
__f_ += __fmpi_;
if( __f_ < __fmax_ )
{
milliSec += __mpi_;
}
else
{
__f_ -= __fmax_;
milliSec += (__mpi_ + 1); //plus 1 because fraction has overflowed. we are playing catchup
}
//if((milliSec-j) > 250)
//{
// toggleBit(&PORTA,1);
//j = milliSec;
// }
//if else to acess volatile variable from memory only once to reduce overhead.
//if fraction is less than or equal to __fmax_ then only __mpi_ has passed,
// else fraction is over __fmax_ so another millisecond has passed.
}
//Interupt service routine for Timer overflow on timer 0
//runs every __TICKPERINTERUPT_*__PRESCALER_ clock cycles. Use this to detmine accuracy.
f is the fraction millisecond count
fmpi is fraction millisecond per interrupt
mpi is milli per interrupt
and fmax is the max of the fraction.
Looking at the code it seems like it would run perfectly. My issue comes when I try to access the volatile variable milliSec from main. for some reason the program completely ignores the variable but only in main.
here is an example of my main code to test the timer. Has yet to work.
uint32_t I=0,boolean=1;
int main
{
while(boolean);
{
if((milliSec-I) > 250)
{
toggleBit(&PORTA,1);
I = milliSec;
}
return 0;
}
Now here comes the crazy part. When I run that loop in main the led I have connected does not toggle. However, when I comment out the loop in main and uncomment the if statement in the timer.h ISR it works perfectly and the led does toggle for 250 milliseconds. I say perfectly because I am able to access a real reading from the variable milliSec, which allows my delay if statement to work correctly, thus toggling the led.
So basically my question is ... Does anyone understand why i am able to read milliSec in the timer.h header file but when I try to read from main it always comes out as 0. Thanks everyone.
Basically I thought volatile was supposed to fix this.
I would love for someone to upload it and try it for me and see if we get the same error in main.
Thanks once again!