STM32 uses TIME3 and TIME4 to output a PWM waveform respectively to realize 2 LED breathing lights

foreword

This experiment is to use TIM3 and TIM4 to output a PWM waveform respectively. The duty cycle of PWM changes with time to drive an LED connected externally and the LED soldered on the minimum development board (fixed to the PC13 GPIO port), to achieve The effect of 2 LED breathing lights.

1. Introduction to PWM

1. Meaning

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.

2. Basic principles

PWM is to control the on-off of the switching device of the inverter circuit, so that the output terminal gets a series of pulses with equal amplitude, 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.

2. Create a project

1. Create a new project with STM32CubeMx

Click ACCESS TO MCU SELECTOR, select our core board (STM32F103C8) in Part Number
insert image description here
insert image description here

2. Configure RCC and SYS

Select RCC under the directory System Core, change HSE to Crystal/Ceramic Reasonator.
insert image description here
Click SYS, and then change Debug to Serial Wire
insert image description here

3. Configure the timer TIME3

1. Check Internal Clock (internal clock)
2. Channel 1 selection: PWM Generation CH1 (PWM output channel 1)
3. Prtscaler (timer frequency division factor): 71
4. Counter Mode (counting mode): Up (count up mode)
5. Counter Period (automatic reload value): 500
6. CKD (clock frequency division factor): No Division (no frequency division)
insert image description here

4. Configure timer TIM4

The configuration is the same as above
insert image description here

5. Configure the clock

Click CLOCK CONFIGURATION, then change HCLK to 72Mhz, remember to click Enter to save
insert image description here

6. Generate project

Click Project Manager, fill in the project name, and change the IDE to MDK-ARM
insert image description here
, click Code Generator, check the following, and click GENERATE CODE
insert image description here

3. Keil writes code

1. Define variables

Define a global variable in main.c

uint16_t pwm=0;   //占空比

insert image description here

2. Open the PWM channel

Add two lines of code to the main function:

	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); 
    HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1); 

insert image description here

3. Write the calling code in the while loop

while (pwm< 500)
	  {
    
    
		  pwm++;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);  
    __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  			
		  HAL_Delay(1);
	  }
	  while (pwm)
	  {
    
    
		  pwm--;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);    
       __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  
			
		  HAL_Delay(1);
	  }
	  HAL_Delay(200);

insert image description here

4. Hardware connection

USB to TTL
3V3 —> 3V3
GND —> GND
RXD —> A9
TXD —> A10
LED light connected to
LED short leg —> A6
LED light long leg —> 3V3
PB6 —> PC13
insert image description here

5. Burning

insert image description here

6. Results

7. Summary

Through this experiment, I understand the basic principle of PWM, and have a preliminary understanding of it, and use it to drive an external LED and the LED that has been soldered on the smallest development board (fixed to the PC13 GPIO port), to achieve The effect of 2 LED breathing lights.
Reference:
https://blog.csdn.net/qq_45237293/article/details/111997424

Guess you like

Origin blog.csdn.net/asdhnkhn/article/details/127739137