Init nochmal verkürzt.
[hackover2013-badge-firmware.git] / core / pwm / pwm.c
1 /**************************************************************************/
2 /*!
3 @file pwm.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Simple PWM example that can be used to control a motor, dim
7 an LED, etc. Uses 16-bit Timer 1 and P1.9 for PWM output.
8
9 @section Example
10
11 @code
12 #include "core/pwm/pwm.h"
13 ...
14
15 // Initialises PWM output on 16-bit Timer 1 and
16 // sets MAT0 (P1.9) as output
17 pwmInit();
18
19 // Setup the pulse-width and duty-cycle
20 pwmSetDutyCycle(50); // Set 50% duty cycle
21 pwmSetFrequencyInMicroseconds(100); // 100 millisecond pulse width
22
23 // Enable PWM output for exactly 50 pulses
24 pwmStartFixed(50);
25
26 // Alternatively, enable PWM output indefinately
27 pwmStart();
28
29 @endcode
30
31 @section LICENSE
32
33 Software License Agreement (BSD License)
34
35 Copyright (c) 2010, microBuilder SARL
36 All rights reserved.
37
38 Redistribution and use in source and binary forms, with or without
39 modification, are permitted provided that the following conditions are met:
40 1. Redistributions of source code must retain the above copyright
41 notice, this list of conditions and the following disclaimer.
42 2. Redistributions in binary form must reproduce the above copyright
43 notice, this list of conditions and the following disclaimer in the
44 documentation and/or other materials provided with the distribution.
45 3. Neither the name of the copyright holders nor the
46 names of its contributors may be used to endorse or promote products
47 derived from this software without specific prior written permission.
48
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
50 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
53 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 /**************************************************************************/
61 #include "pwm.h"
62
63 uint32_t pwmPulseWidth = CFG_PWM_DEFAULT_PULSEWIDTH;
64 uint32_t pwmDutyCycle = CFG_PWM_DEFAULT_DUTYCYCLE;
65
66 // pwmMaxPulses is used by TIMER16_1_IRQHandler to turn PWM off after
67 // a specified number of pulses have been sent. This only relevant when
68 // pwmStartFixed() is used.
69 volatile uint32_t pwmMaxPulses = 0;
70
71 /**************************************************************************/
72 /*!
73 Initialises 16-bit Timer 1, and configures the MAT0 output (pin 1.9)
74 to send the PWM output signal.
75 */
76 /**************************************************************************/
77 void pwmInit(void)
78 {
79 /* Enable the clock for CT16B1 */
80 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
81
82 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
83 /* Alternatively, PIO1.10 (MAT1) can also be used though
84 the initialisation code will need to be modified */
85 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
86 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
87
88 /* Set default pulse width (MR3)*/
89 TMR_TMR16B1MR3 = pwmPulseWidth;
90
91 /* Set default duty cycle (MR0) */
92 TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - pwmDutyCycle)) / 100;
93
94 /* Configure match control register to reset on MR3 */
95 TMR_TMR16B1MCR = (TMR_TMR16B1MCR_MR3_RESET_ENABLED);
96
97 /* External Match Register Settings for PWM */
98 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
99
100 /* Disable Timer1 by default (enabled by pwmStart of pwmStartFixed) */
101 TMR_TMR16B1TCR &= ~TMR_TMR16B1TCR_COUNTERENABLE_MASK;
102
103 /* Enable PWM0 and PWM3 */
104 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED | TMR_TMR16B1PWMC_PWM3_ENABLED;
105
106 /* Make sure that the timer interrupt is enabled */
107 NVIC_EnableIRQ(TIMER_16_1_IRQn);
108 }
109
110 /**************************************************************************/
111 /*!
112 Starts the PWM output
113 */
114 /**************************************************************************/
115 void pwmStart(void)
116 {
117 /* Disable interrupt on MR3 in case it was enabled by pwmStartFixed() */
118 TMR_TMR16B1MCR &= ~(TMR_TMR16B1MCR_MR3_INT_MASK);
119
120 /* Enable Timer1 */
121 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
122 }
123
124 /**************************************************************************/
125 /*!
126 Stops the PWM output
127 */
128 /**************************************************************************/
129 void pwmStop(void)
130 {
131 /* Disable Timer1 */
132 TMR_TMR16B1TCR &= ~(TMR_TMR16B1TCR_COUNTERENABLE_MASK);
133 }
134
135 /**************************************************************************/
136 /*!
137 Starts the PWM output, and stops after the specified number of
138 pulses.
139
140 @param[in] pulses
141 The number of pulses to generate before disabling the
142 PWM output. The output is actually disabled in the
143 timer ISR.
144
145 @warning The PWM output is actually stopped inside the 16-bit
146 timer ISR in "core/timer16/timer16.h".
147 */
148 /**************************************************************************/
149 void pwmStartFixed(uint32_t pulses)
150 {
151 pwmMaxPulses = pulses;
152
153 /* Configure match control register to also raise an interrupt on MR3 */
154 TMR_TMR16B1MCR |= (TMR_TMR16B1MCR_MR3_INT_ENABLED);
155
156 /* Enable Timer1 (it will eventually be disabled in the ISR) */
157 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
158 }
159
160 /**************************************************************************/
161 /*!
162 Sets the signal's duty cycle in percent (1-100).
163
164 @param[in] percentage
165 The duty-cycle in percentage (the amount of time that
166 the signal is 'high' relative to the time its 'low').
167
168 @returns -1 if an invalid percentage was supplied. Value must be
169 between 1 and 100.
170 */
171 /**************************************************************************/
172 int pwmSetDutyCycle(uint32_t percentage)
173 {
174 if ((percentage < 1) || (percentage > 100))
175 {
176 /* Duty Cycle must be a value between 1 and 100 */
177 return -1;
178 }
179
180 /* Set Duty Cycle (MR0) */
181 TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - (pwmDutyCycle = percentage))) / 100;
182
183 return 0;
184 }
185
186 /**************************************************************************/
187 /*!
188 Sets the signal's frequency/pulse-width to the specified number
189 of ticks.
190
191 @param[in] ticks
192 The duration in clock ticks of each full pulse.
193
194 @returns -1 if a zero-value was provided for ticks.
195 */
196 /**************************************************************************/
197 int pwmSetFrequencyInTicks(uint16_t ticks)
198 {
199 if (ticks < 1)
200 {
201 return -1;
202 }
203
204 /* Set Pulse Width (MR3)*/
205 TMR_TMR16B1MR3 = (pwmPulseWidth = ticks - 1);
206
207 /* Adjust Duty Cycle (MR0) */
208 TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - pwmDutyCycle)) / 100;
209
210 return 0;
211 }
212
213 /**************************************************************************/
214 /*!
215 Sets the signal's frequency/pulse-width to the specified number
216 of microseconds.
217
218 @param[in] us
219 The duration in microseconds of each full pulse.
220
221 @returns -1 if the supplied value exceeds the limits of the 16-bit
222 timer, or if a zero-value was provided.
223
224 @Warning Because a 16-bit timer is used here by default, the
225 maximum frequency is quite small. Running at 36MHz, the
226 largest possible pulse-width/frequency is ~1,82mS or
227 1820 microSeconds. At 12MHz its 5461 uS, at 48MHz
228 its 1365 uS, at 72MHz its 909uS.
229 */
230 /**************************************************************************/
231 int pwmSetFrequencyInMicroseconds(uint16_t us)
232 {
233 if (us < 1)
234 {
235 return -1;
236 }
237
238 uint32_t ticks = (((CFG_CPU_CCLK/SCB_SYSAHBCLKDIV) / 1000000) * us);
239 if (ticks > 0xFFFF)
240 {
241 /* Delay exceeds the upper limit for the 16-bit timer */
242 return -1;
243 }
244
245 /* Set Pulse Width (MR3)*/
246 TMR_TMR16B1MR3 = (pwmPulseWidth = ticks - 1);
247
248 /* Adjust Duty Cycle (MR0) */
249 TMR_TMR16B1MR0 = (pwmPulseWidth * (100 - pwmDutyCycle)) / 100;
250
251 return 0;
252 }
253
254
This page took 0.069342 seconds and 5 git commands to generate.