interrupts: Do not clear raised bit twice
[simavr] / tests / atmega88_timer16.c
1 /*
2         atmega88_timer16.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <avr/io.h>
23 #include <stdio.h>
24 #include <avr/interrupt.h>
25 #include <avr/sleep.h>
26
27 /*
28  * This demonstrates how to use the avr_mcu_section.h file.
29  * The macro adds a section to the ELF file with useful
30  * information for the simulator.
31  */
32 #include "avr_mcu_section.h"
33 AVR_MCU(F_CPU, "atmega88");
34
35 /*
36  * This small section tells simavr to generate a VCD trace dump with changes to these
37  * registers.
38  * Opening it with gtkwave will show you the data being read & written to these
39  * It also demonstrate how you can use unused pins to generate your own traces, with
40  * your own events to be displayed.
41  * 
42  * Here the port B first 2 bits are used to display when a tick occurs, and when a 
43  * TCNT reset occurs.
44  */
45 const struct avr_mmcu_vcd_trace_t _mytrace[]  _MMCU_ = {
46         { AVR_MCU_VCD_SYMBOL("TCNT1L"), .what = (void*)&TCNT1L, },      
47         { AVR_MCU_VCD_SYMBOL("TCNT1H"), .what = (void*)&TCNT1H, },      
48         { AVR_MCU_VCD_SYMBOL("tick"), .mask = (1 << 0), .what = (void*)&PORTB, },       
49         { AVR_MCU_VCD_SYMBOL("reset_timer"), .mask = (1 << 1), .what = (void*)&PORTB, },        
50         { AVR_MCU_VCD_SYMBOL("OC2A"), .mask = (1 << 3), .what = (void*)&PORTB, },
51 };
52
53 volatile uint16_t tcnt;
54
55 ISR(TIMER2_COMPA_vect)          // handler for Output Compare 2 overflow interrupt
56 {
57         // this really doesn't no anything but proves a way to wake the main()
58         // from sleep at regular intervals
59         PORTB ^= 1;
60 }
61
62 int main()
63 {       
64         //
65         // start the 16 bits timer, with default "normal" waveform
66         // and no interupt enabled. This just increments TCNT1
67         // at a regular rate
68         //
69         // timer prescaler to 64
70         TCCR1B |= (0<<CS12 | 1<<CS11 | 1<<CS10);
71
72         DDRB = 0x0B;
73         
74         //
75         // now enable a tick counter
76         // using an asynchronous mode
77         //
78         ASSR |= (1 << AS2);             // use "external" 32.7k crystal source
79         // use CLK/8 prescale value, clear timer/counter on compareA match
80         // toggle OC2A pin too
81         TCCR2A = (1 << WGM21) | (1 << COM2A0);
82         TCCR2B = (2 << CS20); // prescaler
83         OCR2A = 63;     // 64 hz
84         TIMSK2  |= (1 << OCIE2A);
85
86         sei();
87         
88         int count = 0;
89         while (count++ < 100) {
90                 // we read TCNT1, which should contain some sort of incrementing value
91                 tcnt = TCNT1;           // read it
92                 if (tcnt > 10000) {
93                         TCNT1 = 500;    // reset it arbitrarily
94                         PORTB ^= 2;             // mark it in the waveform file
95                 }
96                 sleep_cpu();            // this will sleep until a new timer2 tick interrupt occurs
97         }
98         // sleeping with interrupt off is interpreted by simavr as "exit please"
99         cli();
100         sleep_cpu();
101 }