Dear forum members,
I'm learning AVR-C, and these day's I'm studying interrupts. For my Attiny85 with 16 MHz crystal, I made the blink sketch below. I'm trying to make a simple blink sketch where the LED is on for 1 second, and then off for 59 seconds.
I wrote the following code:
#include <avr/io.h>
#include <avr/interrupt.h>
volatile uint32_t milliseconds = 0;
int main(void)
{
uint8_t ledPin = 4;
DDRB |= ledPin; // Set ledPin to OUTPUT
TCCR0A = (1 << WGM01); // Set CTC (Clear Timer on Compare)
OCR0A = 249; // Set No. of Ticks.
TIMSK = (1 << OCIE0A); // Enable comparing with OCIE0A
sei();
TCCR0B = (1 << CS01) | (1 << CS00); // start at 64 Prescaler
uint32_t goal = milliseconds;
while(1)
{
PORTB |= ledPin; // Turn LED ON
goal += 1000;
while( milliseconds < goal);
PORTB &= ~ledPin; // Turn LED OFF
goal += 59000;
while( milliseconds < goal);
}
}
ISR(TIMER0_COMPA_vect)
{
milliseconds++;
}
This code works fine, the LED blinks once every minute. However, sometimes the LED goes on too early.
Using an arduino Nano and a python script, I measured the exact time the LED turns on and off. The results below are what I measured.
Does somebody have an explanation for this behaviour?
ON: 15:51:23.543651 OFF: 14:51:24.311653
ON: 14:52:23.540698 OFF: 14:52:24.540681
ON: 14:53:23.537724 OFF: 14:53:24.369708
ON: 14:54:23.534751 OFF: 14:54:24.534730
[color=red]ON: 14:54:40.141746 OFF: 14:55:24.427774[/color]
ON: 14:56:23.528794 OFF: 14:56:24.528777
ON: 14:57:23.525819 OFF: 14:57:24.485803
ON: 14:58:23.522846 OFF: 14:58:24.522824
ON: 14:59:23.519867 OFF: 14:59:24.519850
ON: 15:00:23.516889 OFF: 15:00:24.516872
ON: 15:01:23.513914 OFF: 15:01:24.513895
[color=red]ON: 15:02:18.870938 OFF: 15:02:24.510920[/color]
ON: 15:03:23.507963 OFF: 15:03:24.507947
ON: 15:04:23.280985 OFF: 15:04:24.504968
ON: 15:05:23.502015 OFF: 15:05:24.501991
ON: 15:06:23.339036 OFF: 15:06:24.499019
ON: 15:07:23.496057 OFF: 15:07:24.496037
ON: 15:08:23.397093 OFF: 15:08:24.493076
ON: 15:09:23.490112 OFF: 15:09:24.490136
[color=red]ON: 15:09:57.600127 OFF: 15:10:24.487133[/color]
ON: 15:11:23.484161 OFF: 15:11:24.484162
ON: 15:12:23.481180 OFF: 15:12:24.481165
ON: 15:13:23.478202 OFF: 15:13:24.478184
ON: 15:14:23.475234 OFF: 15:14:24.475213
ON: 15:15:23.472252 OFF: 15:15:24.472242
ON: 15:16:23.469274 OFF: 15:16:24.469277
ON: 15:17:23.466299 OFF: 15:17:24.466295
[color=red]ON: 15:17:36.329301 OFF: 15:18:24.463321[/color]
ON: 15:19:23.460348 OFF: 15:19:24.460346
ON: 15:20:23.457372 OFF: 15:20:24.257354
ON: 15:21:23.454389 OFF: 15:21:24.454362
ON: 15:22:23.451414 OFF: 15:22:24.315399
ON: 15:23:23.448435 OFF: 15:23:24.448421
ON: 15:24:23.445465 OFF: 15:24:24.373443
[color=red]ON: 15:25:15.058486 OFF: 15:25:24.442472[/color]
ON: 15:26:23.439514 OFF: 15:26:24.431513
ON: 15:27:23.436536 OFF: 15:27:24.436525
ON: 15:28:23.433558 OFF: 15:28:24.433542
ON: 15:29:23.430590 OFF: 15:29:24.430573
ON: 15:30:23.427605 OFF: 15:30:24.427589
ON: 15:31:23.424632 OFF: 15:31:24.424619
ON: 15:32:23.421656 OFF: 15:32:24.421639
Best wishes,
da big fella