STM32 timer single pulse mode

Publisher:TranquilMind88Latest 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 be set to single pulse mode (OPM). The so-called single pulse is to generate a pulse with controllable pulse width after a certain controllable delay through the program. The delay time and pulse width here can be set, mainly by comparing: the timer count value TIM_CNT, the timer comparison value TIM_CCRx and the timer period value TIM_ARR three values ​​to obtain. The details are as follows:
Up counting mode: Delay time = TIM_CCRx - 0 Pulse width = TIM_ARR - TIM_CCRx
Down counting mode: Delay time = TIM_ARR - TIM_CCRx Pulse width = TIM_CCRx - 0
A simple single pulse mode example is shown below:
STM32 timer single pulse mode - ziye334 - ziye334's blog
When channel 2 of the timer (ie TI2 in the figure) detects a rising edge, after a delay of tDelay, the original low level of channel 1 becomes a high level, and then after tPULSE time, it is pulled down by several times. Here, the high level time is the pulse width.
Next, I will talk about how to implement the simple routine above: channel 2 of the timer detects a rising edge, and then outputs a pulse of a certain time after a certain delay. It is still 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 files, OnePulse.c and OnePulse.h, and save them in the src and inc folders of the BSP folder respectively. Then add OnePulse.c to the BSP project group.
 
2. Writing OnePulse.c and OnePulse.h files
First is the initialization of the pins. Here I select timer 4, channel 1 of timer 4 as the channel for outputting a single pulse, and channel 2 of the timer is set to input capture to capture the rising edge. Therefore, the pin PB6 corresponding to channel 1 of TIM4 is set to multiplexed push-pull output, and the pin PB7 corresponding to channel 2 of TIM4 is set to floating input. The code is as follows:

/****************************************************************
Function: OnePulse_GPIO_Init
Description: Timer pin initialization
Input: none
return: none
**********************************************************/
static void OnePulse_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //The pin corresponding to TIM4 CH1 is configured as multiplexed output
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //Configure the pin corresponding to TIM4 CH2 as floating input
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

Next is the configuration of the timer. The code is as follows:

/****************************************************************
Function: OnePulse_TIM4_Init
Description: Timer 4 configuration
Input: none
return: none
**********************************************************/
static void OnePulse_TIM4_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//Initialize TIM4 clock

/*---------------------------------------------------------
The timing base of TIM4 is 1/(72M/72)=1us.
CH1 of TIM4 is configured as PWM2 output mode, and CH2 of TIM4 is configured as input capture mode.
In PWM2 mode, when the count value is less than the comparison value, it is an invalid level, i.e., a low level.
After TIM2 CH2 detects a rising edge, it outputs a pulse after a certain delay time.
The delay time = 10000 * 1us = 10ms
and the pulse width = (65535 - 10000) * 1us = 65.535ms.
---------------------------------------------------------*/
TIM_TimeBaseStructure.TIM_Period = 65535;//Timer period valueTIM_TimeBaseStructure.TIM_Prescaler
= 72 - 1;//PrescalerTIM_TimeBaseStructure.TIM_ClockDivision
= 0;//Clock not dividedTIM_TimeBaseStructure.TIM_CounterMode
= TIM_CounterMode_Up;//Up counting modeTIM_TimeBaseInit
(TIM4, &TIM_TimeBaseStructure);//Initialize timer time baseTIM_OCInitStructure.TIM_OCMode

= TIM_OCMode_PWM2;//TIM4 CH1 is configured as PWM2 output modeTIM_OCInitStructure.TIM_OutputState
= TIM_OutputState_Enable;//Output enableTIM_OCInitStructure.TIM_Pulse
= 10000;//Set the jump valueTIM_OCInitStructure.TIM_OCPolarity
= TIM_OCPolarity_High;//The effective level is highTIM_OC1Init
(TIM4, &TIM_OCInitStructure);

TIM_ICStructInit(&TIM_ICInitStructure);//Initialize the input capture structureTIM_ICInitStructure.TIM_Channel
= TIM_Channel_2;//TIM4 CH2 is configured as input capture modeTIM_ICInitStructure.TIM_ICPolarity
= TIM_ICPolarity_Rising;//Rising edge captureTIM_ICInitStructure.TIM_ICSelection
= TIM_ICSelection_DirectTI;//Direct correspondence between management and registersTIM_ICInitStructure.TIM_ICPrescaler
= TIM_ICPSC_DIV1; //Not with frequency division
TIM_ICInitStructure.TIM_ICFilter = 0; //No filtering
TIM_ICInit(TIM4, &TIM_ICInitStructure); //Initialize TIM4 CH2

TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); //Select timer as single pulse mode
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //Select trigger source as IC2
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); //Select timer slave mode as rising edge trigger
}

First, set the timer count up, 72 pre-division, and then set the period value, here set to 65535, which is the TIM_ARR we mentioned above. Then set channel 1 of the timer. As mentioned above, channel 1 is used to output waveforms, so use PWM2 mode, set the effective level to high level, and then set its comparison value, which is our TIM_CCRx above, and channel 1 is TIM_CCR1. Now set channel 2. Channel 2 is used to check whether there is a rising edge. It is configured in input capture mode and will not have a rising edge. Then set the timer to single pulse mode, the trigger source is IC2 (that is, channel 2), and the rising edge is triggered. The timer is configured in this way. The extension time and pulse width of a single pulse can be calculated from the above configuration:
Extension time = 1 / (72M/72/10000) = 10ms
Pulse width = 1 / (72M/72/(65535 - 10000)) = 65.535ms
You also need to write a general function: OnePulse_Init() to initialize the relevant code:

/****************************************************************
Function: OnePulse_Init
Description: Single pulse mode initialization
Input: none
return: none
*************************************************************/
void OnePulse_Init(void)
{
OnePulse_GPIO_Init();
OnePulse_TIM4_Init();
}

Below is the code for OnePulse.h, which only declares the OnePulse_Init() function:

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

void OnePulse_Init(void);

#endif

3. Writing the main function
The main function is very simple, just calling the relevant initialization function, the code is as follows:

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

4. Testing
Connect the probe of the oscilloscope to the pin PB6 corresponding to TIM4 channel 1, and then use a DuPont line to connect the pin PB7 corresponding to TIM4 channel 2 to the GND ground pin, and then immediately unplug the DuPont line to simulate a rising edge. A pulse is detected on the oscilloscope, as shown in the following figure:
STM32 timer single pulse mode - ziye334 - ziye334's blog
By measuring, we can get a single pulse waveform on the oscilloscope, and the pulse length is about 65ms. It is similar to the following figure:
STM32 timer single pulse mode - ziye334 - ziye334's blog

Keywords:STM32 Reference address:STM32 timer single pulse mode

Previous article:STM32 six-step PWM output
Next article:STM32 timer output compare flip 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号