4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
24 #include <avr/interrupt.h>
25 #include <avr/sleep.h>
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.
32 #include "avr_mcu_section.h"
33 AVR_MCU(F_CPU, "atmega88");
36 * This small section tells simavr to generate a VCD trace dump with changes to these
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.
42 * Here the port B first 2 bits are used to display when a tick occurs, and when a
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, },
53 volatile uint16_t tcnt;
55 ISR(TIMER2_COMPA_vect) // handler for Output Compare 2 overflow interrupt
57 // this really doesn't no anything but proves a way to wake the main()
58 // from sleep at regular intervals
65 // start the 16 bits timer, with default "normal" waveform
66 // and no interupt enabled. This just increments TCNT1
69 // timer prescaler to 64
70 TCCR1B |= (0<<CS12 | 1<<CS11 | 1<<CS10);
75 // now enable a tick counter
76 // using an asynchronous mode
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
84 TIMSK2 |= (1 << OCIE2A);
89 while (count++ < 100) {
90 // we read TCNT1, which should contain some sort of incrementing value
91 tcnt = TCNT1; // read it
93 TCNT1 = 500; // reset it arbitrarily
94 PORTB ^= 2; // mark it in the waveform file
96 sleep_cpu(); // this will sleep until a new timer2 tick interrupt occurs
98 // sleeping with interrupt off is interpreted by simavr as "exit please"