-
Notifications
You must be signed in to change notification settings - Fork 38
/
timer.c
57 lines (43 loc) · 1.17 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
#include <stdint.h>
#include "hardware.h"
#include "timer.h"
volatile uint32_t __timerCounter = 0;
void init_timer() {
union {
uint8_t byte[2];
unsigned short val;
} t1cnt;
// Configure timer to increment timerCounter once every 1ms
// 1000 is scaling mhz to cycles/msec, 128 is T1CTL.DIV, 2 is TICKSPD
uint16_t timer_ticks_per_ms = (SYSTEM_CLOCK_MHZ * 1000) / 128 / 2;
__timerCounter = 0;
T1CTL = 0x00; // disable timer
T1CNTL = 0x00; // Clear counter low
// Start timer in free running mode, no prescaler
T1CTL = 0x01;
do {
t1cnt.byte[0] = T1CNTL;
t1cnt.byte[1] = T1CNTH;
} while (t1cnt.val <= 0xA000);
// Stop timer
T1CTL = 0x00;
// Set period
T1CC0H = timer_ticks_per_ms >> 8;
T1CC0L = timer_ticks_per_ms & 0xff;
T1CCTL0 = 0x44; // Enable int, set output on compare, compare mode, no capture
TIMIF |= OVFIM; // Enable Timer 1 overflow interrupt mask
T1IE = 1;
EA = 1;
T1CTL = 0x0e; // TickFreq/128, modulo
}
void t1_isr(void) __interrupt T1_VECTOR
{
__timerCounter++;
}
void delay(uint32_t msec) {
uint32_t start_time;
read_timer(&start_time);
while(!check_elapsed(start_time, msec)) {
feed_watchdog();
}
}