[STM32CubeMX] Use STM32F103C8T6 to output PWM waveform to realize breathing light

[STM32CubeMX] Use STM32F103C8T6 to output PWM waveform to realize breathing light

1. About PWM

1. About
the meaning of
PWM   PWM (Pulse Width Modulation) is pulse width modulation, referred to as pulse width modulation. It is a very effective technology that uses the digital output of the microprocessor to control the analog circuit; it is an analog control method that modulates the bias of the transistor base or MOS transistor gate according to the change of the corresponding load. To realize the change of the conduction time of the transistor or MOS tube, thereby realizing the change of the output of the switching regulated power supply.
The basic principle of
  PWM is to control the on-off of the switching device of the inverter circuit, so that the output terminal obtains a series of pulses with equal amplitudes, and these pulses are used to replace the sine wave or the required waveform. It can also be understood that PWM is a method of digitally encoding the level of an analog signal. Through the use of high-resolution counters, the duty cycle of the square wave is modulated to encode the level of a specific analog signal. The PWM signal is still digital because at any given moment, full-scale DC power is either completely on (ON) or not at all (OFF). A voltage or current source is applied to an analog load in a repetitive pulse sequence of ON or OFF. Any analog value can be encoded using PWM as long as the bandwidth is sufficient.
Advantages and application range
  Due to its simple control, flexibility and good dynamic response, it has become the most widely used control method in power electronics technology. Its application fields include measurement, communication, power control and conversion, motor control, servo control, dimming, Switching power supplies, and even some audio amplifiers, so learning PWM has very important practical significance.
2. PWM on STM32
PWM generates

  STM32 timers except TIM6 and 7, other timers can be used to generate PWM output. Among them, the advanced timers TIM1 and TIM8 can generate up to seven PWM outputs at the same time. The general-purpose timer can also generate up to 4 PWM outputs at the same time, so that STM32 can generate up to 30 PWM outputs at the same time.
  The pulse width modulation mode can generate a signal with a frequency determined by the TIMx_ARR register and a duty cycle determined by the TIMx_CCRx register. The timer block diagram of the general-purpose timer generating PWM is as follows: (other timer block diagrams are similar)Please add a picture description

PWM related registers
Contains three registers:

Capture/Compare Mode Register (TIMx_CCMR1/2), Capture/Compare Enable Register (TIMx_CCER), Capture/Compare Register (TIMx_CCR1~4).
Set the OCxPE bit of the TIMx_CCMRx register to enable the corresponding preload register, and finally set the ARPE bit of the TIMx_CR1 register (in up-counting or centrosymmetric mode) to enable the auto-reload preload register. Writing 110 (PWM mode 1) or 111 (PWM mode 2) to the OCxM bit in the TIMx_CCMRx register can independently set each OCx output channel to generate a PWM.

Capture/Compare Mode Register (TIMx_CCMRx)
The following figure shows the description of each bit of the TIMx_CCMR1 register:

Please add a picture description

What needs to be used here is the mode setting bit OCxM. There are two PWM modes in total. The difference between these two PWM modes is that the polarity of the output level is opposite.
110: PWM mode 1. When counting up, once TIMx_CNT<TIMx_CCR1, channel 1 is an active level, otherwise it is an invalid level; when counting down, once TIMx_CNT>TIMx_CCR1, channel 1 is an inactive level (OC1REF=0), otherwise it is an active level flat (OC1REF=1).

111: PWM mode 2. When counting up, once TIMx_CNT<TIMx_CCR1, channel 1 is an inactive level, otherwise it is an active level; when counting down, once TIMx_CNT>TIMx_CCR1, channel 1 is an active level, otherwise it is an inactive level.

Capture/Compare Enable Register (TIMx_CCER)
The following figure shows the description of each bit of the TIMx_CCER register:

Please add a picture description

This register controls the switch of each input and output channel. Only the CC2E bit is used here. This bit is the input/capture 2 output enable bit. If you want the PWM to output from the I/O port, this bit must be set to 1.
Capture/Compare Register (TIMx_CCRx)
The following figure shows the description of each bit of the TIMx_CCR1 register:

Please add a picture description

In the output mode, the value of this register is compared with the value of CNT, and an output signal is generated on the OC1 port according to the comparison result. Taking advantage of this, we can control the output pulse width of PWM by modifying the value of this register.

2. Cube MX creation project

Refer to the previous Cube MX general settings, this tutorial only explains the special features, you can also jump to view the detailed steps STM32CUBEMX_PWM-based breathing light
insert image description here
clock configuration
insert image description here
generate code

3. Modify the code

Add the following code to main.c


#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);				//¿ªÆôtim1ͨµÀ1¼ÆʱÆ÷
   
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);				//¿ªÆôtim1ͨµÀ4¼ÆʱÆ÷
  /* USER CODE END 2 */
	int i=0,flag=0;
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	
	 if(flag==0)
	 {
		 i+=10;
		 if(i>400)
			flag=1;
	 }
	 else
	 {
		i-=10;
		if(i<5)
			flag=0;
	 }
	 //PAB2×ÜÏßʱÖÓΪ72Mhz£¬¾­¹ý72·ÖƵºóΪ1Mhz£¬¼ÆÊýÖÜÆÚRCCΪ65535
	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1,i);				  //LED1ÁÁ¶ÈÖð½¥Ôö¼Ó
	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4,420-i);			//LED2ÁÁ¶ÈÖð½¥½µµÍ
//	  HAL_Delay(20);
	 
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4,400-i);
//	  HAL_Delay(20);
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 200);
//	  HAL_Delay(100);
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 300);
//	  HAL_Delay(100);
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 400);
//	  HAL_Delay(100);
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 300);
//	  HAL_Delay(100);	  
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 200);
//	  HAL_Delay(100);
//	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 100);
	  HAL_Delay(100);

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

4. Effect display

6f4c8d52c22ad6

V. Summary

This experiment is mainly to learn the relevant knowledge of PWM, understand and understand PWM, and try to apply it in this experiment. Of course, some problems were also encountered in this experiment. These questions also deepened the understanding of PWM. .

6. Reference and code download

Reference: Use STM32 to output PWM waveform
project download
Extraction code: 8520

Guess you like

Origin blog.csdn.net/qq_52201641/article/details/127581116