avr: clockticks missing in long_options
[avr-sim.git] / test / timer.c
blobb18aeafd1b175606e3634031ba3cb4bc9dd8a171
1 /*
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
31 * Simulavr
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>
38 #include <inttypes.h>
40 typedef struct _TimerInfo TimerInfo;
42 struct _TimerInfo
44 uint16_t overflow_count;
45 uint16_t oc_match_count[3];
48 static TimerInfo timer[4];
49 static uint32_t cycle;
51 SIGNAL(SIG_OVERFLOW0)
53 timer[0].overflow_count++;
56 SIGNAL(SIG_OVERFLOW1)
58 timer[1].overflow_count++;
61 SIGNAL(SIG_OVERFLOW2)
63 timer[2].overflow_count++;
66 SIGNAL(SIG_OVERFLOW3)
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]++;
102 void
103 clear_structs (void)
105 uint8_t i, n;
107 for (i=0; i<4; i++)
109 timer[i].overflow_count=0;
110 for (n=0; n<3; n++)
112 timer[i].oc_match_count[n]=0;
118 main (void)
120 /* Init the struct mem with zeros. */
122 clear_structs();
124 /* TODO: Add PWM test as PWM functionality is introduced in Simulavr.
125 Start 8bit Timer/Counter 0 with prescaler 8. */
127 TCCR0 = _BV(CS01);
129 /* Start 16bit Timer/Counter 1 w/o prescaler. */
131 TCCR1B = _BV(CS10);
133 /* Start 8bit Timer/Counter 2 with prescaler 32. */
135 #if defined (TCCR2)
136 TCCR2 = (_BV(CS20) | _BV(CS22));
137 #endif
139 /* Start 16bit Timer/Counter 3 with prescaler 1024.
140 This is not yet implemented in simulavr (as all extended registers). */
142 #if defined (TCCR3B)
143 TCCR3B = (_BV(CS30) | _BV(CS31) | _BV(CS32));
144 #endif
146 /* Now we set some OCR-Values. */
148 /* 8 bit */
150 #if defined(OCR0)
151 OCR0 = 12;
152 #endif
153 #if defined(OCR2)
154 OCR2 = 34;
155 #endif
157 /* 16 bit */
159 OCR1A = 1234;
160 OCR1B = 2345;
161 #if defined(OCR1C)
162 OCR1C = 3456;
163 #endif
164 #if defined(OCR3A)
165 OCR3A = 4567;
166 #endif
167 #if defined(OCR3B)
168 OCR3B = 5678;
169 #endif
170 #if defined(OCR3C)
171 OCR3C = 6789;
172 #endif
174 /* Enable overflow irqs for timers 0-3 and output compare irq
175 for timer 0, 1a, 1b, 3b. */
177 TIMSK = (
178 _BV(TOIE0)
179 | _BV(TOIE1)
180 #if defined(TOIE2)
181 | _BV(TOIE2)
182 #endif
183 #if defined(OCIE2)
184 | _BV(OCIE2)
185 #endif
186 #if defined(OCIE0)
187 | _BV(OCIE0)
188 #endif
189 | _BV(OCIE1A)
190 | _BV(OCIE1B)
193 #if defined(ETIMSK)
194 ETIMSK = (
195 _BV(TOIE3)
196 | _BV(OCIE3B)
198 #endif
200 sei (); /* Enable irqs */
202 for (;;)
204 cycle++; /* Increase cycle counter */
207 return (0);