-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwr_pwmtimer.c
183 lines (150 loc) · 5.23 KB
/
wr_pwmtimer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "wr_pwmtimer.h"
#include "inc/lm3s811.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/timer.h"
unsigned long ulPeriod;
//
// In thss case ratio mean period of signel in tacts
//
void WRPWMRatioSet(long pwm_out, long pulse_duration)
{
//http://en.wikipedia.org/wiki/Duty_cycle
//Duty cycle == signal_period / period
//signel_period usually from 3000 to 15000
//cause ulPeriod = SysCtlClockGet() / WR_PWMTIMER_RATE;
//6M / 100hz = 60000 (ticks/10ms)
//so length of usual signal is (0.3, 2.5)ms
unsigned long duration = pulse_duration;
long PWM_OUT = PWM_OUT_0;
switch(pwm_out){
case 0: PWM_OUT = PWM_OUT_0; break;
case 1: PWM_OUT = PWM_OUT_1; break;
case 2: PWM_OUT = PWM_OUT_2; break;
case 3: PWM_OUT = PWM_OUT_3; break;
case 4: PWM_OUT = PWM_OUT_4; break;
case 5: PWM_OUT = PWM_OUT_5; break;
}
PWMPulseWidthSet(PWM_BASE, PWM_OUT, duration );
}
void WRTimerRatioSet(long timer_out, long pulse_duration)
{
unsigned long duration = pulse_duration;
if( timer_out == 6 )
TimerMatchSet(TIMER0_BASE, TIMER_A, duration );
if( timer_out == 7 )
TimerMatchSet(TIMER0_BASE, TIMER_B, duration );
if( timer_out == 8 )
TimerMatchSet(TIMER1_BASE, TIMER_A, duration );
}
void WRPWMInit(void)
{
//
// Set the clocking to run directly from the crystal.
//
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//
// Set GPIO D0 and D1 as PWM pins. They are used to output the PWM0 and
// PWM1 signals.
//
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Compute the PWM period based on the system clock.
//
ulPeriod = SysCtlClockGet() / WR_PWMTIMER_RATE;
//
// Set the PWM period to ulRate Hz.
//
PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPeriod);
PWMGenConfigure(PWM_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, ulPeriod);
PWMGenConfigure(PWM_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, ulPeriod);
//
// Set PWM0 to a duty cycle of 25% and PWM1 to a duty cycle of 75%.
//
//PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, ulPeriod / 4);
//PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ulPeriod * 3 / 4);
//
// Enable the PWM0 and PWM1 output signals.
//
PWMOutputState(PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);
PWMOutputState(PWM_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true);
PWMOutputState(PWM_BASE, PWM_OUT_4_BIT | PWM_OUT_5_BIT, true);
//
// Enable the PWM generator.
//
PWMGenEnable(PWM_BASE, PWM_GEN_0);
PWMGenEnable(PWM_BASE, PWM_GEN_1);
PWMGenEnable(PWM_BASE, PWM_GEN_2);
}
void WRTimerInit(void)
{
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_4 ); //PD4
GPIOPinTypeTimer(GPIO_PORTC_BASE, GPIO_PIN_5 ); //PC5
//
// Enable processor interrupts.
//
//IntMasterEnable();
//
// Configure the two 16-bit periodic timers.
//
TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR
| TIMER_CFG_A_PWM | TIMER_CFG_B_PWM );
// TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM );
TimerControlLevel( TIMER0_BASE, TIMER_BOTH, true );
//TimerControlLevel( TIMER1_BASE, TIMER_BOTH, true );
//
// Compute the PWM period based on the system clock.
//
ulPeriod = SysCtlClockGet() / WR_PWMTIMER_RATE;
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, ulPeriod );
// TimerLoadSet(TIMER1_BASE, TIMER_BOTH, ulPeriod );
//
// Setup the interrupts for the timer timeouts.
//
//IntEnable(INT_TIMER0A);
//IntEnable(INT_TIMER1A);
//TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//
// Enable the timers.
//
TimerEnable( TIMER0_BASE, TIMER_BOTH );
//TimerEnable( TIMER1_BASE, TIMER_BOTH );
}
void WRPWMTimerEnable()
{
TimerEnable( TIMER0_BASE, TIMER_BOTH );
TimerEnable( TIMER1_BASE, TIMER_BOTH );
// PWMGenEnable(PWM_BASE, PWM_GEN_0);
// PWMGenEnable(PWM_BASE, PWM_GEN_1);
// PWMGenEnable(PWM_BASE, PWM_GEN_2);
}
void WRPWMTimerDisable()
{
TimerDisable( TIMER0_BASE, TIMER_BOTH );
TimerDisable( TIMER1_BASE, TIMER_BOTH );
// PWMGenDisable(PWM_BASE, PWM_GEN_0);
// PWMGenDisable(PWM_BASE, PWM_GEN_1);
// PWMGenDisable(PWM_BASE, PWM_GEN_2);
}