-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
66 lines (50 loc) · 1.72 KB
/
timer.c
File metadata and controls
66 lines (50 loc) · 1.72 KB
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
/*==============================================================================
Function declarations and data structures for 16 bit timers
=============================================================================*/
#include "timer.h"
/* ------------------ */
/* Extern variables */
/* ------------------ */
/* ------------------ */
/* Static variables */
/* ------------------ */
/* ---------------------- */
/* Function definitions */
/* ---------------------- */
/* # Timer constructor
Select 16-bit timer to configure for PWM output. Choices are
n = 1,3,4,5. See data-sheet and schematics for pin locations.
*/
int TIMER_Init(TIMER *timer, uint8_t n)
{
/* Determine offset address */
switch(n)
{
case 1:
timer->timer_reg_loc = (volatile uint8_t *) 0x80;
break;
case 3:
timer->timer_reg_loc = (volatile uint8_t *) 0x90;
break;
case 4:
timer->timer_reg_loc = (volatile uint8_t *) 0xA0;
break;
case 5:
timer->timer_reg_loc = (volatile uint8_t *) 0x120;
break;
default:
// Error: unknown timer id
return -1;
}
timer->timer_n = n;
/* Set register addresses */
timer->TCCRnA = &_SFR_MEM8(timer->timer_reg_loc + _TCCRnA);
timer->TCCRnB = &_SFR_MEM8(timer->timer_reg_loc + _TCCRnB);
timer->TCCRnC = &_SFR_MEM8(timer->timer_reg_loc + _TCCRnC);
timer->ICRn = &_SFR_MEM16(timer->timer_reg_loc + _ICRn);
timer->TCNTn = &_SFR_MEM16(timer->timer_reg_loc + _TCNTn);
timer->OCRnA = &_SFR_MEM16(timer->timer_reg_loc + _OCRnA);
timer->OCRnB = &_SFR_MEM16(timer->timer_reg_loc + _OCRnB);
timer->OCRnC = &_SFR_MEM16(timer->timer_reg_loc + _OCRnC);
return 0;
}