1. After setting of BLE connection, another part of work is PWM-controlled motor for propellers. This is especially easy with mbed.
Since the PWM is embedded in mbedos system, include "mbed.h" first.
#include "mbed.h"
Then create one PWM handle, set the I/O pin. This pin can be in input and output state at the same time. pwm.write() and pwm.read() are available. This demo shows how to blink LED1 as of PWM siganls.
DigitalOut my_led(LED1); InterruptIn my_button(USER_BUTTON); PwmOut my_pwm(PB_3); ... ... // Set PWM my_pwm.period_ms(10); my_pwm.write(0.5);
Pressing the bottom , the square wave form is changed accordingly.
if (my_pwm.read() == 0.25) { my_pwm.write(0.75); } else { my_pwm.write(0.25); }
2. Full code is,
#include "mbed.h" DigitalOut my_led(LED1); InterruptIn my_button(USER_BUTTON); PwmOut my_pwm(PB_3); void pressed() { if (my_pwm.read() == 0.25) { my_pwm.write(0.75); } else { my_pwm.write(0.25); } } int main() { // Set PWM my_pwm.period_ms(10); my_pwm.write(0.5); // Set button my_button.fall(&pressed); while (1) { my_led = !my_led; wait(0.5); // 500 ms } }
3. Herein, I would show what normal IDE would do,
int main(void) { /* USER CODE BEGIN 1 */ int i=100; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* System interrupt init*/ /* Sets the priority grouping field */ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM10_Init(1000,100);//PWM /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim10,TIM_CHANNEL_1);//PWM /* USER CODE END 2 */ /* USER CODE BEGIN 3 */ /* Infinite loop */ while (1) { if(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13))//button pressed { i=i+100; if(i==1000) i=100; HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1);//PW间为iM
MX_TIM10_Init(1000,i);//PWM reset, HAL_TIM_PWM_Start(&htim10,TIM_CHANNEL_1);//PWM while(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)); } } /* USER CODE END 3 */ }
Then, first, start hardware, then config the timer, setting of interruption, then PMW can be started and control by botton.
There would be more to think about, and more control over your hardware.
STM32L476 provide clock source like real time clock(RTC), watchdog clock(WTC), low power time(LPTMx), etc. but in mbed, you can see nothing in regarding to the selection of timer.
4. While, up to now, mbed is good enough for my project.