forked from ps2/subg_rfspy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.c
54 lines (40 loc) · 1.09 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
#include <stdint.h>
#include "hardware.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;
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 reset_timer() {
timerCounter = 0;
}
void t1_isr(void) __interrupt T1_VECTOR
{
if (timerCounter % 1000 == 0) {
//BLUE_LED = !BLUE_LED;
}
timerCounter++;
}