1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
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.
12 #include "core/pwm/pwm.h"
15 // Initialises PWM output on 16-bit Timer 1 and
16 // sets MAT0 (P1.9) as output
19 // Setup the pulse-width and duty-cycle
20 pwmSetDutyCycle(50); // Set 50% duty cycle
21 pwmSetFrequencyInMicroseconds(100); // 100 millisecond pulse width
23 // Enable PWM output for exactly 50 pulses
26 // Alternatively, enable PWM output indefinately
33 Software License Agreement (BSD License)
35 Copyright (c) 2010, microBuilder SARL
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.
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.
60 /**************************************************************************/
63 uint32_t pwmPulseWidth
= CFG_PWM_DEFAULT_PULSEWIDTH
;
64 uint32_t pwmDutyCycle
= CFG_PWM_DEFAULT_DUTYCYCLE
;
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;
71 /**************************************************************************/
73 Initialises 16-bit Timer 1, and configures the MAT0 output (pin 1.9)
74 to send the PWM output signal.
76 /**************************************************************************/
79 /* Enable the clock for CT16B1 */
80 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_CT16B1
);
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
;
88 /* Set default pulse width (MR3)*/
89 TMR_TMR16B1MR3
= pwmPulseWidth
;
91 /* Set default duty cycle (MR0) */
92 TMR_TMR16B1MR0
= (pwmPulseWidth
* (100 - pwmDutyCycle
)) / 100;
94 /* Configure match control register to reset on MR3 */
95 TMR_TMR16B1MCR
= (TMR_TMR16B1MCR_MR3_RESET_ENABLED
);
97 /* External Match Register Settings for PWM */
98 TMR_TMR16B1EMR
= TMR_TMR16B1EMR_EMC0_TOGGLE
| TMR_TMR16B1EMR_EM0
;
100 /* Disable Timer1 by default (enabled by pwmStart of pwmStartFixed) */
101 TMR_TMR16B1TCR
&= ~TMR_TMR16B1TCR_COUNTERENABLE_MASK
;
103 /* Enable PWM0 and PWM3 */
104 TMR_TMR16B1PWMC
= TMR_TMR16B1PWMC_PWM0_ENABLED
| TMR_TMR16B1PWMC_PWM3_ENABLED
;
106 /* Make sure that the timer interrupt is enabled */
107 NVIC_EnableIRQ(TIMER_16_1_IRQn
);
110 /**************************************************************************/
112 Starts the PWM output
114 /**************************************************************************/
117 /* Disable interrupt on MR3 in case it was enabled by pwmStartFixed() */
118 TMR_TMR16B1MCR
&= ~(TMR_TMR16B1MCR_MR3_INT_MASK
);
121 TMR_TMR16B1TCR
= TMR_TMR16B1TCR_COUNTERENABLE_ENABLED
;
124 /**************************************************************************/
128 /**************************************************************************/
132 TMR_TMR16B1TCR
&= ~(TMR_TMR16B1TCR_COUNTERENABLE_MASK
);
135 /**************************************************************************/
137 Starts the PWM output, and stops after the specified number of
141 The number of pulses to generate before disabling the
142 PWM output. The output is actually disabled in the
145 @warning The PWM output is actually stopped inside the 16-bit
146 timer ISR in "core/timer16/timer16.h".
148 /**************************************************************************/
149 void pwmStartFixed(uint32_t pulses
)
151 pwmMaxPulses
= pulses
;
153 /* Configure match control register to also raise an interrupt on MR3 */
154 TMR_TMR16B1MCR
|= (TMR_TMR16B1MCR_MR3_INT_ENABLED
);
156 /* Enable Timer1 (it will eventually be disabled in the ISR) */
157 TMR_TMR16B1TCR
= TMR_TMR16B1TCR_COUNTERENABLE_ENABLED
;
160 /**************************************************************************/
162 Sets the signal's duty cycle in percent (1-100).
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').
168 @returns -1 if an invalid percentage was supplied. Value must be
171 /**************************************************************************/
172 int pwmSetDutyCycle(uint32_t percentage
)
174 if ((percentage
< 1) || (percentage
> 100))
176 /* Duty Cycle must be a value between 1 and 100 */
180 /* Set Duty Cycle (MR0) */
181 TMR_TMR16B1MR0
= (pwmPulseWidth
* (100 - (pwmDutyCycle
= percentage
))) / 100;
186 /**************************************************************************/
188 Sets the signal's frequency/pulse-width to the specified number
192 The duration in clock ticks of each full pulse.
194 @returns -1 if a zero-value was provided for ticks.
196 /**************************************************************************/
197 int pwmSetFrequencyInTicks(uint16_t ticks
)
204 /* Set Pulse Width (MR3)*/
205 TMR_TMR16B1MR3
= (pwmPulseWidth
= ticks
- 1);
207 /* Adjust Duty Cycle (MR0) */
208 TMR_TMR16B1MR0
= (pwmPulseWidth
* (100 - pwmDutyCycle
)) / 100;
213 /**************************************************************************/
215 Sets the signal's frequency/pulse-width to the specified number
219 The duration in microseconds of each full pulse.
221 @returns -1 if the supplied value exceeds the limits of the 16-bit
222 timer, or if a zero-value was provided.
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.
230 /**************************************************************************/
231 int pwmSetFrequencyInMicroseconds(uint16_t us
)
238 uint32_t ticks
= (((CFG_CPU_CCLK
/SCB_SYSAHBCLKDIV
) / 1000000) * us
);
241 /* Delay exceeds the upper limit for the 16-bit timer */
245 /* Set Pulse Width (MR3)*/
246 TMR_TMR16B1MR3
= (pwmPulseWidth
= ticks
- 1);
248 /* Adjust Duty Cycle (MR0) */
249 TMR_TMR16B1MR0
= (pwmPulseWidth
* (100 - pwmDutyCycle
)) / 100;