forked from buserror/simavr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatmega48_enabled_timer.c
59 lines (44 loc) · 1.22 KB
/
atmega48_enabled_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* avrtest.c
*
* Created on: 4 Feb 2011
* Author: sliedes
* This is a very slightly modified version of atmega48_disabled_timer.c
* by jone.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "avr_mcu_section.h"
AVR_MCU(F_CPU, "atmega48");
volatile uint8_t count;
ISR(TIMER0_COMPA_vect)
{
++count;
}
int main(void)
{
// Set up timer0 - do not start yet
TCCR0A |= (1 << WGM01); // Configure timer 0 for CTC mode
TIMSK0 |= (1 << OCIE0A); // Enable CTC interrupt
OCR0A = 0xAA; // CTC compare value
TCCR0B |= (1 << CS00) | (1 << CS01); // Start timer: clk/64
while ((TIFR0 & (1 << OCF0A)) == 0)
;
// Now interrupt is pending. Try and clear it.
TIFR0 = 0;
if (TIFR0 & (1 << OCF0A))
++count; // Should not clear
TIFR0 = (1 << OCF0A);
if ((TIFR0 & (1 << OCF0A)) == 0)
++count; // Should clear!
sei(); // Enable global interrupts
// Let it run to next interrupt.
sleep_mode();
TIMSK0 = 0; // Disable CTC interrupt
if (count == 3) // Expected
cli();
// Time out if interrupting or count wrong.
for (;;)
sleep_mode();
}