這裡寫目錄标題
-
- 0. 聲明
- 1.定時器時鐘樹
- 2.定時器中結構體分析
-
- 2.1TIM時基初始化結構定義
- 2.2TIM輸出比較初始化結構定義
- 2.3TIM輸入捕獲初始化結構定義
- 2.4BDTR結構定義
- 3.定時器溢出中斷
- 4.定時器PWM輸出分析
- 5.定時器比較輸出分析
0. 聲明
需要先學習的基礎内容:
1.請在時鐘樹的基礎上進行定時器的學習,不然會知其然不知其是以然。
2.GPIO配置基礎
3.NVIC中斷優先級配置基礎
4.序列槽通信基礎(可有可無,主要用于檢視調試資訊)
5.可以先進行基本定時器和通用定時器的學習
1.定時器時鐘樹
2.定時器中結構體分析
2.1TIM時基初始化結構定義
/**
* @brief TIM Time Base Init structure definition
* @note This structure is used with all TIMx except for TIM6 and TIM7.
*/
typedef struct
{
uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_CounterMode; /*!< Specifies the counter mode.
This parameter can be a value of @ref TIM_Counter_Mode */
uint32_t TIM_Period; /*!< Specifies the period value to be loaded into the active
Auto-Reload Register at the next update event.
This parameter must be a number between 0x0000 and 0xFFFF. */
uint16_t TIM_ClockDivision; /*!< Specifies the clock division.
This parameter can be a value of @ref TIM_Clock_Division_CKD */
uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter
reaches zero, an update event is generated and counting restarts
from the RCR value (N).
This means in PWM mode that (N+1) corresponds to:
- the number of PWM periods in edge-aligned mode
- the number of half PWM period in center-aligned mode
This parameter must be a number between 0x00 and 0xFF.
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_TimeBaseInitTypeDef;
2.2TIM輸出比較初始化結構定義
/**
* @brief TIM Output Compare Init structure definition
*/
typedef struct
{
uint16_t TIM_OCMode; /*!< Specifies the TIM mode.
This parameter can be a value of @ref TIM_Output_Compare_and_PWM_modes */
uint16_t TIM_OutputState; /*!< Specifies the TIM Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_State */
uint16_t TIM_OutputNState; /*!< Specifies the TIM complementary Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_N_State
@note This parameter is valid only for TIM1 and TIM8. */
uint32_t TIM_Pulse; /*!< Specifies the pulse value to be loaded into the Capture Compare Register.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_OCPolarity; /*!< Specifies the output polarity.
This parameter can be a value of @ref TIM_Output_Compare_Polarity */
uint16_t TIM_OCNPolarity; /*!< Specifies the complementary output polarity.
This parameter can be a value of @ref TIM_Output_Compare_N_Polarity
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCNIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_OCInitTypeDef;
2.3TIM輸入捕獲初始化結構定義
/**
* @brief TIM Input Capture Init structure definition
*/
typedef struct
{
uint16_t TIM_Channel; /*!< Specifies the TIM channel.
This parameter can be a value of @ref TIM_Channel */
uint16_t TIM_ICPolarity; /*!< Specifies the active edge of the input signal.
This parameter can be a value of @ref TIM_Input_Capture_Polarity */
uint16_t TIM_ICSelection; /*!< Specifies the input.
This parameter can be a value of @ref TIM_Input_Capture_Selection */
uint16_t TIM_ICPrescaler; /*!< Specifies the Input Capture Prescaler.
This parameter can be a value of @ref TIM_Input_Capture_Prescaler */
uint16_t TIM_ICFilter; /*!< Specifies the input capture filter.
This parameter can be a number between 0x0 and 0xF */
} TIM_ICInitTypeDef;
2.4BDTR結構定義
(此結構僅與TIM1和TIM8一起使用。)
/**
* @brief BDTR structure definition
* @note This structure is used only with TIM1 and TIM8.
*/
typedef struct
{
uint16_t TIM_OSSRState; /*!< Specifies the Off-State selection used in Run mode.
This parameter can be a value of @ref TIM_OSSR_Off_State_Selection_for_Run_mode_state */
uint16_t TIM_OSSIState; /*!< Specifies the Off-State used in Idle state.
This parameter can be a value of @ref TIM_OSSI_Off_State_Selection_for_Idle_mode_state */
uint16_t TIM_LOCKLevel; /*!< Specifies the LOCK level parameters.
This parameter can be a value of @ref TIM_Lock_level */
uint16_t TIM_DeadTime; /*!< Specifies the delay time between the switching-off and the
switching-on of the outputs.
This parameter can be a number between 0x00 and 0xFF */
uint16_t TIM_Break; /*!< Specifies whether the TIM Break input is enabled or not.
This parameter can be a value of @ref TIM_Break_Input_enable_disable */
uint16_t TIM_BreakPolarity; /*!< Specifies the TIM Break Input pin polarity.
This parameter can be a value of @ref TIM_Break_Polarity */
uint16_t TIM_AutomaticOutput; /*!< Specifies whether the TIM Automatic Output feature is enabled or not.
This parameter can be a value of @ref TIM_AOE_Bit_Set_Reset */
} TIM_BDTRInitTypeDef;
3.定時器溢出中斷
/*函數名:void Timer1_Init(u16 arr,u16 psc)
函數功能: TIM1定時器溢出中斷初始化
傳入參數:
arr:計數值
psc:預分頻數值
*/
void Timer1_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrecture; //定時器時基初始化結構體
NVIC_InitTypeDef NVIC_InitStructure; //中斷優先級配置結構體
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); /*使能定時器1的時鐘*/
TIM_TimeBaseInitStrecture.TIM_Period = arr; /*重裝載寄存器數值*/
TIM_TimeBaseInitStrecture.TIM_Prescaler = psc-1; /*預配置設定數值*/
TIM_TimeBaseInitStrecture.TIM_ClockDivision = TIM_CKD_DIV1; /*時鐘分頻*/
TIM_TimeBaseInitStrecture.TIM_CounterMode = TIM_CounterMode_Up; /*向上計數*/
TIM_TimeBaseInitStrecture.TIM_RepetitionCounter = 0; /*重複計數寄存器*/
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStrecture); /*初始化*/
TIM_ClearFlag(TIM1,TIM_FLAG_Update); /*清更新标志位*/
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); /*使能中斷*/
TIM_Cmd(TIM1,ENABLE); /*使能計數*/
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;/*定時器1的中斷通道使能*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/*定時器1的中斷通道使能*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;/*搶占優先級*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;/*響應優先級*/
NVIC_Init(&NVIC_InitStructure);/*配置中斷分組,并使能中斷*/
}
//函數名: void TIM1_UP_TIM10_IRQHandler(void)
//函數功能: TIM1定時器溢出中斷,中斷服務函數。
void TIM1_UP_TIM10_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)//檢查指定的TIM中斷發生與否:TIM 中斷源
{
LED1=~LED1;
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清除TIMx的中斷待處理位:TIM 中斷源
}
}
4.定時器PWM輸出分析
5.定時器比較輸出分析
目前遇到一個小的BUG ,就是在自動重裝載值,比較小的時候,反轉輸出會出現各種問題喲!!!
目前先了解到這裡,先不向後面繼續了解了!!!
// 第一步: 進行引腳的配置--> 把TIM1進階定時器的通道1, 複用到GPIOE_9上面。
void STEPMOTOR_TIM1_Init_GPIO()
{
GPIO_InitTypeDef GPIO_InitStructure; //基本定時器結構體
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); //引腳時鐘結構體初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //引腳 PE9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //複用到TIM1 通道1上面。
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //翻轉速度100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化GPIO
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1); //複用配置
}
//第二步 :進行定時器的基本時基部分的配置 比較輸出使能的配置 還有關于中斷的優先級配置
void STEPMOTOR_TIM1_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定時器基本結構體
TIM_OCInitTypeDef TIM_OCInitStructure; //定時器比較輸出結構體
NVIC_InitTypeDef NVIC_InitStructure; //中斷配置結構體
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //開定時器時鐘(高速時鐘)
STEPMOTOR_TIM1_Init_GPIO(); //調用引腳函數。
TIM_TimeBaseStructure.TIM_Period = 0XFFFF; //設定在下一個更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =168-1; //設定用來作為TIMx時鐘頻率除數的預分頻值 不分頻 168M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的參數初始化TIMx的時間基數機關
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; // 比較輸出模式:反轉輸出
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable; //互補比較使能
TIM_OCInitStructure.TIM_Pulse=1000; //指定要加載到捕獲/比較寄存器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //輸出極性為高
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set; //指定空閑狀态下的TIM輸出比較pin狀态
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根據TIM_OCInitStruct中指定的參數初始化外設TIMx
TIM_OC1FastConfig(TIM1,TIM_OCFast_Enable);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Disable); //禁止TIM1在CCR上的預裝載寄存器,這裡很重要不要使能啊
TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Enable);
TIM_ITConfig(TIM1, TIM_IT_CC1,ENABLE); //中斷使能
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
int tim_count = 0;
void TIM1_CC_IRQHandler(void)
{
if( TIM_GetITStatus( TIM1 , TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
LED1=~LED1;
tim_count=TIM_GetCapture1(TIM1); //得到通道1的計數數值。
TIM_SetCompare1(TIM1,tim_count+1000); //設定新的CCR 值,新的比較數值。
}
}