结构体名 结构体成员 结构体成员对应的寄存器或者位 TIM_TypeDef #definePERIPH_BASE((uint32_t)0x40000000)#defineAPB1PERIPH_BASEPERIPH_BASE#defineTIM2_BASE(APB1PERIPH_BASE+0x0000) #defineTIM3_BASE(APB1PERIPH_BASE+0x0400) #defineTIM4_BASE(APB1PERIPH_BASE+0x0800) #defineTIM5_BASE(APB1PERIPH_BASE+0x0C00) #defineTIM6_BASE(APB1PERIPH_BASE+0x1000) #defineTIM7_BASE(APB1PERIPH_BASE+0x1400) #defineTIM12_BASE(APB1PERIPH_BASE+0x1800) #defineTIM13_BASE(APB1PERIPH_BASE+0x1C00) #defineTIM14_BASE(APB1PERIPH_BASE+0x2000)#defineTIM1_BASE(APB2PERIPH_BASE+0x2C00)#defineTIM8_BASE(APB2PERIPH_BASE+0x3400)#defineTIM16_BASE(APB2PERIPH_BASE+0x4400) #defineTIM17_BASE(APB2PERIPH_BASE+0x4800) #defineTIM9_BASE(APB2PERIPH_BASE+0x4C00) #defineTIM10_BASE(APB2PERIPH_BASE+0x5000) #defineTIM11_BASE(APB2PERIPH_BASE+0x5400)#defineTIM2((TIM_TypeDef)TIM2_BASE) #defineTIM3((TIM_TypeDef)TIM3_BASE) #defineTIM4((TIM_TypeDef)TIM4_BASE) #defineTIM5((TIM_TypeDef)TIM5_BASE) #defineTIM6((TIM_TypeDef)TIM6_BASE) #defineTIM7((TIM_TypeDef)TIM7_BASE) #defineTIM12((TIM_TypeDef)TIM12_BASE) #defineTIM13((TIM_TypeDef)TIM13_BASE) #defineTIM14((TIM_TypeDef)TIM14_BASE)#defineTIM8((TIM_TypeDef)TIM8_BASE)#defineTIM1((TIM_TypeDef)TIM1_BASE)#defineTIM15((TIM_TypeDef)TIM15_BASE) #defineTIM16((TIM_TypeDef)TIM16_BASE) #defineTIM17((TIM_TypeDef)TIM17_BASE) #defineTIM9((TIM_TypeDef)TIM9_BASE) #defineTIM10((TIM_TypeDef)TIM10_BASE) #defineTIM11((TIM_TypeDef)TIM11_BASE) __IOuint16_tCR1; uint16_tRESERVED0; __IOuint16_tCR2; uint16_tRESERVED1; __IOuint16_tSMCR; uint16_tRESERVED2; __IOuint16_tDIER; uint16_tRESERVED3; __IOuint16_tSR; uint16_tRESERVED4; __IOuint16_tEGR; uint16_tRESERVED5; __IOuint16_tCCMR1; uint16_tRESERVED6; __IOuint16_tCCMR2; uint16_tRESERVED7; __IOuint16_tCCER; uint16_tRESERVED8; __IOuint16_tCNT; uint16_tRESERVED9; __IOuint16_tPSC; uint16_tRESERVED10; __IOuint16_tARR; uint16_tRESERVED11; __IOuint16_tRCR; uint16_tRESERVED12; __IOuint16_tCCR1; uint16_tRESERVED13; __IOuint16_tCCR2; uint16_tRESERVED14; __IOuint16_tCCR3; uint16_tRESERVED15; __IOuint16_tCCR4; uint16_tRESERVED16; __IOuint16_tBDTR; uint16_tRESERVED17; __IOuint16_tDCR; uint16_tRESERVED18; __IOuint16_tDMAR; uint16_tRESERVED19; uint16_tTIM_Prescaler; uint16_tTIM_CounterMode; uint16_tTIM_Period; uint16_tTIM_ClockDivision; uint8_tTIM_RepetitionCounter; TIM_TimeBaseInitTypeDef TIMx_Psc TIMx_CR1[6:4]=CMS[1:0]+DIR TIMx_ARR TIMx_CR1[9:8]=CKD[1:0] TIMx_RCR SpecifiestheprescalervalueusedtodividetheTIMclock.Thisparametercanbeanumberbetween0x0000and0xFFFF SpecifiestheperiodvaluetobeloadedintotheactiveAuto-ReloadRegisteratthenextupdateevent.Thisparametermustbeanumberbetween0x0000and0xFFFF. 相关宏定义及参数取值 Specifiestherepetitioncountervalue.EachtimetheRCRdowncounterreacheszero,anupdateeventisgeneratedandcountingrestartsfromtheRCRvalue(N).ThismeansinPWMmodethat(N+1)correspondsto:-thenumberofPWMperiodsinedge-alignedmode-thenumberofhalfPWMperiodincenter-alignedmodeThisparametermustbeanumberbetween0x00and0xFF.@noteThisparameterisvalidonlyforTIM1andTIM8. uint16_tTIM_OCMode uint16_tTIM_OutputState uint16_tTIM_OutputNState uint16_tTIM_Pulse uint16_tTIM_OCPolarity uint16_tTIM_OCNPolarity uint16_tTIM_OCIdleState uint16_tTIM_OCNIdleState TIM_OCInitTypeDef SpecifiestheTIMmode.Thisparametercanbeavalue:#defineTIM_OCMode_Timing((uint16_t)0x0000) #defineTIM_OCMode_Active((uint16_t)0x0010) #defineTIM_OCMode_Inactive((uint16_t)0x0020) #defineTIM_OCMode_Toggle((uint16_t)0x0030) #defineTIM_OCMode_PWM1((uint16_t)0x0060) #defineTIM_OCMode_PWM2((uint16_t)0x0070) Specifiestheclockdivision.Thisparametercanbeavalue:#defineTIM_CKD_DIV1((uint16_t)0x0000) #defineTIM_CKD_DIV2((uint16_t)0x0100) #defineTIM_CKD_DIV4((uint16_t)0x0200) Specifiesthecountermode.Thisparametercanbeavalue:#defineTIM_CounterMode_Up((uint16_t)0x0000) #defineTIM_CounterMode_Down((uint16_t)0x0010) #defineTIM_CounterMode_CenterAligned1((uint16_t)0x0020) #defineTIM_CounterMode_CenterAligned2((uint16_t)0x0040) #defineTIM_CounterMode_CenterAligned3((uint16_t)0x0060)
#defineTIM_OutputState_Disable((uint16_t)0x0000) #defineTIM_OutputState_Enable((uint16_t)0x0001) #defineTIM_OutputNState_Disable((uint16_t)0x0000) #defineTIM_OutputNState_Enable((uint16_t)0x0004) #defineTIM_OCNPolarity_High((uint16_t)0x0000) #defineTIM_OCNPolarity_Low((uint16_t)0x0008) #defineTIM_OCPolarity_High((uint16_t)0x0000) #defineTIM_OCPolarity_Low((uint16_t)0x0002) #defineTIM_OCIdleState_Set((uint16_t)0x0100) #defineTIM_OCIdleState_Reset((uint16_t)0x0000) #defineTIM_OCNIdleState_Set((uint16_t)0x0200) #defineTIM_OCNIdleState_Reset((uint16_t)0x0000) 函数原型 操作的寄存器和位 TIMx_CCMR1[6:4,1:0]=OC1M[1:0]+CC1S[1:0]TIMx_CCER[3:0]=CC1NP+CC1NE+CC1P+CC1ETIMx_CR2[9:8]=OIS1N+OIS1TIMx_CCR1 TIMx_CCMR1[14:12,9:8]=OC2M[1:0]+CC2S[1:0]TIMx_CCER[7:4]=CC2NP+CC2NE+CC2P+CC2ETIMx_CR2[11:10]=OIS2N+OIS2TIMx_CC2 TIMx_CCMR2[6:4,1:0]=OC3M[1:0]+CC3S[1:0]TIMx_CCER[11:8]=CC3NP+CC3NE+CC3P+CC3ETIMx_CR2[13:12]=OIS3N+OIS3TIMx_CCR3 TIMx_CCMR2[14:12,9:8]=OC4M[1:0]+CC4S[1:0]TIMx_CCER[13:12]=CC4P+CC4ETIMx_CR2[14]=OIS1TIMx_CCR4 RCC_APB2RSTR[11]=TIM1RSTRCC_APB1RSTR[5:0]=TIM7RST+…+TIM2RST TIMx_CCMR1[3]=OC1PE TIMx_CCMR1[11]=OC2PE TIMx_CCMR2[3]=OC3PE TIMx_CCMR2[11]=OC4PE voidTIM_DeInit(TIM_TypeDefTIMx);TIMx时钟关闭 voidTIM_TimeBaseInit(TIM_TypeDefTIMx,TIM_TimeBaseInitTypeDefTIM_TimeBaseInitStruct);TIMx时基初始化 TIMx_CCMR1[2]=OC1FE TIMx_CCMR1[10]=OC2FE TIMx_CCMR2[2]=OC3FE TIMx_CCMR2[10]=OC4FE voidTIM_OC1PreloadConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPreload);TIMx输出比较模式CCR1预装载配置 voidTIM_OC2PreloadConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPreload);TIMx输出比较模式CCR2预装载配置 voidTIM_OC3PreloadConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPreload); TIMx输出比较模式CCR3预装载配置 voidTIM_OC4PreloadConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPreload);TIMx输出比较模式CCR4预装载配置 voidTIM_OC1FastConfig(TIM_TypeDefTIMx,uint16_tTIM_OCFast);TIMx_CH1输出比较模式快速功能配置 voidTIM_OC2FastConfig(TIM_TypeDefTIMx,uint16_tTIM_OCFast);TIMx_CH2输出比较模式快速功能配置 voidTIM_OC3FastConfig(TIM_TypeDefTIMx,uint16_tTIM_OCFast);TIMx_CH3输出比较模式快速功能配置 voidTIM_OC4FastConfig(TIM_TypeDefTIMx,uint16_tTIM_OCFast);TIMx_CH4输出比较模式快速功能配置 TIMx_CCMR1[7]=OC1CE TIMx_CCMR1[15]=OC2CE TIMx_CCMR2[7]=OC3CE TIMx_CCMR2[15]=OC4CE voidTIM_ClearOC1Ref(TIM_TypeDefTIMx,uint16_tTIM_OCClear);TIMx_CH1输出比较模式OC1REF参考电压在外部触发信号ETRF高电平时是否清零 voidTIM_ClearOC2Ref(TIM_TypeDefTIMx,uint16_tTIM_OCClear);TIMx_CH2输出比较模式OC2REF参考电压在外部触发信号ETRF高电平时是否清零 voidTIM_ClearOC3Ref(TIM_TypeDefTIMx,uint16_tTIM_OCClear);TIMx_CH3输出比较模式OC3REF参考电压在外部触发信号ETRF高电平时是否清零 voidTIM_ClearOC4Ref(TIM_TypeDefTIMx,uint16_tTIM_OCClear);TIMx_CH4输出比较模式OC4REF参考电压在外部触发信号ETRF高电平时是否清零 voidTIM_OC1Init(TIM_TypeDefTIMx,TIM_OCInitTypeDefTIM_OCInitStruct);TIMx_CH1输出比较模式初始化 voidTIM_OC2Init(TIM_TypeDefTIMx,TIM_OCInitTypeDefTIM_OCInitStruct);TIMx_CH2输出比较模式初始化 voidTIM_OC3Init(TIM_TypeDefTIMx,TIM_OCInitTypeDefTIM_OCInitStruct); TIMx_CH3输出比较模式初始化 voidTIM_OC4Init(TIM_TypeDefTIMx,TIM_OCInitTypeDefTIM_OCInitStruct);TIMx_CH4输出比较模式初始化 voidTIM_OC1PolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPolarity);TIMx_CH1输出比较模式OC1输出极性 voidTIM_OC4PolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPolarity);TIMx_CH4输出比较模式OC1输出极性 voidTIM_OC1NPolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCNPolarity);TIMx_CH1输出比较模式OC1N输出极性 voidTIM_OC2PolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPolarity);TIMx_CH2输出比较模式OC2输出极性 voidTIM_OC2NPolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCNPolarity);TIMx_CH2输出比较模式OC2N输出极性 voidTIM_OC3PolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCPolarity);TIMx_CH3输出比较模式OC3输出极性 voidTIM_OC3NPolarityConfig(TIM_TypeDefTIMx,uint16_tTIM_OCNPolarity);TIMx_CH3输出比较模式OC3N输出极性 TIMx_CCER[1]=CC1P TIMx_CCER[3]=CC1NP TIMx_CCER[5]=CC2P TIMx_CCER[7]=CC2NP TIMx_CCER[9]=CC3P TIMx_CCER[11]=CC3NP TIMx_CCER[13]=CC4P voidTIM_OCStructInit(TIM_OCInitTypeDefTIM_OCInitStruct);TIM_OCInitTypeDef类型变量初始化 TIMx_CCMR1[6:4]=OC1M[2:0] TIMx_CCER[O]=CC1E TIMx_CCER[2]=CC1NE TIMx_CCR1 TIMx_CCER[1]=CC1P TIMx_CCER[3]=CC1NP TIMx_CR2[8]=OIS1 TIMx_CR2[9]=OIS1N TIM_OCMode=TIM_OCMode_Timing=TIMx_CCMR1[6:4]=OC1M[2:0]=0x0000TIM_OutputState=TIM_OutputState_Disable=TIMx_CCER[O]=CC1E=0x0000TIM_OutputNState=TIM_OutputNState_Disable=TIMx_CCER[2]=CC1NE=0x0000TIM_Pulse=0x0000=TIMx_CCRTIM_OCPolarity=TIM_OCPolarity_High=TIMx_CCER[1]=CC1P=0x0000TIM_OCNPolarity=TIM_OCPolarity_High=TIMx_CCER[3]=CC1NP=0x0000TIM_OCIdleState=TIM_OCIdleState_Reset=TIMx_CR2[8]=OIS1=0x0000TIM_OCNIdleState=TIM_OCNIdleState_Reset=TIMx_CR2[9]=OIS1N=0x0000 voidTIM_ForcedOC1Config(TIM_TypeDefTIMx,uint16_tTIM_ForcedAction);TIMx_CH1输出比较模式强制OC1REF参考电压为低或者高 voidTIM_ForcedOC2Config(TIM_TypeDefTIMx,uint16_tTIM_ForcedAction);TIMx_CH2输出比较模式强制OC2REF参考电压为低或者高 voidTIM_ForcedOC3Config(TIM_TypeDefTIMx,uint16_tTIM_ForcedAction);TIMx_CH3输出比较模式强制OC3REF参考电压为低或者高 voidTIM_ForcedOC4Config(TIM_TypeDefTIMx,uint16_tTIM_ForcedAction);TIMx_CH4输出比较模式强制OC4REF参考电压为低或者高 TIMx_CCMR1[6:4]=OC1M TIMx_CCMR1[14:12]=OC2M TIMx_CCMR2[6:4]=OC3M TIMx_CCMR2[14:12]=OC4M voidTIM_SetCompare2(TIM_TypeDefTIMx,uint16_tCompare2);TIMx_CH2的捕获比较寄存器CCR2的值设置 voidTIM_SetCompare3(TIM_TypeDefTIMx,uint16_tCompare3);TIMx_CH3的捕获比较寄存器CCR3的值设置 voidTIM_SetCompare4(TIM_TypeDefTIMx,uint16_tCompare4);TIMx_CH4的捕获比较寄存器CCR4的值设置 TIMx_CCR1 TIMx_CCR2 TIMx_CCR3 TIMx_CCR4 uint16_tTIM_Channel; uint16_tTIM_ICPolarity; uint16_tTIM_ICSelection; TIM_ICInitTypeDef #defineTIM_Channel_1((uint16_t)0x0000) #defineTIM_Channel_2((uint16_t)0x0004) #defineTIM_Channel_3((uint16_t)0x0008) #defineTIM_Channel_4((uint16_t)0x000C) #defineTIM_ICSelection_DirectTI((uint16_t)0x0001)/!#defineTIM_ICSelection_IndirectTI((uint16_t)0x0002)/!#defineTIM_ICSelection_TRC((uint16_t)0x0003)/!#defineTIM_ICPSC_DIV1((uint16_t)0x0000)/!#defineTIM_ICPSC_DIV2((uint16_t)0x0004)/!#defineTIM_ICPSC_DIV4((uint16_t)0x0008)/!#defineTIM_ICPSC_DIV8((uint16_t)0x000C)/!Specifiestheinputcapturefilter.Thisparametercanbeanumberbetween0x0and0xF/ TIMx_CCMR1[7:4]=IC1F[3:0] #defineTIM_ICPolarity_Rising((uint16_t)0x0000) #defineTIM_ICPolarity_Falling((uint16_t)0x0002) 用作if判断选择要设置的通道 voidTIM_ICInit(TIM_TypeDefTIMx,TIM_ICInitTypeDefTIM_ICInitStruct);TIMx捕获模式初始化 TIMx_CCMR1[7:4]=IC1F[3:0]TIMx_CCMR1[1:0]=CC1S[1:0]TIMx_CCMR1[3:2]=IC1PSC[1:0]TIMx_CCER[1]=CC1PTIMx_CCMR1[0]=CC1E该函数调用两个子函数:staticvoidTI1_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter)voidTIM_SetIC1Prescaler(TIM_TypeDefTIMx,uint16_tTIM_ICPSC) TIMx_CCMR1[14:12]=IC2F[3:0]TIMx_CCMR1[9:8]=CC2S[1:0]TIMx_CCER[5]=CC2PTIMx_CCER[4]=CC2E TIMx_CCMR1[7:4]=IC1F[3:0]TIMx_CCMR1[1:0]=CC1S[1:0]TIMx_CCER[1]=CC1PTIMx_CCER[0]=CC1E TIMx_CCMR2[7:4]=IC3F[3:0]TIMx_CCMR2[1:0]=CC3S[1:0]TIMx_CCER[9]=CC3PTIMx_CCER[8]=CC3E TIMx_CCMR2[14:12]=IC1F[3:0]TIMx_CCMR1[1:0]=CC1S[1:0]TIMx_CCER[13]=CC4PTIMx_CCER[12]=CC4E staticvoidTI1_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter)配置TIMx_CH1捕获模式 staticvoidTI2_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter)配置TIMx_CH2捕获模式 staticvoidTI3_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter)配置TIMx_CH3捕获模式 staticvoidTI4_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter)配置TIMx_CH4捕获模式 voidTIM_SetIC1Prescaler(TIM_TypeDefTIMx,uint16_tTIM_ICPSC);设置TIMx_CH1捕获通道预分频 voidTIM_SetIC2Prescaler(TIM_TypeDefTIMx,uint16_tTIM_ICPSC);设置TIMx_CH2捕获通道预分频 voidTIM_SetIC3Prescaler(TIM_TypeDefTIMx,uint16_tTIM_ICPSC);设置TIMx_CH3捕获通道预分频 voidTIM_SetIC4Prescaler(TIM_TypeDefTIMx,uint16_tTIM_ICPSC);设置TIMx_CH4捕获通道预分频 TIMx_CCMR1[3:2]=IC1PSC[1:0] TIMx_CCMR1[11:10]=IC2PSC[1:0] TIMx_CCMR2[3:2]=IC1PSC[1:0] TIMx_CCMR3[11:10]=IC2PSC[1:0] voidTIM_PWMIConfig(TIM_TypeDefTIMx,TIM_ICInitTypeDefTIM_ICInitStruct);TIMx捕获模式检测外部输入的PWM信号 uint16_tTIM_ICFilter; uint16_tTIM_ICPrescaler; TIMx_CH1和TIMx_CH2成对使用用来检测输入的PWM信号,但两者的CC1P和CC2P相反,CC1S和CC2S要选择在同一个通道上,其他TIM_ICInitTypeDef中的成员TIM_ICFilter、TIM_ICPrescaler配置相同。TIMx_CH1:TIMx_CCMR1[7:4]=IC1F[3:0]TIMx_CCMR1[1:0]=CC1S[1:0]TIMx_CCMR1[3:2]=IC1PSC[1:0]TIMx_CCER[1]=CC1PTIMx_CH2:TIMx_CCMR1[14:12]=IC2F[3:0]TIMx_CCMR1[9:8]=CC2S[1:0]TIMx_CCMR1[11:10]=IC2PSC[1:0]TIMx_CCER[5]=CC2P uint16_tTIM_OSSRState; SpecifiestheOff-StateselectionusedinRunmode.#defineTIM_OSSRState_Enable((uint16_t)0x0800) #defineTIM_OSSRState_Disable((uint16_t)0x0000) uint16_tTIM_OSSIState SpecifiestheOff-StateusedinIdlestate.#defineTIM_OSSIState_Enable((uint16_t)0x0400) #defineTIM_OSSIState_Disable((uint16_t)0x0000) uint16_tTIM_LOCKLevel uint16_tTIM_DeadTime uint16_tTIM_Break; uint16_tTIM_BreakPolarity; uint16_tTIM_AutomaticOutput; TIM_BDTRInitTypeDef #defineTIM_LOCKLevel_OFF((uint16_t)0x0000) #defineTIM_LOCKLevel_1((uint16_t)0x0100) #defineTIM_LOCKLevel_2((uint16_t)0x0200) #defineTIM_LOCKLevel_3((uint16_t)0x0300) Specifiesthedelaytimebetweentheswitching-offandtheswitching-onoftheoutputs.Thisparametercanbeanumberbetween0x00and0xFF SpecifieswhethertheTIMBreakinputisenabledornot.#defineTIM_Break_Enable((uint16_t)0x1000) #defineTIM_Break_Disable((uint16_t)0x0000) SpecifiestheTIMBreakInputpinpolarity.#defineTIM_BreakPolarity_Low((uint16_t)0x0000) #defineTIM_BreakPolarity_High((uint16_t)0x2000) SpecifieswhethertheTIMAutomaticOutputfeatureisenabledornot.#defineTIM_AutomaticOutput_Enable((uint16_t)0x4000) #defineTIM_AutomaticOutput_Disable((uint16_t)0x0000) voidTIM_BDTRConfig(TIM_TypeDefTIMx,TIM_BDTRInitTypeDefTIM_BDTRInitStruct);配置BDTR寄存器的相关位 TIMx_BDTR[14:0]=AOE+BKP+BKE+OSSR+OSSI+LOCK[1:0]+DTG[7:0] 时基设置 通道输出设置 TIMx_CR1[9:8,6:4]=CKD[1:0]+CMS[1:0]+DIR,TIM_xPsc,TIMx_ARR,TIMx_RCR,TIMx_EGR[0]=UG TIM_Period=0xFFFF=TIMx_ARRTIM_Prescaler=0x0000=TIM_xPscTIM_ClockDivision=TIM_CKD_DIV1=TIMx_CR1[9:8]=CKD[1:0]TIM_CounterMode=TIM_CounterMode_Up=TIMx_CR1[4]=DIRTIM_RepetitionCounter=0x0000=TIMx_RCR 通道输入设置 刹车功能设置 voidTIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDefTIM_TimeBaseInitStruct);默认时基TIM_TimeBaseInitTypeDef结构体定义 voidTIM_ICStructInit(TIM_ICInitTypeDefTIM_ICInitStruct);默认输入捕获TIM_ICInitTypeDef结构体定义 TIMx_CCER[1]=CC1P TIMx_CCMR1[1:0]=CC1S[1:0] TIM_Channel=TIM_Channel_1TIM_ICPolarity=TIM_ICPolarity_Rising=TIMx_CCER[1]=CC1P=0TIM_ICSelection=TIM_ICSelection_DirectTITIMx_CCMR1[1:0]=CC1S[1:0]=01TIM_ICPrescaler=TIM_ICPSC_DIV1=TIMx_CCMR1[3:2]=IC1PSC[1:0]=00TIM_ICFilter=0x00 voidTIM_BDTRStructInit(TIM_BDTRInitTypeDefTIM_BDTRInitStruct);默认刹车和死区时间寄存器TIM_BDTRInitTypeDef结构体定义 TIM_OSSRState=TIM_OSSRState_DisableTIM_OSSIState=TIM_OSSIState_DisableTIM_LOCKLevel=TIM_LOCKLevel_OFFTIM_DeadTime=0x00TIM_Break=TIM_Break_DisableTIM_BreakPolarity=TIM_BreakPolarity_LowTIM_AutomaticOutput=TIM_AutomaticOutput_Disable TIX_BDTR[11]=OSSR TIX_BDTR[10]=OSSI TIX_BDTR[9:8]=LOCK[1:0] TIX_BDTR[7:0]=DTG[7:0] TIX_BDTR[12]=BKE TIX_BDTR[13]=BKP TIX_BDTR[14]=AOE voidTIM_Cmd(TIM_TypeDefTIMx,FunctionalStateNewState);使能或者禁能CNT计数器 TIMx->CR1[0]=CEN voidTIM_CtrlPWMOutputs(TIM_TypeDefTIMx,FunctionalStateNewState);使能或者禁能TIMx主输出 TIMx_BDTR[15]=MOE voidTIM_ITConfig(TIM_TypeDefTIMx,uint16_tTIM_IT,FunctionalStateNewState);中断使能和禁能 TIMx_DIER[7:0]=BIE+TIE+COMIE+CC4IE+CC3IE+CC2IE+CC1IE+UIE 事件产生设置 voidTIM_GenerateEvent(TIM_TypeDefTIMx,uint16_tTIM_EventSource);事件生成 TIMx_EGR[7:0]=BG+TG+COMG+CC4G+CC3G+CC2G+CC1G+UG 中断和DMA设置 voidTIM_DMAConfig(TIM_TypeDefTIMx,uint16_tTIM_DMABase,uint16_tTIM_DMABurstLength);DMA方式访问寄存器设置 TIMx_DCR[12:8,4:0]=DBL[4:0]+DBA[4:0] voidTIM_DMACmd(TIM_TypeDefTIMx,uint16_tTIM_DMASource,FunctionalStateNewState);DMA请求使能和禁能 TIMx_DIER=TDE+COMDE+CC4DE+CC3DE+CC2DE+CC1DE+UDE 从模式控制器设置 voidTIM_InternalClockConfig(TIM_TypeDefTIMx);配置TIMx使用内部时钟CK_INT TIMx_SMCR[2:0]=SMS[2:0]=000 voidTIM_SelectInputTrigger(TIM_TypeDefTIMx,uint16_tTIM_InputTriggerSource);选择触发器的输入源 TIMx_SMCR[6:4]=TS[2:0] voidTIM_ITRxExternalClockConfig(TIM_TypeDefTIMx,uint16_tTIM_InputTriggerSource);ExternalClockMode1配置 调用TIM_SelectInputTrigger()子函数。TIMx_SMCR[6:4]=TS[2:0]TIMx_SMCR[2:0]=SMS[2:0]=111 voidTIM_TIxExternalClockConfig(TIM_TypeDefTIMx,uint16_tTIM_TIxExternalCLKSource,uint16_tTIM_ICPolarity,uint16_tICFilter) TIMx_CCER[1:0]=CC1P+CC1E(1)TIMx_CCMR1[7:4,1:0]=IC1F[3:0]+CC1S[1:0] 调用子函数voidTI1_Config()和TIM_SelectInputTrigger()。TIMx_CCER[1:0]=CC1P+CC1E(1)TIMx_CCMR1[7:4,1:0]=IC1F[3:0]+CC1S[1:0]TIMx_SMCR[6:4]=TS[2:0]=100~110TIMx_SMCR[2:0]=SMS[2:0]=111 staticvoidTI1_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter);配置TI1作为输入 TIMx_CCER[5:4]=CC2P+CC2E(1)TIMx_CCMR1[15:12,9:8]=IC2F[3:0]+CC2S[1:0] staticvoidTI2_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter);配置TI2作为输入 staticvoidTI3_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter);配置TI3作为输入 staticvoidTI4_Config(TIM_TypeDefTIMx,uint16_tTIM_ICPolarity,uint16_tTIM_ICSelection,uint16_tTIM_ICFilter);配置TI4作为输入 TIMx_CCER[9:8]=CC3P+CC3E(1)TIMx_CCMR2[7:4,1:0]=IC3F[3:0]+CC3S[1:0] TIMx_CCER[13:12]=CC4P+CC4E(1)TIMx_CCMR2[15:12,9:8]=IC4F[3:0]+CC4S[1:0] voidTIM_ETRClockMode1Config(TIM_TypeDefTIMx,uint16_tTIM_ExtTRGPrescaler,uint16_tTIM_ExtTRGPolarity,uint16_tExtTRGFilter);TIMx_CH1用TIMx_ETR作为触发源的外部模式1配置 voidTIM_ETRConfig(TIM_TypeDefTIMx,uint16_tTIM_ExtTRGPrescaler,uint16_tTIM_ExtTRGPolarity,uint16_tExtTRGFilter);TIMx_ETR的配置 调用函数TIM_ETRConfig()。TIMx_SMCR[6:4]=TS[2:0]=111TIMx_SMCR[2:0]=SMS[2:0]=111TIMx_SMCR[15,14,13:12,11:8]=ETP+ECE(0)+ETPS[1:0]+ETF[3:0] voidTIM_ETRClockMode2Config(TIM_TypeDefTIMx,uint16_tTIM_ExtTRGPrescaler,uint16_tTIM_ExtTRGPolarity,uint16_tExtTRGFilter);使能外部时钟模式2 调用函数TIM_ETRConfig()。TIMx_SMCR[15,14,13:12,11:8]=ETP+ECE(1)+ETPS[1:0]+ETF[3:0] voidTIM_PrescalerConfig(TIM_TypeDefTIMx,uint16_tPrescaler,uint16_tTIM_PSCReloadMode);配置TIMx_PSC TIMx_Psc,TIMx_EGR[0]=UG voidTIM_CounterModeConfig(TIM_TypeDefTIMx,uint16_tTIM_CounterMode);计数模式配置 TIMx_CR1[6:4]=CMS[1:0]+DIR voidTIM_EncoderInterfaceConfig(TIM_TypeDefTIMx,uint16_tTIM_EncoderMode,uint16_tTIM_IC1Polarity,uint16_tTIM_IC2Polarity);配置编码器模式 voidTIM_ARRPreloadConfig(TIM_TypeDefTIMx,FunctionalStateNewState);配置TIMx_ARR是否可以预装载 TIMx_CR1[7]=ARPE CR2相关位配置 voidTIM_SelectCOM(TIM_TypeDefTIMx,FunctionalStateNewState);配置CR2的位2CCUS voidTIM_SelectCCDMA(TIM_TypeDefTIMx,FunctionalStateNewState);配置CR2的位3CCDS TIMx_CR2[2]=CCUS TIMx_CR2[3]=CCDS voidTIM_CCPreloadControl(TIM_TypeDefTIMx,FunctionalStateNewState);配置CR2位0CCPC CCER相关位设置 TIMx_CCER[12,8,4,0]=CC1E+CC2E+CC3E+CC4E voidTIM_CCxCmd(TIM_TypeDefTIMx,uint16_tTIM_Channel,uint16_tTIM_CCx);使能或者禁能CHx捕获、输出功能 voidTIM_CCxNCmd(TIM_TypeDefTIMx,uint16_tTIM_Channel,uint16_tTIM_CCxN);使能或者禁能CHxN捕获、输出功能 TIMx_CCER[14,10,6,2]=CC1NE+CC2NE+CC3NE+CC4NE voidTIM_SelectOCxM(TIM_TypeDefTIMx,uint16_tTIM_Channel,uint16_tTIM_OCMode);设置输出通道的模式OCxM TIMx_CCMR1[14:12,6:4]=OC2M[2:0]+OC1M[2:0]TIMx_CCMR2[14:12,6:4]=OC3M[2:0]+OC4M[2:0] CR1相关位配置 voidTIM_UpdateDisableConfig(TIM_TypeDefTIMx,FunctionalStateNewState);更新CR1的Updatedisable位 TIMx_CR1[1]=UDIS voidTIM_UpdateRequestConfig(TIM_TypeDefTIMx,uint16_tTIM_UpdateSource);更新CR1的Updaterequestsource位 TIMx_CR1[2]=URS voidTIM_SelectHallSensor(TIM_TypeDefTIMx,FunctionalStateNewState);使能或禁能定时器的霍尔传感器接口 TIMx_CR2[0]=CCPC TIMx_CR2[7]=TI1S voidTIM_SelectOnePulseMode(TIM_TypeDefTIMx,uint16_tTIM_OPMode);选择单脉冲模式 TIMx_CR1[3]=OPM voidTIM_SelectOutputTrigger(TIM_TypeDefTIMx,uint16_tTIM_TRGOSource);主模式选择 TIMx_CR2[6:4]=MMS[2:0] voidTIM_SelectSlaveMode(TIM_TypeDefTIMx,uint16_tTIM_SlaveMode);从模式选择 TIMx_SMCR[15,14,13:12,11:8]=ETP+ECE(0)+ETPS[1:0]+ETF[3:0] TIMx_SMCR[2:0]=SMS[2:0] voidTIM_SelectMasterSlaveMode(TIM_TypeDefTIMx,uint16_tTIM_MasterSlaveMode);主从模式选择 TIMX_SMCR[2:0]=SMS[2:0]=001~011,CCMR1[9:8,1:0]=CC2S[1:0](01)+CC1S[1:0](01),CCER[5,1]=CC2P+CC1P TIMX_SMCR[7]=MSM voidTIM_SetCounter(TIM_TypeDefTIMx,uint16_tCounter);设置计数器 TIMx->CNT voidTIM_SetAutoreload(TIM_TypeDefTIMx,uint16_tAutoreload);设置重装载计数器 TIMx->ARR voidTIM_SetClockDivision(TIM_TypeDefTIMx,uint16_tTIM_CKD);设置时钟分频因子 TIMx_CR1[9:8]=CDK[1:0] voidTIM_SetCompare1(TIM_TypeDefTIMx,uint16_tCompare1);TIMx_CH1的捕获比较寄存器CCR1的值设置 uint16_tTIM_GetCapture1(TIM_TypeDefTIMx);获取TIMx_CH1的捕获比较寄存器CCR1的值 uint16_tTIM_GetCapture2(TIM_TypeDefTIMx);获取TIMx_CH2的捕获比较寄存器CCR2的值 uint16_tTIM_GetCapture3(TIM_TypeDefTIMx);获取TIMx_CH3的捕获比较寄存器CCR3的值 uint16_tTIM_GetCapture4(TIM_TypeDefTIMx);获取TIMx_CH4的捕获比较寄存器CCR4的值 uint16_tTIM_GetCounter(TIM_TypeDefTIMx);获取计数器CNT的值 uint16_tTIM_GetPrescaler(TIM_TypeDefTIMx);获取预分频寄存器的值 returnTIMx->CNT returnTIMx->PSC SR(状态寄存器)配置 FlagStatusTIM_GetFlagStatus(TIM_TypeDefTIMx,uint16_tTIM_FLAG);获取相应位的状态 TIMx->SR&TIM_FLAG voidTIM_ClearFlag(TIM_TypeDefTIMx,uint16_tTIM_FLAG);清除相应的状态标志位 TIMx->SR=(uint16_t)~TIM_FLAG ITStatusTIM_GetITStatus(TIM_TypeDefTIMx,uint16_tTIM_IT);检测中断是否发生 TIMx->SR&TIM_ITTIMx->DIER&TIM_IT voidTIM_ClearITPendingBit(TIM_TypeDefTIMx,uint16_tTIM_IT);清除悬挂起的中断请求 TIMx->SR=(uint16_t)~TIM_IT;
|
|