stm32 timer output comparison function different frequency

Publisher:云淡雅致Latest update time:2017-10-25 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Output comparison: Turn on a TIMx counter, and then turn on one or several output comparators of TIMx (4 in total). After all are configured, the counter starts counting. When the value in the counter is equal to the value in the comparison register, an output comparison interrupt is generated. In the interrupt, the value in the counter is read out, added to the rollover period, and written to the comparison register, so that it has the same rollover period as the next event.

The general meaning is that after the counter is turned on, the count value continues to increase. When it increases to the value of the comparison register, the level flips and a comparison interrupt is generated. In the comparison interrupt, the counter value is read out and added to the period, and written into the comparison register together, so that they have the same flip period.

For example, if the TIM clock frequency is set to 12 MHZ, and the self-load value in the output compare register is 600 (high level or low level count value), the output PWM frequency is

                   frequency = 12MHZ/(600*2)=10KHZ.

But the initial value can be set by yourself, in other words, the phase can be determined by yourself.

1. TIM3 clock and corresponding IO clock are enabled. 

TIM3 is mounted under APB1, so we enable TIM3 through the enable function under APB1 bus. The called function is: 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 mounted under APB1 clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Enable channel 1 PB1 of timer 3 and enable PB clock

2. GPIO structure initialization Because it is a timer output, it is a multiplexed push-pull output AF-PP

GPIO_InitTypeDef GPIO_InitStructure;

//Step1. Set PB.1 port as OC4 output port of TIM3

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

3. Set the timer mode to output compare mode

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 71; //The frequency division coefficient is 71, so the actual time base unit is 72M/(71+1)=1M

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up

TIM_TimeBaseStructure.TIM_Period = 0xffff; //Self-loaded value is 65535 special value

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

TIM_OCStructInit(&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //Trigger event level flip

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Enable output

TIM_OCInitStructure.TIM_Pulse = t; //initial value 10000

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Output polarity is high

TIM_OC4Init(TIM3,&TIM_OCInitStructure); //Channel 4

4. Because you want to enable the timer interrupt OC and set the interrupt priority

NVIC_InitTypeDef NVIC_InitStructure;

//Step 3. Enable TIM3 output compare match interrupt

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

5. Clear the interrupt flag and enable the interrupt to enable timer 3

TIM_ClearFlag(TIM3, TIM_FLAG_CC4); // Clear interrupt flag

TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE); //TIM3 interrupt source setting, enable capture comparison interrupt of corresponding channel

TIM_Cmd(TIM3, ENABLE); //First disable timer 3

6. Interrupt service function: void TIM3_IRQHandler(void)

7. First check whether the output comparison interrupt response is set    

if(TIM_GetITStatus(TIM3,TIM_IT_CC4) == SET)

8. Clear interrupt flag

TIM_ClearITPendingBit(TIM3, TIM_IT_CC4 );

9. Extract the current count value of Timer_CNT

capture = TIM_GetCapture4(TIM3); //Extract the current count value of Timer3

10. Reassign CCR. Next timer interrupt time.

TIM_SetCompare4(TIM3, capture + t); //need to accumulate the current count value

Frequency output calculation method: PCLK2/(TIM_Prescaler+1)/(CCR)/2=f

Assuming CCR is 1000 72M/(71+1)/1000/2=500HZ   

TIM_SetCompare4(TIM3, capture + t); Changing t means changing CCR.


Keywords:stm32 Reference address:stm32 timer output comparison function different frequency

Previous article:8 basic types of IO settings for stm32
Next article:STM32 standby wake-up operation

Recommended ReadingLatest update time:2024-11-16 16:40

stm32.cube (II)——HAL structure and initialization
1. HAL Structure The role of HAL is to abstract the basic register read and write operations so that programmers only need to care about the behavioral operations of the chip module. The cube package of stm32 is designed with a similar object-oriented concept. Each chip module is abstracted into a class. Its private m
[Microcontroller]
STM32ADC single conversion example
    ADC initialization steps: 1. Initialize the channel IO used by ADC 2. Turn on the ADC clock and initialize the ADC structure 3. Call the ADC_Cmd function to enable ADC, so that the ADC register can be read and written 4. Calibrate the ADC (this step is not necessary. When calibrating the ADC, the ADC hardware will
[Microcontroller]
STM32ADC single conversion example
Solution to the problem that the STM32 I/O port cannot output high and low levels normally
The I/O port cannot output normally because the port is reused. In addition to checking whether there is a program to reuse the port in the program, you should also pay attention to the following: some ports are reused by default when the microcontroller is powered on, such as the PA13, PA14, PA15, PB3, and PB4 pins r
[Microcontroller]
Solution to the problem that the STM32 I/O port cannot output high and low levels normally
STM32+74HC595: Guide you to use 74HC595 in 10 minutes
It uses STM32CBT8. The small module is very cost-effective and has rich resources. It is easy to use after porting u/COS, HTTP, MQTT protocols, etc. It is really addictive! BUT: The IO port resources are too limited. I want you to drive 100 LEDs. Please tell me that my requirements are too many and you cannot meet the
[Microcontroller]
STM32 programming uses TIM timer input capture function infrared remote control decoding
1. Main program #include "sys.h" #include "usart.h" #include "led.h" #include "lcd.h" #include "delay.h" #include "remote.h" #include "stm32f10x_tim.h" /* Program function: Use the timer input capture function of STM32 for infrared decoding.          When the key of the infrared remote control is pressed,
[Microcontroller]
STM32 study notes remapping function
Introduction: I'm learning STM32 recently. In a BZ article about serial communication, there is a piece of code: RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE); It was originally written with reference to the development of the inside and has always been on GPIOD or "RCC_APB2Periph_ ... Keyw
[Microcontroller]
STM32 study notes remapping function
Solve the problem that STM32 cannot simulate simulation
I've been learning RT-Thread recently, and I found that compiling and burning it into the development board each time is a waste of time, because it always becomes single-step mode once it is simulated. How to solve this problem? I searched for a method on the Internet, and after actual testing, the method is indeed
[Microcontroller]
A brief discussion on learning i2c in stm32
       First of all, let me introduce my learning background. I just got started with STM32 last week and learned the basic usage of GPIO ports and the introduction of interrupts. With such knowledge reserves, I began to learn the I2C communication protocol and tried to write a small task assigned by my senior.    
[Microcontroller]
A brief discussion on learning i2c in stm32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号