//The following is the TIM configuration program
void TIM1_PWM_INIT(void) //TIM1 configures PWM output parameters
{
u32 temp_fre,temp_duty;
TempC = 60; //Set the initial frequency value
TempD = 4; //Set the initial duty cycle 1 / 2
temp_fre = (1000000 / TempC) - 1;
temp_duty = ((temp_fre + 1) / TempD);
TIM_TimeBaseStructure.TIM_Prescaler = 72;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = temp_fre;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse
= temp_duty; TIM_OCInitStructure.TIM_OCPolarity
= TIM_OCPolarity_Low; InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High
; TIM_OCInitStructure.TIM_OCIdleState
= TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 counter enable */
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void TIM2_PWM_INIT(void)
{
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure. TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T1_H;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T2_H;
TIM_OC2Init(TIM M2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable) ;
/* Output Compare Toggle Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T3_H;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T4_H;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConf ig(TIM2, TIM_OCPreload_Disable);
/* TIM enable counter * /
TIM_Cmd(TIM2, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
}
void TIM3_PWM_IN_INIT(void)
{
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3 , &TIM_TimeBaseStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlave Mode(TIM3, TIM_SlaveMode_Reset) ;
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}
// The following is the interrupt handler
void TIM2_IRQHandler(void)
{
/* TIM2_CH1 toggling with frequency = 183.1 Hz */
static u8 i1,i2,i3,i4;
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
i1++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM2);
if(i1 % 2 == 1)
{
TIM_SetCompare1(TIM2, capture + T1_L);
}
else
{
TIM_SetCompare1(TIM2, capture + T1_H);
}
}
/* TIM2_CH2 toggling with frequency = 366.2 Hz */
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
i2++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM2);
if(i2 % 2 == 1)
{
TIM_SetCompare2(TIM2, capture + T2_L);
}
else
{
TIM_SetCompare2(TIM2, capture + T2_H);
}
}
/* TIM2_CH3 toggling with frequency = 732.4 Hz */
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
i3++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM2);
if(i3 % 2 == 1)
{
TIM_SetCompare3(TIM2, capture + T3_L);
}
else
{
TIM_SetCompare3(TIM2, capture + T3_H);
}
}
/* TIM2_CH4 toggling with frequency = 1464.8 Hz */
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
i4++;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM2);
if(i4 % 2 == 1)
{
TIM_SetCompare4(TIM2, capture + T4_L);
}
else
{
TIM_SetCompare4(TIM2, capture + T4_H);
}
}
}
/****************************************************** ******************************
* Function Name: TIM3_IRQHandler
* Description: This function handles TIM3 global interrupt request.
* Input: None
* Output: None
* Return: None
****************************************** *************************************/
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2) ;
IC2Value = TIM_GetCapture2(TIM3);
if (IC2Value != 0)
{
DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //Duty cycle
TempB = 1000000 / IC2Value; //Input PWM frequency
TempA = IC2Value / 1000 ; //Input PWM pulse width
}
else
{
DutyCycle = 0;
TempB = 0;
TempA = 0;
}
}
Previous article:STM32 output comparison error and solution
Next article:STM32 online upgrade QiFreeUSBS
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- [Modification] Both the boost board and the purifier are troublesome in voltage
- Where does the magnetic flux go in this case and what is its impact?
- EEWORLD University ---- ROS Courses
- TTI&TE Live: The development and latest applications of sensors in industrial motors
- Qorvo Biotechnologies Receives FDA Emergency Use Authorization (EUA) for Rapid COVID-19 Antigen Test
- TI 28335 and AD7606 communicate via SPI serial data
- 【i.MX6ULL】Driver Development 4——Light up the LED (Register Version)
- [Raspberry Pi 3B+ Review] Connect to WIFI & Log in to VNC & Update System Support
- IAR FOR AVR crashes within seconds when running under WIN10, please help me solve it
- A Guide to Hooking Up with Electronic Gangsters