微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32之定时器应用

STM32之定时器应用

时间:11-11 来源:互联网 点击:
定时控制的寄存器包括; TIMX_CR1;TIMX_CR2;TIMX_SMCR;TIMX_DIERTIMX_SR;TIMX_EGR;TIMX_CCMR1;TIMX_CCMR2;TIMX_CCER;TIMX_CNT;TIMX_PSC;TIMX_ARR;TIMX_RCR;TIMX_CCR1;TIMX_CCR2;TIMX_CCR3;TIMX_CCR4;TIMX_BDTR;TIMX_DCR;TIMX_tDMAR;各个寄存器配置:

void TIMER_X_Init(TIM_TypeDef* TIMx,uint16_t TIM_ClockDivision,uint16_t TIM_Prescaler)

{

if(TIMx==TIM1)

RCC->APB2ENR =1<11;

if(TIMx==TIM2)

RCC->APB1ENR|=1<0;

if(TIMx==TIM3)

RCC->APB1ENR|=1<1; //时钟使能

if(TIMx==TIM4)

RCC->APB1ENR|=1<2;

TIMx->ARR=TIM_Prescaler; //设定计数器自动重装值

TIMx->PSC=TIM_ClockDivision; //预分频器

//TIMx->CR1 |=~(1<4); //计数方向1上0下

TIMx->DIER|=1<0; //允许更新中断

TIMx->DIER|=1<6; //允许触发中断

TIMx->CR1 |=0x01; //使能定时器

}

用库函数:

void TIM2_Init(void) //TIM2初始化函数

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //定时器2外设时钟使能

TIM_DeInit(TIM2); //复位定时器2

TIM_TimeBaseStructure.TIM_Period=TIM2_T; //定时器初始值

TIM_TimeBaseStructure.TIM_Prescaler=71; //时钟预分频

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1; //时钟分割

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模

TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化定时器的值

TIM_ClearFlag(TIM2,TIM_FLAG_Update); //清除定时器中断标志

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能中断

TIM_Cmd(TIM2,ENABLE); //开启时钟

TIM_SetAutoreload(TIM2, TIM2_T); //设置定时器2重装载值

TIM_ARRPreloadConfig(TIM2, ENABLE); //定时器2自动重装使能

}

pwm寄存器模式:

void TIMERX_PWM_Init(TIM_TypeDef* TIMx,uint16_t PSC,uint16_t ARR,uint8_t CH)

{ RCC->APB2ENR|=0X0001;

if(TIMx==TIM1) RCC->APB2ENR =1<11;

if(TIMx==TIM2) RCC->APB1ENR|=1<0;

if(TIMx==TIM3) RCC->APB1ENR|=1<1; //时钟使能

if(TIMx==TIM4) RCC->APB1ENR|=1<2; TIMx->ARR=ARR; //设定计数器自动重装值

TIMx->PSC=PSC; //预分频器

switch(CH) {

case 1:TIMx->CCR1=ARR/2;TIMx->CCMR1 |=7<4; TIMx->CCMR1 |=1<3;TIMx->CCER|=1<0; break;

case 2:TIMx->CCR2=ARR/2;TIMx->CCMR1 |=7<12; TIMx->CCMR1 |=1<11;TIMx->CCER|=1<4; break;

case 3:TIMx->CCR3=ARR/2;TIMx->CCMR2 |=7<4; TIMx->CCMR1 |=1<3;TIMx->CCER|=1<8; break;

case 4:TIMx->CCR4=ARR/2;TIMx->CCMR2 |=7<12; TIMx->CCMR1 |=1<11;TIMx->CCER|=1<12; break;

} TIMx->CR1=0x0080; //ARPE使能 TIMx->CR1 |=0x01; //使能定时器

}

pwm库函数:

void PWM_Init(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

/* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_AFIO, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */

TimerPeriod = (SystemCoreClock / PWM_Frequency ) - 1;

/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */

Channel1Pulse = (uint16_t) (((uint32_t) PWM_Duty * (TimerPeriod - 1)) / 1000);

/* Time Base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = TimerPeriod;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

/* Channel 1, 2,3 and 4 Configuration in PWM mode */

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 = Channel1Pulse;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

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);

/* TIM1 counter ena

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top