您的位置 首页 动态

STM32之定时器使用

定时控制的寄存器包括;TIMX_CR1;TIMX_CR2;TIMX_SMCR;TIMX_DIERTIMX_SR;TIMX_EGR;TIMX_CCMR1;TIMX_CCMR2;TIMX_CCER;TIM

守时操控的寄存器包含; 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 enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/274337.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部