色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 基于STM32處理器 定時器比較輸出測試程序

          基于STM32處理器 定時器比較輸出測試程序

          作者: 時間:2016-11-22 來源:網(wǎng)絡 收藏
          這里給出最關(guān)鍵的三個文件
          main.c文件
          #include "stm32f10x.h"
          #include "stm32f10x_tim.h"
          #include "misc.h"
          unsigned int CCR3_Val=5000;
          unsigned int CCR4_Val=10000;
          extern void time_ini(void);
          void RCC_Configuration(void)
          {
          SystemInit();
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
          | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
          }
          void NVIC_Configuration(void)
          {
          NVIC_InitTypeDef NVIC_InitStructure;
          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
          NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQn ; //配置定時器中斷
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
          NVIC_Init(&NVIC_InitStructure);
          }
          int main(void)
          {
          RCC_Configuration(); //系統(tǒng)時鐘設置及外設時鐘使能
          NVIC_Configuration(); //中斷源配置
          time_ini(); //定時器3的初始化
          while(1);
          }
          Tim.c文件
          #include "stm32f10x_tim.h"
          #include "stm32f10x_gpio.h"
          #include
          void time_ini(void);
          extern unsigned int CCR3_Val;
          extern unsigned int CCR4_Val;
          TIM_TimeBaseInitTypeDef TIM3_TimeBaseStructure;
          TIM_OCInitTypeDef TIM3_OCInitStructure;
          TIM_BDTRInitTypeDef TIM3_BDTRInitStructure;
          void time_ini(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //定時器3 時鐘使能
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //PB5復用為TIM3的通道2
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_Init(GPIOB, &GPIO_InitStructure);
          GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE);
          TIM3_TimeBaseStructure.TIM_Prescaler = 7200; //預分頻器TIM3_PSC=63
          TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //計數(shù)器向上計數(shù)模式 TIM3_CR1[4]=0
          TIM3_TimeBaseStructure.TIM_Period =0xffff; //自動重裝載寄存器TIM3_APR
          TIM3_TimeBaseStructure.TIM_ClockDivision = 0x0; //時鐘分頻因子 TIM3_CR1[9:8]=00
          TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure); //寫TIM3各寄存器參數(shù)
          TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ; //TIM3_CCMR1[14:12]=011 翻轉(zhuǎn)當TIM3_CCR2=TIM3_CNT時,翻轉(zhuǎn)OC2REF的電平
          TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //輸出極性 低電平有效 TIM3_CCER[5]=1;
          TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸入/捕獲2輸出允許 OC2信號輸出到對應的輸出引腳PB5
          TIM3_OCInitStructure.TIM_Pulse =CCR3_Val; //若CC1通道配置為輸出:CCR2是裝入當前捕獲/比較2 TIM3_CCR2寄存器的值(預裝載值)。
          TIM_OC3Init(TIM3, &TIM3_OCInitStructure);
          TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
          TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸入/捕獲2輸出允許 OC2信號輸出到對應的輸出引腳PB5
          TIM3_OCInitStructure.TIM_Pulse =CCR4_Val; //若CC1通道配置為輸出:CCR2是裝入當前捕獲/比較2 TIM3_CCR2寄存器的值(預裝載值)。
          TIM_OC4Init(TIM3, &TIM3_OCInitStructure);
          TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable); //TIM3_CCMR1[1]=0 禁止TIM3_CCR2寄存器的預裝載功能,可隨時寫入TIM3_CCR2
          TIM_Cmd(TIM3,ENABLE); //啟動定時器3 TIM3_CR1[0]=1;
          TIM_ITConfig(TIM3,TIM_IT_CC3|TIM_IT_CC4,ENABLE); //TIM3_DIER[2]=1 允許捕獲/比較2中斷
          }
          stm32f10x_it.c文件
          #include "stm32f10x.h"
          #include "stm32f10x_tim.h"
          unsigned int capture1=0;
          extern unsigned int CCR3_Val;
          extern unsigned int CCR4_Val;
          extern TIM_OCInitTypeDef TIM3_OCInitStructure;
          void TIM3_IRQHandler(void)
          {
          if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET) //判斷狀態(tài)寄存器 TIM3_SR[2] 是否發(fā)生了捕獲/比較2 中斷
          {
          TIM_ClearITPendingBit(TIM3, TIM_IT_CC3); //軟件清除狀態(tài)寄存器 TIM3_SR[2] 捕獲/比較2 中斷中斷標志
          capture1 = TIM_GetCapture3(TIM3); //獲取TIM3_CCR2的預裝值
          TIM_SetCompare3(TIM3, capture1+ CCR3_Val); //和CCR2_Val累加后裝入TIM3_CCR2 這是為了配合計數(shù)器值的遞增。以在下一次作出比較。
          }
          if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET) //判斷狀態(tài)寄存器 TIM3_SR[2] 是否發(fā)生了捕獲/比較2 中斷
          {
          TIM_ClearITPendingBit(TIM3, TIM_IT_CC4); //軟件清除狀態(tài)寄存器 TIM3_SR[2] 捕獲/比較2 中斷中斷標志
          capture1 = TIM_GetCapture4(TIM3); //獲取TIM3_CCR2的預裝值
          TIM_SetCompare4(TIM3, capture1+ CCR4_Val); //和CCR2_Val累加后裝入TIM3_CCR2 這是為了配合計數(shù)器值的遞增。以在下一次作出比較。
          }
          }



          評論


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

          關(guān)閉