Getting Started with Timer

Publisher:丝语轻风Latest update time:2015-10-19 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "TIM_Init.h"

void TIM1_Configuration (uint Period,uint Pulse)
{
 TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
 TIM_OCInitTypeDef     TIM_OCInitStructure;
 TIM_BDTRInitTypeDef   TIM_BDTRInitStructure;

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);       //Enable the TIM1 port in the peripheral controlled by the APB2
 
 clockTIM_DeInit(TIM1);               //Reset all registers of timer
 
 1TIM_TimeBaseStructure.TIM_Prescaler = 0;          //Prescaler is 0, no frequency
 divisionTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //Counting mode is sequential counting mode, increasing type
 //TIM_TimeBaseStructure.TIM_Period = 5625-1;         //Set the initial value of the counter after overflowTIM_TimeBaseStructure.TIM_Period   
 = Period-1;         //Set the initial value of the counter after overflowTIM_TimeBaseStructure.TIM_ClockDivision
 = 0;       //Configure the clock division
 valueTIM_TimeBaseStructure.TIM_RepetitionCounter = 0;      //Loop count value
 
 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);      //Use the above parameters to initialize the timer time base module

 
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
 TIM_OCInitStructure.TIM_Pulse = Pulse;//127;
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
 
 TIM_OC1Init(TIM1, &TIM_OCInitStructure);
 
 
 TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
 TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
 TIM_BDTRInitStructure.TIM_DeadTime = 5;
 TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
 TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
 
 TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
 
 //清中断
 TIM_ClearFlag(TIM1, TIM_FLAG_Update);
 TIM_ClearFlag(TIM2, TIM_FLAG_Update);
 
 //TIM1中断源设置,开启相应通道的捕捉比较中断 
 
 
 
 TIM_Cmd(TIM1, ENABLE);  //使能定时器1
 
 
 
 TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能定时器1的PWM输出

}

void TIM2_Configuration (uint Period)
{
 TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);     //Initialize the timer time base module with the above parameters
  
 TIM_DeInit(TIM2);               //Reset all registers of timer 1
 
 TIM_TimeBaseStructure.TIM_Prescaler = 0;          //Prescaler is 0, no frequency division
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //Counting mode is sequential counting mode, increasing type
 //TIM_TimeBaseStructure.TIM_Period = 5625-1;         //Set the initial value of the counter after overflow   
 TIM_TimeBaseStructure.TIM_Period = Period-1;         //Set the initial value of the counter after overflow
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;       //Configure the clock separation value
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;      //Cycle count value
 
 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
 //Clear interrupt
 TIM_ClearFlag(TIM2, TIM_FLAG_Update);
 
 //Set TIM1 interrupt source, enable capture comparison interrupt of corresponding channel
  TIM_ITConfig(   //Enable or disable the specified TIM interrupt
  TIM2, //TIM2
  TIM_IT_Update   |   //TIM interrupt source
  TIM_IT_Trigger,    //TIM trigger interrupt source
  ENABLE   //Enable
  );

 TIM_Cmd(TIM2, ENABLE);   //Enable timer 1

}

void NVIC_Configuration(void)
  
 NVIC_InitTypeDef NVIC_InitStructure;

 #ifdef   VECT_TAB_RAM              //Interrupt vector table storage location selection
  
   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);       //The interrupt vector table is stored in RAM 0x00
 #else 
  
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);        //The interrupt vector table is stored in FLASH 0x00
 #endif

 //Interrupt NVIC settings: enable interrupts, set priority //Interrupt vector settings 
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);      //Set priority grouping: preempt priority 2, sub priority 2
 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;      //Update events
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 0;   //Preempt priority 0
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     //Response priority 1
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      //Enable interrupts
 NVIC_Init(&NVIC_InitStructure);          //Write settings
  
}

Keywords:Timer Reference address:Getting Started with Timer

Previous article:Summary of 22 common concepts of ARM
Next article:TIM Configuration

Recommended ReadingLatest update time:2024-11-16 22:02

STM8 MCU ADC, Timer, USART practical routines
Introduction: This is a routine for STM8L-051 that I spent a long time to figure out. It controls the LED light. Timer2 enters the interrupt after 100us. The software starts the ADC, samples 10 times and takes the average. The result is sent to the PC through UASART. The utility program displayed on the hyperterminal
[Microcontroller]
stm8s development (V) Use of TIMER: timing!
  STM8S provides three types of TIM timers: advanced control type (TIM1), general type (TIM2/TIM3/TIM5) and basic timer (TIM4/TIM6). Although they have different functions, they are all based on a common architecture. This common architecture makes it very easy and convenient to design applications using various timer
[Microcontroller]
stm8s development (V) Use of TIMER: timing!
51 MCU uses Timer0 to realize traffic lights at crossroads
Function implementation: When the east-west light is green, the north-south light is red. After a period of time, the east-west green light turns yellow and flashes three times, while the north-south light remains yellow. After flashing, the east-west light turns red, while the north-south light turns green. After a p
[Microcontroller]
51 MCU uses Timer0 to realize traffic lights at crossroads
S5PV210 hours
TCFG0, R/W, Address = 0xE250_0000 Timer Input Clock Frequency = PCLK / ( {prescaler value + 1} ) / {divider value} TCFG1, R/W, Address = 0xE250_0004 CON, R/W, Address = 0xE250_0008 TCNTB4, R/W, Address = 0xE250_003C Example #define TCFG0 (*(unsigned int*)0xE2500000) #define TCFG1 (*(unsigned int*)0xE2500004
[Microcontroller]
S5PV210 hours
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号