//------------------------------------------------------------------------------ // 一、普通定時使用 #include"stm32f10x.h" #include"time.h" static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } static void Time_Config( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE ); //TIM_DeInit(TIM2); TIM_TimeBaseInitStructure.TIM_Prescaler = ( 10000 - 1 ); //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period = ( 7200 - 1 ); //自動重裝寄存器 TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分割,這裏不涉及 #if 0 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure ); //配置參數 TIM_ClearITPendingBit( TIMx, TIM_IT_Update ); //清除中斷標示位 TIM_ITConfig( TIMx, TIM_IT_Update, ENABLE ); //中斷配置 TIM_Cmd( TIMx, ENABLE ); //啓動定時器 } void Time_Init( void ) { Time_Config( ); //定時器配置 Time_NVIC_Config( ); //嵌套中斷向量初始化 } //------------------------------------------------------------------------------ // 二、PWM輸出 #include"stm32f10x.h" #include"time.h" #if 1 static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } #endif void Time_OUT_PWM_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE ); GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推免式輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure ); } static void Time_OUT_PWM_Config( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE ); //開啓時鐘 TIM_DeInit( TIM2 ); TIM_TimeBaseInitStructure.TIM_Prescaler = ( 100 - 1 ); //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period = ( 1000 - 1 ); //自動重裝寄存器 PWM頻率:72M/100/1000=720Hz TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分頻,這裏不涉及 #ifdef Time1 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure ); //配置參數 TIM_ClearITPendingBit( TIMx, TIM_IT_Update ); //清除中斷標示位 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式 OCx電平根據ARR與CCRx比較結果發生反轉 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道1 #ifdef Time1 TIM_OCInitStructure.TIM_OutputNState =; #endif TIM_OCInitStructure.TIM_Pulse = CCR1_VAL; //佔空比800/1000 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //計數器值小於CCR值時輸出高電平 #ifdef Time1 TIM_OCInitStructure.TIM_OCNPolarity =; TIM_OCInitStructure.TIM_OCIdleState =; TIM_OCInitStructure.TIM_OCNIdleState =; #endif TIM_OC1Init( TIMx, &TIM_OCInitStructure ); TIM_OC1PreloadConfig( TIMx, TIM_OCPreload_Enable ); //使能CCR1預裝載 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道2 TIM_OCInitStructure.TIM_Pulse = CCR2_VAL; TIM_OC2Init( TIMx, &TIM_OCInitStructure ); TIM_OC2PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道3 TIM_OCInitStructure.TIM_Pulse = CCR3_VAL; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC3Init( TIMx, &TIM_OCInitStructure ); TIM_OC3PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道4 TIM_OCInitStructure.TIM_Pulse = CCR4_VAL; TIM_OC4Init( TIMx, &TIM_OCInitStructure ); TIM_OC4PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_ARRPreloadConfig( TIMx, ENABLE ); //開啓ARR預裝載 TIM_Cmd( TIMx, ENABLE ); //啓動定時器 } void Time_OUT_PWM_Init( void ) { Time_OUT_PWM_GPIO_Config( ); Time_OUT_PWM_Config( ); //定時器配置 } //------------------------------------------------------------------------------ // 三、輸出比較模式 #include"stm32f10x.h" #include"time.h" static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } static void Time_OUT_PWM_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE ); GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推免式輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure ); } static void Time_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推免式輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOD, &GPIO_InitStructure ); } static void Time_OUT_PWM_Config( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE ); //開啓時鐘 TIM_DeInit( TIM2 ); TIM_TimeBaseInitStructure.TIM_Prescaler = ( 100 - 1 ); //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period = ( 1000 - 1 ); //自動重裝寄存器 PWM頻率:72M/100/1000=720Hz TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分頻,這裏不涉及 #ifdef Time1 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure ); //配置參數 TIM_ClearITPendingBit( TIMx, TIM_IT_Update ); //清除中斷標示位 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; //ARR與CCRx相同時強制OCxREF爲低 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道1 #ifdef Time1 TIM_OCInitStructure.TIM_OutputNState =; #endif TIM_OCInitStructure.TIM_Pulse = CCR1_VAL; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OCx=!OCxREF _High OCx=OCxREF #ifdef Time1 TIM_OCInitStructure.TIM_OCNPolarity =; TIM_OCInitStructure.TIM_OCIdleState =; TIM_OCInitStructure.TIM_OCNIdleState =; #endif TIM_OC1Init( TIMx, &TIM_OCInitStructure ); TIM_OC1PreloadConfig( TIMx, TIM_OCPreload_Enable ); //使能CCR1預裝載 TIM_ITConfig( TIMx, TIM_IT_CC1, ENABLE ); //使能OC1比較中斷 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active; //ARR與CCRx相同時強制OCxREF爲高 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道2 TIM_OCInitStructure.TIM_Pulse = CCR2_VAL; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //OCx=!OCxREF _High OCx=OCxREF TIM_OC2Init( TIMx, &TIM_OCInitStructure ); TIM_OC2PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_ITConfig( TIMx, TIM_IT_CC2, ENABLE ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //ARR與CCRx比較結果對OCxREF無效 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道3 TIM_OCInitStructure.TIM_Pulse = CCR3_VAL; TIM_ITConfig( TIMx, TIM_IT_CC3, ENABLE ); TIM_OC3Init( TIMx, &TIM_OCInitStructure ); TIM_OC3PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ARR與CCRx比較結果對OCxREF採用PWM1模式 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出使能通道4 TIM_OCInitStructure.TIM_Pulse = CCR4_VAL; //TIM_ITConfig(TIMx,TIM_IT_CC4,ENABLE); //PWM就沒有必要設置中斷 固然也能夠在中斷裏面改變佔空比 TIM_OC4Init( TIMx, &TIM_OCInitStructure ); TIM_OC4PreloadConfig( TIMx, TIM_OCPreload_Enable ); TIM_ARRPreloadConfig( TIMx, ENABLE ); //開啓ARR預裝載 TIM_Cmd( TIMx, ENABLE ); //啓動定時器 } void Time_OUT_PWM_Init( void ) { Time_OUT_PWM_GPIO_Config( ); Time_GPIO_Config( ); Time_NVIC_Config( ); Time_OUT_PWM_Config( ); //定時器配置 } //------------------------------------------------------------------------------ // 四、PWMI模式 #include"stm32f10x.h" #include"time.h" static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } static void Time_IN_PWM_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( TIMx_IN_GPIO_RCC, ENABLE ); GPIO_InitStructure.GPIO_Pin = TIMx_IN_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( TIMx_IN_Port, &GPIO_InitStructure ); } static void Time_IN_PWM_Config( void ) { #if 0 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; #endif TIM_ICInitTypeDef TIM_ICInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE ); //開啓時鐘 TIM_DeInit( TIM2 ); #if 0 TIM_TimeBaseInitStructure.TIM_Prescaler =(100-1); //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period =(1000-1);//自動重裝寄存器 PWM頻率:72M/100/1000=720Hz TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;//向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision =0;//時鐘分頻,這裏不涉及 #ifdef Time1 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置參數 TIM_ClearITPendingBit(TIMx,TIM_IT_Update);//清除中斷標示位 #endif TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿有效 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //TIM Input 1, 2, 3 or 4 is selected to be // connected to IC1, IC2, IC3 or IC4, respectively */ TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //無預分頻 TIM_ICInitStructure.TIM_ICFilter = 0x0; //無濾波 TIM_ICInit( TIMx, &TIM_ICInitStructure ); //初始化PWM輸入模式 //參見函數體與參考手冊關於PWMI說明 TIM_PWMIConfig( TIMx, &TIM_ICInitStructure ); TIM_SelectInputTrigger( TIMx, TIM_TS_TI2FP2 ); //選擇TI2FP2做爲TIMx輸入觸發 一個信號來以後 啓動定時器開始計數 TIM_SelectSlaveMode( TIMx, TIM_SlaveMode_Reset ); //選擇從模式控制器爲復位模式,選中的TRGI上升沿從新初始化計數器 //從模式控制器鏈接到TI1FP1 TI2FP2 只要二者有效爲設置的電平,就會復位計數器 參見TIME結構圖 TIM_SelectMasterSlaveMode( TIMx, TIM_MasterSlaveMode_Enable ); //使能主從模式 ?????????????????? TIM_Cmd( TIMx, ENABLE ); TIM_ITConfig( TIMx, TIM_IT_CC2, ENABLE ); } void Time_IN_PWM_Init( void ) { Time_IN_PWM_GPIO_Config( ); Time_NVIC_Config( ); Time_IN_PWM_Config( ); //定時器配置 } //------------------------------------------------------------------------------ // 五、單脈衝模式 #include"stm32f10x.h" #include"time.h" static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } static Time_SinglePluse_IN_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( TIMx_IN_GPIO_RCC, ENABLE ); GPIO_InitStructure.GPIO_Pin = TIMx_IN_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( TIMx_IN_Port, &GPIO_InitStructure ); } static Time_SinglePluse_OUT_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE ); GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推免複用輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure ); } static void Time_SinglePluse_Config( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE ); //開啓時鐘 TIM_DeInit( TIM2 ); TIM_TimeBaseInitStructure.TIM_Prescaler = 1; //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period = 65535; //自動重裝寄存器 TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分頻,這裏不涉及 #ifdef Time1 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure ); //配置參數 TIM_ClearITPendingBit( TIMx, TIM_IT_Update ); //清除中斷標示位 TIM_ICStructInit( &TIM_ICInitStructure ); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; //通道2爲輸入 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿有效 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //TIM Input 1, 2, 3 or 4 is selected to be // connected to IC1, IC2, IC3 or IC4, respectively*/ TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //無預分頻 TIM_ICInitStructure.TIM_ICFilter = 0x0; //無濾波 TIM_ICInit( TIMx, &TIM_ICInitStructure ); //初始化輸入模式 TIM_SelectOnePulseMode( TIMx, TIM_OPMode_Single ); //選擇單脈衝模式 這樣 下一次更新時間是中止計數器 TIM_SelectInputTrigger( TIMx, TIM_TS_TI2FP2 ); //選擇TI2FP2做爲TIMx輸入觸發 一個信號來以後 啓動定時器開始計數 TIM_SelectSlaveMode( TIMx, TIM_SlaveMode_Trigger ); //選擇從模式控制器爲觸發模式 其鏈接到了TI2FP2 讓從模式控制器啓動計數器 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //輸出模式選擇爲PWM模式2 用PWM2 向上計數時,CNT TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能通道 #ifdef Time1 TIM_OCInitStructure.TIM_OutputNState =; #endif TIM_OCInitStructure.TIM_Pulse = 12345; //CCRx裏面存放的值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OCREF與OC實際輸出相同 #ifdef Time1 TIM_OCInitStructure.TIM_OCNPolarity =; TIM_OCInitStructure.TIM_OCIdleState =; TIM_OCInitStructure.TIM_OCNIdleState =; #endif TIM_OC1Init( TIMx, &TIM_OCInitStructure ); //使用通道1做爲單脈衝的輸出通道 //TIM_Cmd(TIMx, ENABLE); //使用TI2FP1來觸發定時器,不須要軟件啓動定時器 } void Time_SinglePluse_Init( void ) { Time_SinglePluse_IN_GPIO_Config( ); //配置time2的通道2爲輸入 Time_SinglePluse_OUT_GPIO_Config( ); //配置time2的通道1爲輸出 Time_NVIC_Config( ); //能夠不用 Time_SinglePluse_Config( ); //定時器配置 } //------------------------------------------------------------------------------ // 六、定時器聯級 #include"stm32f10x.h" #include"time.h" static Time_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 ); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); } static Time_Connect_GPIO_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_6; //time2 ch1 pin.0 time3 cha1 pin.6 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推免複用輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //time4 ch1 pin.6 GPIO_Init( GPIOB, &GPIO_InitStructure ); } static void Time_Connect_Config( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE ); TIM_DeInit( TIM2 ); TIM_DeInit( TIM3 ); TIM_DeInit( TIM4 ); TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //時鐘分頻係數 TIM_TimeBaseInitStructure.TIM_Period = 99; //自動重裝寄存器 TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式 TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分頻,這裏不涉及 #ifdef Time1 //或者Time8 TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8 #endif TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure ); //配置參數 TIM_ClearITPendingBit( TIM2, TIM_IT_Update ); //清除中斷標示位 TIM_TimeBaseInitStructure.TIM_Period = 5; TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStructure ); TIM_ClearITPendingBit( TIM3, TIM_IT_Update ); TIM_TimeBaseInitStructure.TIM_Period = 5; TIM_TimeBaseInit( TIM4, &TIM_TimeBaseInitStructure ); TIM_ClearITPendingBit( TIM4, TIM_IT_Update ); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //輸出模式選擇爲PWM模式2 用PWM2 向上計數時,CNT TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能通道 TIM_OCInitStructure.TIM_Pulse = 49; //CCRx裏面存放的值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OCREF與OC實際輸出相同 TIM_OC1Init( TIM2, &TIM_OCInitStructure ); //使用通道1做爲單脈衝的輸出通道 TIM_SelectMasterSlaveMode( TIM2, TIM_MasterSlaveMode_Enable ); //使能主從模式 TIM_SelectOutputTrigger( TIM2, TIM_TRGOSource_Update ); //選擇Time2的更新事件做爲觸發輸出 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = 3; TIM_OC1Init( TIM3, &TIM_OCInitStructure ); TIM_OC1Init( TIM4, &TIM_OCInitStructure ); TIM_SelectSlaveMode( TIM3, TIM_SlaveMode_Gated ); //從模式的輸入觸發選擇爲門控模式 TIM_SelectInputTrigger( TIM3, TIM_TS_ITR1 ); //從模式觸發源選擇爲內部觸發1?????????????爲何是內部觸發1 不是0 2 3 //緣由:ITR0 ITR1 ITR2 ITR3 對應主模式的TIM2 TIM3 TIM4 TIM5 TIM_SelectSlaveMode( TIM4, TIM_SlaveMode_Gated ); TIM_SelectInputTrigger( TIM4, TIM_TS_ITR1 ); TIM_Cmd( TIM2, ENABLE ); TIM_Cmd( TIM3, ENABLE ); TIM_Cmd( TIM4, ENABLE ); } void Time_Connect_Init( void ) { Time_Connect_GPIO_Config( ); Time_NVIC_Config( ); //能夠不用 Time_Connect_Config( ); //定時器配置 }