
One of several PWM examples for the STM32L432 nucleo board
Dependencies: mbed
main.cpp@0:18d54d648967, 2019-03-26 (annotated)
- Committer:
- f3d
- Date:
- Tue Mar 26 12:37:18 2019 +0000
- Revision:
- 0:18d54d648967
Simple single channel PWM for the STM32L432KC Nucleo board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:18d54d648967 | 1 | #include "mbed.h" |
f3d | 0:18d54d648967 | 2 | #include <stm32l432xx.h> |
f3d | 0:18d54d648967 | 3 | |
f3d | 0:18d54d648967 | 4 | |
f3d | 0:18d54d648967 | 5 | #define PWM_PERIOD_COUNT 1000 |
f3d | 0:18d54d648967 | 6 | |
f3d | 0:18d54d648967 | 7 | /* |
f3d | 0:18d54d648967 | 8 | For register definitions see here: |
f3d | 0:18d54d648967 | 9 | https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/stm32l432xx.h |
f3d | 0:18d54d648967 | 10 | */ |
f3d | 0:18d54d648967 | 11 | #define BUF_SIZE 99 |
f3d | 0:18d54d648967 | 12 | |
f3d | 0:18d54d648967 | 13 | // Using the MBED libraries to configure the PWM outputs (its easier than |
f3d | 0:18d54d648967 | 14 | // calculating all of the register contents by hand) |
f3d | 0:18d54d648967 | 15 | DigitalOut myled(LED1); |
f3d | 0:18d54d648967 | 16 | PwmOut PhaATop(PA_8); |
f3d | 0:18d54d648967 | 17 | |
f3d | 0:18d54d648967 | 18 | void initTimer1() |
f3d | 0:18d54d648967 | 19 | { |
f3d | 0:18d54d648967 | 20 | |
f3d | 0:18d54d648967 | 21 | TIM1->CR1 = 0; // make sure Counter is disabled before changing configuration |
f3d | 0:18d54d648967 | 22 | TIM1->CR2 = 0; |
f3d | 0:18d54d648967 | 23 | TIM1->PSC = 1; |
f3d | 0:18d54d648967 | 24 | TIM1->ARR = PWM_PERIOD_COUNT; |
f3d | 0:18d54d648967 | 25 | TIM1->CCR1 = PWM_PERIOD_COUNT/4; // 25% duty |
f3d | 0:18d54d648967 | 26 | // Enable channel 1 |
f3d | 0:18d54d648967 | 27 | TIM1->CCER = 1; |
f3d | 0:18d54d648967 | 28 | TIM1->SR = 0; // Clear flags. |
f3d | 0:18d54d648967 | 29 | TIM1->CR1 |= 1; // enable counter |
f3d | 0:18d54d648967 | 30 | |
f3d | 0:18d54d648967 | 31 | } |
f3d | 0:18d54d648967 | 32 | |
f3d | 0:18d54d648967 | 33 | int main() { |
f3d | 0:18d54d648967 | 34 | |
f3d | 0:18d54d648967 | 35 | initTimer1(); |
f3d | 0:18d54d648967 | 36 | while(1) { |
f3d | 0:18d54d648967 | 37 | |
f3d | 0:18d54d648967 | 38 | } |
f3d | 0:18d54d648967 | 39 | } |