Outcome is this!
Experimental environment
The machine spec and motor information are described in the table.
| host PC | DELL xps 13 | 
| host OS | ubuntu 22.04 | 
| IDE | STM32CubeIDE 1.13.1 | 
| microcontroller | NUCLEO STM32 F030R8T6 | 
| motor | GB12-N20B (chosen in this article) | 
The circuit is expressed as follows. This is based on the article about writing a schematic of a drone.
STM32 configuration and source code
Configuration
Choose TIM3 this time since Timer 3 can be used in all of STM32 series (ref).
I set the following configurations.
| Clock Source | Internal Clock | 
| Channel 1 | PWM Generation CH1 | 
| Prescaler | 0 (default) | 
| Counter Period | 255 (8 bit) | 
A screenshot of the above configuration is shown here. This is specified in a .ioc file.
PWM frequency is calculated as follows (ref).
$$ \mathrm{PWM \ frequency} = \frac{\mathrm{frequency \ of \ the \ clock}}{\mathrm{prescaler} \times \mathrm{counter \ period}}$$
※ As for STM32CubeIDE setting, we should subtract $1$ from the prescaler and the counter period values because they are 0-based (ref).
Here, the prescaler is $1$, the counter period is $256$, and the frequency of the clock is $8 \, \mathrm{Mhz}$, thus the PWM frequency is $31.25 \, \mathrm{kHz}$.
After setting the above configuration, automatically PA6 was chosen for the timer output PIN.
Referring to PIN layout shown below, we need to connect D12 to use PA6 for timer 3 channel 1.
Source code
The main source code is uploaded on github repository.
The point is the generation of PWM signals depending on time and creates a sine wave for PWM input. The data variable ranges from $95$ to $255$ and the period of the sine wave is around $6.28$ seconds.

  while (1)
  {
	double t = HAL_GetTick() / 1000.0;  // ms -> s
	const double gain = 80.0;
	const double offset = 175.0;
	int data = gain * sin(t) + offset;
	__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, data);
	printf("data: %d \n", data);
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }The function __HAL_TIM_SET_COMPARE() can change the PWM value dynamically (ref).
Reference
Some references are in Japanese but are quite well documented.
 
  
  
  
  








コメント