-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.c
65 lines (51 loc) · 1.31 KB
/
Timer.c
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
#include "Timer.h"
void setupTimer(void);
int getSampleRate(void);
void _setTimerReload(int reload);
/****** Constants ******/
const int MAXSAMPLERATE = 400; //Max samples per second
const int MINSAMPLERATE = 10; //Min samples per second
const int CYCLESPERSECOND = 16000000; //clock cycles persecond
/****** Static variables *******/
static int sampleRate = 10; //The current sample rate
/****** "Public" Methods ******/
//Configures the timer - Period mode, 10Hz
void setupTimer(void)
{
*RCGCTIMER |= 0x1;
*GPTMCTL &= ~0x01; //disable timer
*GPTMCFG &= ~0x7; //set to 32bit mode
*GPTMTAMR |= 0x02; //set timer to periodic mode
*GPTMTAILR = CYCLESPERSECOND/sampleRate; //set reload value
*GPTMCTL |= 0x21; //enable timer and adc trigger
}
//Returns the current sample rate
int getSampleRate(void)
{
return sampleRate;
}
//Increments the sample rate by 10
void increaseSampleRate(void)
{
if (!(sampleRate == MAXSAMPLERATE))
{
sampleRate += 10;
_setTimerReload(CYCLESPERSECOND/sampleRate);
}
}
//Decrements the sample rate by 10
void decreaseSampleRate(void)
{
if (!(sampleRate == MINSAMPLERATE))
{
sampleRate -= 10;
_setTimerReload(CYCLESPERSECOND/sampleRate);
}
}
/*
* Changes the reload value of the timer
*/
void _setTimerReload(int reload)
{
*GPTMTAILR = reload;
}