Program 1: TIM2 output comparison, generating a square wave with a frequency of 366.2Hz
#include "stm32f10x.h"
void RCC_configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE); //Turn on the GPIO clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
}
void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //Define the structure for GPIO initialization
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; //Select pin 1, channel 2 of TIM2
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //Select speed 50MHZ
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
GPIO_Init(GPIOA,&GPIO_InitStruct); //Initialize IO port A
}
void NVIC_Configuration(void) //NVIC configuration
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Interrupt group 0
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0); //Interrupt vector FLASH
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority 0
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; //Sub priority 0
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //Interrupt vector enable
NVIC_Init(&NVIC_InitStruct);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_BaseInitStruct.TIM_Period = 65535; //Reload value
TIM_BaseInitStruct.TIM_Prescaler = 2; //Prescaler value
TIM_BaseInitStruct.TIM_ClockDivision = 0;
TIM_BaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //Count up 0 ~ Period
TIM_TimeBaseInit(TIM2,&TIM_BaseInitStruct); //Timer register initialization
/************************Channel 1, the generated frequency is 366HZ************/
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Toggle; //Select timer mode: TIM pulse width modulation mode 2
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //Comparison output enable
TIM_OCInitStruct.TIM_Pulse = 32768; //Set the pulse value to be loaded into the capture compare
registerTIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low; //Output polarity: TIM output compare polarity highTIM_OC1Init
(TIM2, &TIM_OCInitStruct); //Initialize the peripheral TIMx according to the parameters specified in TIM_OCInitStruct
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //Enable TIMx preload register on CCR2TIM_ITConfig
(TIM2,TIM_IT_CC1,ENABLE); //TIM interrupt
enableTIM_ARRPreloadConfig(TIM2, ENABLE); //Enable TIMx preload register on
ARRTIM_Cmd(TIM2,ENABLE); //Timer enable
}
int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
RCC_configuration();
Led_Init();
NVIC_Configuration();
TIM2_Configuration();
while (1)
{
}
}
/* *******************Interrupt function************************/
void TIM2_IRQHandler( void)
{
uint16_t capture = 0;
/**************Channel 1,366.2HZ************************/
if(TIM_GetITStatus( TIM2,TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2,capture + 32767);
}
}
Question 1: Why is my output not a 1:1 square wave, but a square wave with a duty cycle of 1:3? Question 2: Why is my output frequency 183.1, that is, TIM2CLK = 36M, but when I change to PWM mode, the measured TIM2CLK = 72M? Is it because the TIM multiplier does not work in output comparison mode?
The problem is: TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //Enable TIMx preload register on CCR2 Because I am using comparison output and setting the frequency in the interrupt, I use the function TIM_SetCompare1(TIM2,capture + 32767) to change the value of the comparison register. Therefore, when the preload register is enabled, the value of the compare register is reassigned every time an interrupt occurs, which conflicts with the frequency set in the interrupt. So the solution is TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable). In this way, one timer can be used to generate 4 square waves with different frequencies and different duty cycles. If there is anything wrong, I hope you can point it out.
Previous article:stm32 12864 driver
Next article:STM32 output comparison mode outputs pulses with fixed frequency
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- EEWORLD University ---- SimpleLink Academy: Develop your Bluetooth? Low Energy project
- dsPIC33EP32MC204PWM current limiting function problem
- Do you have any recommendations for low-power encryption products? I am using this on a smart device and have strict requirements on power consumption.
- Detailed explanation of diagrams: A quick guide for electronic engineers to read diagrams
- Please help me analyze an op amp circuit
- Microcontroller port input problem?
- Operation of EEPROM inside PIC microcontroller
- [Zhongke Bluexun AB32VG1 RISC-V board "running into" RTT evaluation] 4: Mount the file system and read the SD card
- Will the voice signal remain the original sound after being processed in this way?
- 【GD32307E-START】+MP3 playback control