c) Initialization function definition:
void TIM_Configuration(void); //Define TIM initialization function
d) Initialization function call:
TIM_Configuration(); //TIM initialization function call
e) Initialization function, different from the previous module, TIM initialization is divided into two parts - basic initialization and channel initialization:
void TIM_Configuration(void) //TIM initialization function
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Timer initialization structure
TIM_OCInitTypeDef TIM_OCInitStructure; //Channel output initialization structure
//TIM3 initialization
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //Period 0~FFFF
TIM_TimeBaseStructure.TIM_Prescaler = 5; //Clock division
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Clock division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ModeTIM_TimeBaseInit
(TIM3, &TIM_TimeBaseStructure); //Basic initializationTIM_ITConfig
(TIM3, TIM_IT_CC4, ENABLE); //Turn on interrupt, interrupt needs this line of code
//TIM3 channel initializationTIM_OCStructInit
(& TIM_OCInitStructure); //Default
parametersTIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Working stateTIM_OCInitStructure.TIM_OutputState
= TIM_OutputState_Enable; //Set to output, this line of code is required for PWM outputTIM_OCInitStructure.TIM_Pulse
= 0x2000; //Duty lengthTIM_OCInitStructure.TIM_OCPolarity
= TIM_OCPolarity_High; //High levelTIM_OC4Init
(TIM3, &TIM_OCInitStructure); //Channel initialization
TIM_Cmd(TIM3, ENABLE); //Start TIM3
}
f) Add TIM clock start in RCC initialization function:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3, ENABLE);
g) Set the input and output pin modes in GPIO. Signal: AF_PP, 50MHz.
h) If you use interrupts, add the following code to NVIC:
//Open TIM2 interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; //Channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Preemption level
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Response level
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Start
NVIC_Init(&NVIC_InitStructure); //Initialization
Interrupt code:
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) //Judge the interrupt source
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); //Clear interrupt flag
GPIO_WriteBit(GPIOB, GPIO_Pin_11, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_11)));//Change LED color
IC4value = TIM_GetCapture4(TIM2); //Get captured value
}
}
i) Simple application:
//Change duty cycle
TIM_SetCompare4(TIM3, variable);
j) Note:
The IO output mode of the pin is determined according to the application. For example, if PWM output is used to drive the LED, the corresponding pin should be set to AF_PP, otherwise the microcontroller will have no output.
Keywords:STM32
Reference address:PWM output based on STM32
void TIM_Configuration(void); //Define TIM initialization function
d) Initialization function call:
TIM_Configuration(); //TIM initialization function call
e) Initialization function, different from the previous module, TIM initialization is divided into two parts - basic initialization and channel initialization:
void TIM_Configuration(void) //TIM initialization function
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Timer initialization structure
TIM_OCInitTypeDef TIM_OCInitStructure; //Channel output initialization structure
//TIM3 initialization
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //Period 0~FFFF
TIM_TimeBaseStructure.TIM_Prescaler = 5; //Clock division
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Clock division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ModeTIM_TimeBaseInit
(TIM3, &TIM_TimeBaseStructure); //Basic initializationTIM_ITConfig
(TIM3, TIM_IT_CC4, ENABLE); //Turn on interrupt, interrupt needs this line of code
//TIM3 channel initializationTIM_OCStructInit
(& TIM_OCInitStructure); //Default
parametersTIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Working stateTIM_OCInitStructure.TIM_OutputState
= TIM_OutputState_Enable; //Set to output, this line of code is required for PWM outputTIM_OCInitStructure.TIM_Pulse
= 0x2000; //Duty lengthTIM_OCInitStructure.TIM_OCPolarity
= TIM_OCPolarity_High; //High levelTIM_OC4Init
(TIM3, &TIM_OCInitStructure); //Channel initialization
TIM_Cmd(TIM3, ENABLE); //Start TIM3
}
f) Add TIM clock start in RCC initialization function:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3, ENABLE);
g) Set the input and output pin modes in GPIO. Signal: AF_PP, 50MHz.
h) If you use interrupts, add the following code to NVIC:
//Open TIM2 interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; //Channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Preemption level
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Response level
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Start
NVIC_Init(&NVIC_InitStructure); //Initialization
Interrupt code:
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) //Judge the interrupt source
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); //Clear interrupt flag
GPIO_WriteBit(GPIOB, GPIO_Pin_11, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_11)));//Change LED color
IC4value = TIM_GetCapture4(TIM2); //Get captured value
}
}
i) Simple application:
//Change duty cycle
TIM_SetCompare4(TIM3, variable);
j) Note:
The IO output mode of the pin is determined according to the application. For example, if PWM output is used to drive the LED, the corresponding pin should be set to AF_PP, otherwise the microcontroller will have no output.
Previous article:STM32 learning three
Next article:STM32 PB2 (BOOT1) usage precautions
Recommended ReadingLatest update time:2024-11-16 15:49
Detailed explanation of the magic of STM32 buttons
Assume that there are three ports PB6, 7, and 4: If pressed, it is low level (0); otherwise, it is high level (1). void ReadKey(void) { //1. Get the GPIB- IDR register value (get the key value) u16 ReadData = (GPIO_InputData( GPIOB ) & 0xD0) ^ 0xD0; //2. Determine which bit h
[Microcontroller]
STM32 hardware IIC and 51 analog IIC communication
IIC Introduction The IIC protocol stipulates that the data transmitted on SDA must remain stable during the high level of SCL, and the data on SDA can only change during the low level of SCL. During IIC, data is placed on SDA at the rising edge of the pulse, and data is read from SAD at the falling edge of the pul
[Microcontroller]
STM32 timer timing calculation formula
Tout = ((arr+1)*(psc+1))/Tclk; in: Tclk: Timer input clock frequency (unit: MHZ) Tout: Timer overflow time (unit: us) .TIM_Period = arr; eg; 4999 .TIM_Prescaler = psc; eg: 7199 Tout = ((4999+1) × (7199+1))/72 = 500000us = 500ms.
[Microcontroller]
First look at I2C with STM32
1. Communication protocol. I2C is a two-wire serial bus developed by PHILIPS and is a synchronous half-duplex bus. Data is valid When transmitting data, the SDA line must remain stable during the high level period of the clock. The high or low level state of SDA can only change when the clock signal of the SCL line
[Microcontroller]
STM32 IO configuration lighting
1. Specific code of led.c: /*----------------------------------------------------------*/ #include "led.h" /* ------------------------------------------------------------------------- File name: led.c Description: Configure the LED port according to the hardware connection and open the corresponding register ----
[Microcontroller]
STM32F4 study notes 5——The highest bit error of the data sent by the stm32 serial port
Recently, I encountered a problem with the data transmission of the stm32 serial port when doing ModBus protocol communication based on the stm32f401 serial port. I spent a whole day looking for the problem, from the ModBus protocol format, scheduling algorithm to the serial port configuration, and finally sol
[Microcontroller]
How to use TXE and TC flags when STM32 USART sends data
There are two registers at the transmitting end of USART, one is the USART_DR register (the shaded TDR in the figure below) that can be seen by the program, and the other is the shift register (the shaded Transmit Shift Register in the figure below) that cannot be seen by the program.
There are two flags correspondi
[Microcontroller]
Implementation of STM32+IAP solution under IAR environment
1. What is IAP and why do we need IAP?
IAP stands for In Application Programming. Generally, the devices with STM32F10x series chips as the main controller have already used J-Link emulator to burn the application code before leaving the factory. If the application code needs to be replaced or upgraded during th
[Microcontroller]
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Excuse me, what is this component with silk screen 6023?
- Analysis of the working principles of seven types of three-pole emitter DC circuits (updated)
- Design and implementation of FIR filter in DSP
- Some Allegro practical skills that PCB Layout engineers regret not knowing earlier
- Buck-Boost and Step-Up Converters in Wireless Security Cameras and Video Doorbells
- EEWORLD University Hall----Operating System Zhejiang University (Li Shanping)
- TI CapTIvate technology adds capacitive touch capabilities to stoves
- PCB short circuit problem
- Fanuc PLC help newbie
- MFIA Impedance Analyzer Free Trial Application