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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* $Id: timer.c,v 1.2 2003/11/12 07:33:17 troth Exp $
*
****************************************************************************
*
* simulavr - A simulator for the Atmel AVR family of microcontrollers.
* Copyright (C) 2003, Hermann Kraus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
****************************************************************************
*/
/* This test program has several functions:
* - Testing interrupts in general.
* - Testing the timer overflow and output compare match irqs.
* - Showing how the timers work.
* - Allowing the user to examine the "struct" and "array" debugging with
* Simulavr
*
* But be careful: This is an example for testing purposes only. For real
* programs it wastes too much flash.
*/
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include "common.h"
typedef struct _TimerInfo TimerInfo;
struct _TimerInfo
{
uint16_t overflow_count;
uint16_t oc_match_count[3];
};
static TimerInfo timer[4];
static uint32_t cycle;
SIGNAL(SIG_OVERFLOW0)
{
timer[0].overflow_count++;
}
SIGNAL(SIG_OVERFLOW1)
{
timer[1].overflow_count++;
}
SIGNAL(SIG_OVERFLOW2)
{
timer[2].overflow_count++;
}
SIGNAL(SIG_OVERFLOW3)
{
timer[4].overflow_count++;
}
SIGNAL(SIG_OUTPUT_COMPARE0)
{
timer[0].oc_match_count[0]++;
}
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
timer[1].oc_match_count[0]++;
}
SIGNAL(SIG_OUTPUT_COMPARE1B)
{
timer[1].oc_match_count[1]++;
}
SIGNAL(SIG_OUTPUT_COMPARE1C)
{
timer[1].oc_match_count[2]++;
}
SIGNAL(SIG_OUTPUT_COMPARE2)
{
timer[2].oc_match_count[0]++;
}
SIGNAL(SIG_OUTPUT_COMPARE3A)
{
timer[3].oc_match_count[0]++;
}
SIGNAL(SIG_OUTPUT_COMPARE3B)
{
timer[3].oc_match_count[1]++;
}
SIGNAL(SIG_OUTPUT_COMPARE3C)
{
timer[3].oc_match_count[2]++;
}
void
clear_structs (void)
{
uint8_t i, n;
for (i=0; i<4; i++)
{
timer[i].overflow_count=0;
for (n=0; n<3; n++)
{
timer[i].oc_match_count[n]=0;
}
}
}
int
main (void)
{
/* Init the struct mem with zeros. */
clear_structs();
/* TODO: Add PWM test as PWM functionality is introduced in Simulavr.
Start 8bit Timer/Counter 0 with prescaler 8. */
TCCR0 = _BV(CS01);
/* Start 16bit Timer/Counter 1 w/o prescaler. */
TCCR1B = _BV(CS10);
/* Start 8bit Timer/Counter 2 with prescaler 32. */
#if defined (TCCR2)
TCCR2 = (_BV(CS20) | _BV(CS22));
#endif
/* Start 16bit Timer/Counter 3 with prescaler 1024.
This is not yet implemented in simulavr (as all extended registers). */
#if defined (TCCR3B)
TCCR3B = (_BV(CS30) | _BV(CS31) | _BV(CS32));
#endif
/* Now we set some OCR-Values. */
/* 8 bit */
#if defined(OCR0)
OCR0 = 12;
#endif
#if defined(OCR2)
OCR2 = 34;
#endif
/* 16 bit */
OCR1A = 1234;
OCR1B = 2345;
#if defined(OCR1C)
OCR1C = 3456;
#endif
#if defined(OCR3A)
OCR3A = 4567;
#endif
#if defined(OCR3B)
OCR3B = 5678;
#endif
#if defined(OCR3C)
OCR3C = 6789;
#endif
/* Enable overflow irqs for timers 0-3 and output compare irq
for timer 0, 1a, 1b, 3b. */
TIMSK = (
_BV(TOIE0)
| _BV(TOIE1)
#if defined(TOIE2)
| _BV(TOIE2)
#endif
#if defined(OCIE0)
| _BV(OCIE0)
#endif
| _BV(OCIE1A)
| _BV(OCIE1B)
);
#if defined(ETIMSK)
ETIMSK = (
_BV(TOIE3)
| _BV(OCIE3B)
);
#endif
sei (); /* Enable irqs */
for (;;)
{
cycle++; /* Increase cycle counter */
}
return (0);
}
|