<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32之定時器應(yīng)用

          STM32之定時器應(yīng)用

          作者: 時間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
          定時控制的寄存器包括; 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; //設(shè)定計數(shù)器自動重裝值
          TIMx->PSC=TIM_ClockDivision; //預(yù)分頻器
          //TIMx->CR1 |=~(1<<4); //計數(shù)方向1上0下
          TIMx->DIER|=1<<0; //允許更新中斷
          TIMx->DIER|=1<<6; //允許觸發(fā)中斷
          TIMx->CR1 |=0x01; //使能定時器
          }
          用庫函數(shù):
          void TIM2_Init(void) //TIM2初始化函數(shù)
          {
          TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //定時器2外設(shè)時鐘使能
          TIM_DeInit(TIM2); //復(fù)位定時器2
          TIM_TimeBaseStructure.TIM_Period=TIM2_T; //定時器初始值
          TIM_TimeBaseStructure.TIM_Prescaler=71; //時鐘預(yù)分頻
          TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //時鐘分割
          TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上計數(shù)模
          TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化定時器的值
          TIM_ClearFlag(TIM2,TIM_FLAG_Update); //清除定時器中斷標(biāo)志
          TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能中斷
          TIM_Cmd(TIM2,ENABLE); //開啟時鐘
          TIM_SetAutoreload(TIM2, TIM2_T); //設(shè)置定時器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; //設(shè)定計數(shù)器自動重裝值
          TIMx->PSC=PSC; //預(yù)分頻器
          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庫函數(shù):
          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);
          }



          關(guān)鍵詞: stm32定時

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();