2 * $Id: timer.c,v 1.2 2003/11/12 07:33:17 troth Exp $
4 ****************************************************************************
6 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7 * Copyright (C) 2003, Hermann Kraus
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ****************************************************************************
26 /* This test program has several functions:
27 * - Testing interrupts in general.
28 * - Testing the timer overflow and output compare match irqs.
29 * - Showing how the timers work.
30 * - Allowing the user to examine the "struct" and "array" debugging with
33 * But be careful: This is an example for testing purposes only. For real
34 * programs it wastes too much flash.
37 #include <avr/interrupt.h>
40 typedef struct _TimerInfo TimerInfo
;
44 uint16_t overflow_count
;
45 uint16_t oc_match_count
[3];
48 static TimerInfo timer
[4];
49 static uint32_t cycle
;
53 timer
[0].overflow_count
++;
58 timer
[1].overflow_count
++;
63 timer
[2].overflow_count
++;
68 timer
[4].overflow_count
++;
72 SIGNAL(SIG_OUTPUT_COMPARE0
)
74 timer
[0].oc_match_count
[0]++;
77 SIGNAL(SIG_OUTPUT_COMPARE1A
)
79 timer
[1].oc_match_count
[0]++;
82 SIGNAL(SIG_OUTPUT_COMPARE1B
)
84 timer
[1].oc_match_count
[1]++;
87 SIGNAL(SIG_OUTPUT_COMPARE2
)
89 timer
[2].oc_match_count
[0]++;
92 SIGNAL(SIG_OUTPUT_COMPARE3A
)
94 timer
[3].oc_match_count
[0]++;
97 SIGNAL(SIG_OUTPUT_COMPARE3B
)
99 timer
[3].oc_match_count
[1]++;
109 timer
[i
].overflow_count
=0;
112 timer
[i
].oc_match_count
[n
]=0;
120 /* Init the struct mem with zeros. */
124 /* TODO: Add PWM test as PWM functionality is introduced in Simulavr.
125 Start 8bit Timer/Counter 0 with prescaler 8. */
129 /* Start 16bit Timer/Counter 1 w/o prescaler. */
133 /* Start 8bit Timer/Counter 2 with prescaler 32. */
136 TCCR2
= (_BV(CS20
) | _BV(CS22
));
139 /* Start 16bit Timer/Counter 3 with prescaler 1024.
140 This is not yet implemented in simulavr (as all extended registers). */
143 TCCR3B
= (_BV(CS30
) | _BV(CS31
) | _BV(CS32
));
146 /* Now we set some OCR-Values. */
174 /* Enable overflow irqs for timers 0-3 and output compare irq
175 for timer 0, 1a, 1b, 3b. */
200 sei (); /* Enable irqs */
204 cycle
++; /* Increase cycle counter */