2021-01-25 01:55 AM
Below are example code for Arduino provided in the Basic Esc documentation
#include <Servo.h>
byte servoPin = 9;
Servo servo;
void setup() {
servo.attach(servoPin);
servo.writeMicroseconds(1500); // send "stop" signal to ESC.
delay(7000); // delay to allow the ESC to recognize the stopped signal
}
void loop() {
int signal = 1700; // Set signal value, which should be between 1100 and 1900
servo.writeMicroseconds(signal); // Send signal to ESC.
}
2021-01-25 02:07 AM
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pulse_width);
The pulse width depends on the timer prescaler and period. You might set the prescaler such that timer clock is 1MHz. Then a 50 Hz period corresponds to a period length of 20000 (µs). Note that everything counts from 0. So you set the upper limit to 20000-1 and the prescaler to e.g. 48-1 if the SysClock is 48 MHz.
hth
KnarfB
2021-01-25 03:29 AM
Thank you. I`ve set it up using 48MHz, Prescaler 47, Counter Period 19999 and Pulse 10000. I got a 50 Hz signal with period length 20000 µs.
Below are my code. I get a 1500 µs pulse (Initzialization signal) but i want a delay for 2 seconds and then give a 1700 µs to run the motor. This does not happen?
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // Starter PWM
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1500);
HAL_Delay(2000);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1700);
/* USER CODE END WHILE */
Attached are oscilloscope print and motor diagram if needed.
2021-01-25 03:37 AM
Do you delay after the second setting? Should this be in a loop?
Scope isn't showing multiple seconds, diagram the waveform you actually want to see.
2021-01-25 03:56 AM
I initialize a 1500 µs pulse and then delay for 2 seconds. I tried to move it outside the loop (see code below).
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // Start PWM
/* USER CODE END 2 */
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1500); // Initialization signal
HAL_Delay(2000); // Delay 2 seconds
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1700); // Run motor
Still doesent change after 2 seconds to a 1700 µs pulse. Is it possible that i have reset the timer before sending a new signal?
2021-01-25 04:04 AM
No, you don't have to reset. The __HAL macro results in a single register write and that's fine.
Your code should work. Are you sure that the last line is reached, i.e. HAL_Delay is working okay (needs SysTick running, not in interrupt).
2021-01-25 04:19 AM
I tried to make a new prosject since i have change a lot of configurations during my troubleshooting.
Verifed the HAL_Delay on my oscilloscope, and now it changes to 1700 µs after 2 seconds!
Fantastic, but my motor wont run. If you guys cant fint anything wrong with my code i suspect the ESC to be defect..
Anyway thanks for your help
2 seconds delay 1700 µsSTM32CubeIDE setup
2021-01-25 04:29 AM
ESC has typically a dead-zone around the neutral value (although your diagram doesn't show one). You could carefully try larger values or put the scope at the ESC output.