STM32: Basic timer details

Publisher:丝语轻风Latest update time:2018-12-28 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction to basic timers


In STM32, the basic timers include TIM6, TIM7, etc. The basic timer mainly includes a time base unit, which provides 16-bit counting and can count 0~65535. In addition to the counting function, the basic timer can also output a TRGO signal to the DAC module. The basic timer block diagram is as follows:



2. Time base unit introduction


All timers of STM32 have a time base unit. The function of the time base unit is to simply count, that is, to count the number of pulses of the clock source TMxCLK, which comes from the APB1 bus. Advanced and general timers can also use other clock sources for counting, which will be introduced in detail in the advanced timer and general timer. In the basic timer framework, it can be seen that the time base unit consists of the following three parts:

1.ARR automatic reload register

2. CNT counter

3.PSC prescaler


The timing (counting) function of the basic timer is configured as follows:



void TIM6_IRQHandler(void)

{

static int counter = 0;

if(TIM_GetITStatus(TIM6,TIM_IT_Update))

{

//After setting TIM_SelectOnePulseMode(TIM6,TIM_OPMode_Single); interrupt twice

TIM_ClearITPendingBit(TIM6,TIM_IT_Update);

}

}

 

//Basic timer

void TIM6_Configuration()

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

NVIC_InitTypeDef NVIC_InitStruct;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //Clock enable


TIM_TimeBaseInitStruct.TIM_Period = 10 -1;

TIM_TimeBaseInitStruct.TIM_Prescaler = 72;

TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;

TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStruct);// TIMx->EGR.UG   

 

NVIC_InitStruct.NVIC_IRQChannel = TIM6_IRQn;

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

NVIC_Init(&NVIC_InitStruct);


TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);

TIM_ClearITPendingBit(TIM6,TIM_IT_Update);

// TIM_SelectOnePulseMode(TIM6,TIM_OPMode_Single); //To configure single pulse mode, open this comment

TIM_ARRPreloadConfig(TIM6,ENABLE);

TIM_Cmd(TIM6,ENABLE);//CEN bit

TIM_ClearITPendingBit(TIM6,TIM_IT_Update);

}


It is worth noting that the basic timer also supports single pulse mode. You can configure the single pulse mode as shown in the code comments. In the single pulse mode, it is important to note that the timer is turned off only after the timer overflows twice, that is, the timer is disabled. In the code, interrupts are configured. In the single pulse mode, it can be clearly seen that the timer interrupt is entered twice.


3. Timer signal output


The signal output of the timer is related to the MMS bit of the control register 2 (TIM6->CR2) in the timer. The signal output by the basic timer can only be used as a trigger for the DAC, while the output signal of the advanced timer and general timer can trigger the timer and DAC. The specific details are not described here. For an example of the timer signal output, please refer to my blog http://blog.csdn.net/quentinecho/article/details/79068001. In this example, the TRGO signal output by TIM6 is used to start the DAC to generate a triangle wave. Of course, other DAC triggering methods can also generate a triangle wave.


#include "stm32f10x.h"

#include "stm32f10x_rcc.h"

#include "system_stm32f10x.h"

#include "stm32f10x_dac.h"

#include "stm32f10x_gpio.h"

#include "stm32f10x_tim.h"

 

/*DAC output = Vref x (DOR/4095)*/

 

//The two channels of DAC can be configured and used

//Same trigger source/different trigger source

//Simultaneous trigger/independent trigger DAC_DualSoftwareTriggerCmd function sets software simultaneous trigger

//Use waveform generator/Do not use waveform generator

//Use triangle wave generator/Use noise generator/Do not use waveform generator

//Set the same DAC_LFSRUnmask_TriangleAmplitude value/set different DAC_LFSRUnmask_TriangleAmplitude values

//The above situations can be combined arbitrarily without affecting each other.

void DAC_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

DAC_InitTypeDef DAC_InitStruct;

//First step: Enable the clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);

//Step 2 Configuration parameters

/*Once the DACx channel is enabled, the corresponding GPIO pin will automatically be connected to the analog output of the DAC. In order to avoid parasitic interference and additional power consumption, pins PA4/PA5 should be set to "analog input" before.

Note that it is "analog input", because there is no analog output in STM32, so although PA4 PA5 outputs analog signals, it can only be set to GPIO_Mode_AIN*/

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;

GPIO_InitStruct.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStruct);

GPIO_SetBits(GPIOA,GPIO_Pin_4 | GPIO_Pin_5); //PA.4 PA.5 input high, pull-up input plays an anti-interference role


// /*DAC channel 1 PA4 generates noise*/

// DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_Noise;

// DAC_InitStruct.DAC_Trigger = DAC_Trigger_T6_TRGO; //DAC_Trigger_T6_TRGO;

// DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //The output buffer can be used to reduce the output impedance and directly drive the external load without an external op amp

// DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits10_0; //Calculate the LSFR algorithm once each time it is triggered, add the obtained value to the value of DAC_DHRx, remove the overflow bit and write it to the DAC_DORx register to output a specific voltage

// DAC_Init(DAC_Channel_1,&DAC_InitStruct); //The number of bits involved in the LSFR algorithm is determined by DAC_LFSRUnmask_TriangleAmplitude. The value of DAC_LFSRUnmask_Bits10_0 indicates that 10 bits are involved in the LSFR calculation.


/*DAC channel 1 PA4 normal digital-to-analog conversion*/

DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_None; //Turn off the waveform generator

DAC_InitStruct.DAC_Trigger = DAC_Trigger_T6_TRGO;//DAC_Trigger_Software/DAC_Trigger_Ext_IT9

DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //The output buffer can be used to reduce the output impedance and directly drive the external load without an external op amp

DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; //This parameter is related to the noise/triangle wave generator. For normal DAC conversion, just set it to 0.

DAC_Init(DAC_Channel_1,&DAC_InitStruct);  

 

/*DAC channel 2 PA5 generates triangle wave*/

DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;

DAC_InitStruct.DAC_Trigger = DAC_Trigger_T6_TRGO;

DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable;

DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; //The internal triangle wave counter accumulates 1 each time it is triggered. The value of the counter is added to the value of DAC_DHRx, and the overflow bit is removed before writing to the DAC_DORx register. The output voltage

DAC_Init(DAC_Channel_2,&DAC_InitStruct); //The maximum value of the triangle wave counter is determined by DAC_LFSRUnmask_TriangleAmplitude. When the counter reaches this maximum value, the triangle wave counter starts to decrease.


//Step 3: Enable the device

//DAC_SetDualChannelData(DAC_Align_12b_R,4095,0); Equivalent to DAC_SetChannel1Data(DAC_Align_12b_R, 4095); DAC_SetChannel2Data(DAC_Align_12b_R, 0);  

/*DAC channel 1 PA4 enable*/

DAC_SetChannel1Data(DAC_Align_12b_R, 4095); //Set DAC value in 12-bit right-aligned data format. The maximum value is 4095. If it is set to 4096, it will overflow and DORx will be 0.

DAC_Cmd(DAC_Channel_1, ENABLE); //Enable DAC1


/*DAC channel 2 PA5 enable*/

DAC_Cmd(DAC_Channel_2, ENABLE); //Enable DAC1

DAC_SetChannel2Data(DAC_Align_12b_R, 0); //Set DAC value in 12-bit right-aligned data format

}

 

 

//Basic timer

void TIM6_Configuration()

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

//First step: Enable the clock

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //Clock enable

 

//The second step is to configure the parameters

TIM_TimeBaseInitStruct.TIM_Period = 10 -1;

TIM_TimeBaseInitStruct.TIM_Prescaler = 72;

TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;

TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStruct);// TIMx->EGR.UG   

 


/*TIM6,7 can output 3 types of TRGO signals

#define TIM_TRGOSource_Reset ((uint16_t)0x0000) //Reset UG

#define TIM_TRGOSource_Enable ((uint16_t)0x0010) // Enable CEN

#define TIM_TRGOSource_Update ((uint16_t)0x0020) //Update event

*/


TIM_SelectOutputTrigger(TIM6,TIM_TRGOSource_Update); //Output trigger TRGO signal Here TRGO signal is the update signal generated by timer overflow


//Step 3: Enable the device

TIM_Cmd(TIM6,ENABLE);//CEN bit

}

 

int main()

{

DAC_Configuration();

TIM6_Configuration();

while(1)

{

}

}


Keywords:stm32 Reference address:STM32: Basic timer details

Previous article:stm32 runtime measurement and interval execution
Next article:STM32 timer writes precise delay function

Recommended ReadingLatest update time:2024-11-16 18:05

STM32 library function development project template
After reading a lot of information and writing a lot of programs myself, I found that although they are all developed with library functions, the organization of files is different. For example, some people like to have OBJ, while others use LISTING. So which one is better? The organization of files is really a headac
[Microcontroller]
STM32_SPI read and write Flash
Today I will explain "STM32F103 SPI reading and writing Flash". In fact, this first stage mainly explains the SPI function of STM32. So today's focus is on SPI. I should talk about FLASH again later. The software project provided and explained today is modified based on the software project "A0.0.0 (STM32F10x_TIM dela
[Microcontroller]
STM32_SPI read and write Flash
Use of STM32 USART
SECTION 1 A strange problem was found during debugging of the STM32 serial port. After initializing serial port 1 and enabling the serial port send completion interrupt, the send completion interrupt was immediately entered. Carefully read the introduction of the serial port in the STM32 manual:            The follow
[Microcontroller]
34. Memory Management
1. Introduction to Memory Management 1. Why use memory management?  For example, how to browse SD card files on LCD If there is memory available for memory management, apply for memory and release it for other use after use. There is no need to define a large array in advance to occupy a lot of memory. 2. What is
[Microcontroller]
34. Memory Management
About STM32 general timer update event interrupt
 //Timer 3 interrupt service routine   void TIM3_IRQHandler(void)  {                    if(TIM3- SR&0X0001) //Generate update event   {    LED1=!LED1;    LED0=!LED0;                                }         TIM3- SR&=~(1 0);//Clear interrupt flag       }  //General timer interrupt initialization  //Here the clock sele
[Microcontroller]
STM32 learning record 17 serial port one-click download
1.MCUISP serial port software one-click download settings: DTR low level reset, RTS high level enter bootload serial port download The DTR and RTS output levels of the ch340 chip are opposite to the levels set by the computer software. The one-key download circuit completes the corresponding function according to th
[Microcontroller]
STM32 learning record 17 serial port one-click download
STM32 bit operation and marquee experiment
     The bit operation code is in the sys.h file, which implements the bit operation of each IO port of STM32, including reading and output. Of course, before calling these functions, the IO port clock must be enabled and the IO port function must be defined. 1. Principle of bit-band operation Expand a bit into a
[Microcontroller]
STM32 bit operation and marquee experiment
STM32 Learning Notes-TIM3 Overflow Timing
TIM3 is a general-purpose timer. The program uses the APB1 clock (PCLK1), 72MHz. In the program, TIM3 overflows, that is, overflows when 0-ARR. The frequency of TIM3 in the above program is (PCLK1*2)/(36000-1+1)=2KHz, where PCLK1 is 36MHz, so counting 2000 times is 1s. Steps to use timer timing: 1
[Microcontroller]
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号