-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
131 lines (105 loc) · 3.08 KB
/
main.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
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
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/pin_map.h"
typedef unsigned long ul;
#define P(port,pin,timer,side) { GPIO_P ## port ## pin ## _T ## timer ## CCP ## side, GPIO_PORT ## port ##_BASE, SYSCTL_PERIPH_TIMER ## timer, TIMER ## timer ## _BASE, 0xff << (side * 2) }
ul d[][5] = {
/*{ GPIO_PF0_T0CCP0, GPIO_PORTF_BASE, SYSCTL_PERIPH_TIMER0, TIMER0_BASE, TIMER_A },
{ GPIO_PF1_T0CCP1, GPIO_PORTF_BASE, SYSCTL_PERIPH_TIMER0, TIMER0_BASE, TIMER_B }...
Replaced with equivalent below.*/
P(F,0,0,0),
P(F,1,0,1),
P(F,2,1,0),
P(F,3,1,1),
P(F,4,2,0),
P(B,0,2,0),
P(B,1,2,1),
P(B,2,3,0),
P(B,3,3,1),
P(B,4,1,0),
P(B,5,1,1),
P(B,6,0,0),
P(B,7,0,1),
P(C,0,4,0),
P(C,1,4,1),
P(C,2,5,0),
P(C,3,5,1)
};
#define PORTF 5
#define PORTB 1
#define PORTC 2
int getId(ul port, ul pin) {
switch(port) {
case PORTB:
return pin + 5;
case PORTC:
return pin + 5 + 8;
default:
return pin;
}
}
/*
* Can use PORTF pins 0 - 4
* PORTB pins 0 - 7
* PORTC pins 0 - 3
*
* Cannot use these pins together:
* PB6 and PF0
* PB7 and PF1
* PF2 and PB4
* PF3 and PB5
* PF4 and PB0
*
* You can use up to 12 pins for different pwm output
*/
void timerInit(ul port, ul pin) {
int id = getId(port, pin);
SysCtlPeripheralEnable(1 << port | 0x20000000);
GPIOPinTypeGPIOOutput(d[id][1], 1 << pin);
GPIOPinWrite(d[id][1], 1 << pin, 0);
GPIOPinConfigure(d[id][0]);
GPIOPinTypeTimer(d[id][1], 1 << pin);
//GPIOPadConfigSet(d[id][1], 1 << pin, GPIO_STRENGTH_8MA_SC, GPIO_PIN_TYPE_STD);
SysCtlPeripheralEnable(d[id][2]);
TimerConfigure (d[id][3], TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM|TIMER_CFG_B_PWM);
TimerControlLevel (d[id][3], d[id][4], true);
TimerPrescaleSet (d[id][3], d[id][4], 10); // pwm period = (10+1) * 0xFFFF = 720885 ~ 18 ms on 40 MHz
TimerPrescaleMatchSet(d[id][3], d[id][4], 0);
TimerMatchSet (d[id][3], d[id][4], 0);
TimerLoadSet (d[id][3], d[id][4], 0xFFFF);
TimerEnable (d[id][3], TIMER_BOTH);
}
void timerSet(unsigned long duty, ul port, ul pin) {
int id = getId(port, pin);
TimerPrescaleMatchSet(d[id][3], d[id][4], duty / 0xFFFF);
TimerMatchSet (d[id][3], d[id][4], duty % 0xFFFF);
}
int main(void) {
unsigned long ulPeriod, dutyCycle;
// 40 MHz system clock
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|
SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
ulPeriod = 0xFFFF;
dutyCycle = 0xFFFF;
// use timerInit on port-pin once
timerInit(PORTF, 1);
timerInit(PORTF, 2);
timerInit(PORTF, 0);
// then timerSet to change duty cycle
timerSet(40 * 544, PORTF, 1);
timerSet(40 * 2400, PORTF, 2);
while(1) {
dutyCycle += 10000;
if(dutyCycle >= 40 * 2400) {
dutyCycle = 40 * 544;
SysCtlDelay(5000000);
}
timerSet(dutyCycle, PORTF, 0);
if(dutyCycle == 40 * 544)
SysCtlDelay(10000000);
SysCtlDelay(1000000);
}
}