STM32 timer output compare active mode

Publisher:chwwdchLatest update time:2016-10-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The STM32 timer can also work in the output active mode TIM_OCMode_Active. The so-called active mode here, in layman's terms, is when the timer count value reaches the comparison value, it is forced to output a high level at the pin end. Here, I write a program to make the timer work in the output active mode, and then after a certain delay, the channel pin level will be forced to be pulled high.
Let me talk about the relevant code, which is based on my own standard project.
1. Modification of the project
1) The timer is used here, so you need to add stm32f10x_tim.h to the STM32F10x_StdPeriod_Driver project group.
2) Open the stm32f0x_conf.h file and remove the comment of the previously blocked statement: #include "stm32f10x_tim.h".
3) Create two new files, OCActive.c and OCActive.h, and save them in the src and inc folders of the BSP folder respectively. Then add OCActive.c to the BSP project group.
 
2. Writing OCActive.c and OCActive.h codes
The first thing is to initialize the pins of each channel of the timer. Here we choose TIM2, so we need to initialize the pins corresponding to each channel: PA0, PA1, PA2, PA3. The code is as follows:

/****************************************************************
Function: OCActive_GPIO_Init
Description: Output compare active mode timer channel pin configuration
Input: none
return: none
**************************************************************/
static void OCActive_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Timer channel pins are configured as multiplexed push-pull output
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

Note that since these pins are used as the output of each channel of the timer, they must be configured as multiplexed outputs, and here they are configured as multiplexed push-pull outputs. Another thing to know is that when the pin is configured as multiplexed push-pull output, the default output level of the pin is low. So when the timer is configured as the output comparison active mode, the pin will be pulled high, and we can see a rising edge.
Next is the configuration of the timer. Its code is as follows:

u16 CCR1_Val = 10000;
u16 CCR2_Val = 5000;
u16 CCR3_Val = 2500;
u16 CCR4_Val = 1250;

/****************************************************************
Function: OCActive_TIM2_Init
Description: Timer 2 is configured to output compare active mode
Input: none
return: none
**************************************************************/
static void OCActive_TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Initialize TIM2 clock

/* -------------------------------------------------------
The timer's beat is 720000000/36000/2=1kHz,
TIM_OCMode_Active mode forces the output to be high, and
each channel pin is originally at a low level. After the following delay, it jumps to a high level
TIM2_CH1 delay = CCR1_Val/1kHz = 10s
TIM2_CH2 delay = CCR2_Val/1kHz = 5s
TIM2_CH3 delay = CCR3_Val/1kHz = 2.5s
TIM2_CH4 delay = CCR4_Val/1kHz = 1.25s
---------------------------------------------------------*/
TIM_TimeBaseStructure.TIM_Period = 60000; //Timer counting period
TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1; //Prescaler
TIM_TimeBaseStructure.TIM_ClockDivision = 2 - 1; //Clock 2 division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Increase count
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //Initialize timer


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;//Output comparison active modeTIM_OCInitStructure.TIM_OutputState
= TIM_OutputState_Enable;//Output enableTIM_OCInitStructure.TIM_Pulse
= CCR1_Val;//Set comparison value (jump value)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//The effective level is high
levelTIM_OC1Init(TIM2, &TIM_OCInitStructure);//Initialize output compare registerTIM_OC1PreloadConfig
(TIM2, TIM_OCPreload_Disable);//Turn off preloadTIM_OCInitStructure.TIM_OutputState

= TIM_OutputState_Enable;//Output enableTIM_OCInitStructure.TIM_Pulse
= CCR2_Val;//Set comparison value (jump value)
TIM_OC2Init(TIM2, &TIM_OCInitStructure);//Initialize output compare register
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//Output enable
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;//Set comparison value (jump value)
TIM_OC3Init(TIM2, &TIM_OCInitStructure);//Initialize output compare register
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//Output enable
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;//Set comparison value (jump value)
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //Initialize output compare register
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);

TIM_ARRPreloadConfig(TIM2, ENABLE); //Turn on the automatic reload function of timer 2
TIM_Cmd(TIM2, ENABLE); //Turn on timer 2
}

First, set the timer's time base. Here, configure the timer's clock to be divided by 2, and the pre-division is 36000. In this case, the timer's clock frequency is 72M/2/36000=1kHz. Then set the comparison value of each channel. For example, set the comparison value of channel 1 to CCR1_Val = 10000. When the count value reaches this comparison value, the corresponding pin level will be forced to be pulled high. We calculate the time to reach this count value as 10000/1000=10s. In other words, the channel 1 pin originally outputs a low level, and after 10s, the timer count value reaches the comparison value and the pin level is pulled high! The other channel pins are similar: the pin corresponding to channel 2 is pulled high after a delay of 5s; the pin corresponding to channel 3 is pulled high after a delay of 2.5s; the pin level of channel 4 is pulled high after 1.25s.
Then write a total function: OCActive_Init() to initialize the above configuration. The code is as follows:

/****************************************************************
Function: OCActive_Init
Description: Output compare active M mode initialization
Input: none
return: none
*************************************************************/
void OCActive_Init(void)
{
OCActive_GPIO_Init();
OCActive_TIM2_Init();
}

Finally, the code of the OCActive.h file is as follows:

#ifndef __OCACTIVE_H__
#define __OCACTIVE_H__
#include "stm32f10x.h"

void OCActive_Init(void);

#endif

3. Writing the main function
The mian function is still very simple, the code is as follows:

/****************************************************** ************
Function: main
Description: mainInput
: none
return: none
************************* **********************************/
int main(void)
{
BSP_Init();
OCActive_Init() ;
PRINTF("\nmain() is running!\r\n");
while(1)
{
LED1_Toggle();
Delay_ms(1000);
}
}

4. Testing
Use the oscilloscope probe to test pins PA0, PA1, PA2, and PA3 respectively. Press the reset button of the development board each time during the test, and you can see the following phenomenon:
PA0 was originally at a low level, and was programmed to a high level after 10 seconds;
PA1 was originally at a low level, and was programmed to a high level after 5 seconds.
PA2 was originally at a low level, and was programmed to a high level after 2.5s;
PA3 was originally at a low level, and was programmed to a high level after 1.25s;
The following is the waveform phenomenon of the PA0 output detected by the oscilloscope, as shown in the figure below (although the change in time cannot be seen):
STM32 timer output compare active mode - ziye334 - ziye334's blog

Keywords:STM32 Reference address:STM32 timer output compare active mode

Previous article:STM32 timer output compare non-active mode
Next article:STM32 timer output comparison time mode

Latest Microcontroller Articles
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号