| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include <MyProject.h>
- void Timer2_Init(void)
- {
-
- ClrBit(PH_SEL , T2SEL); //P10
- ClrBit(PH_SEL , T2SSEL); //P07
- SetReg(TIM2_CR0, T2PSC0 | T2PSC1 | T2PSC2, T2PSC0 );
-
- ClrBit(TIM2_CR0 , T2OCM);
- SetBit(TIM2_CR0 , T2IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
- ClrBit(TIM2_CR0 , T2CES);
-
- SetBit(TIM2_CR1 , T2IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
- ClrBit(TIM2_CR1 , T2IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
- ClrBit(TIM2_CR1 , T2FE); //输入噪声滤波使能,小于4个时钟周期脉宽滤除
- ClrBit(TIM2_CR1 , T2DIR); //QEP&ISD&步进模式专用:当前的方向 0-->正向 1-->反向
- TIM2__CNTR = 0;
- TIM2__DR = 1200;
- TIM2__ARR = 24000.;
- ClrBit(TIM2_CR0 , T2MOD1); //00-->输入Timer模式 01-->输出模式
- SetBit(TIM2_CR0 , T2MOD0); //10-->输入Counter模式 11-->QEP&ISD&步进模式
-
- }
- /* -------------------------------------------------------------------------------------------------
- Function Name : Timer3_Init
- Description : 定时器3初始化
- Date : 2021-11-08
- Parameter : None
- ------------------------------------------------------------------------------------------------- */
- void Timer3_Init(void)
- {
- ClrBit(TIM3_CR1 , T3EN);
- SetBit(PH_SEL , T3SEL); //Timer3端口使能
- ClrBit(PH_SEL1 , T3CT0); //默认端口为P11,功能转移后为P01,需TIMER4转移到P00
- ClrBit(TIM3_CR0 , T3PSC2); //计数器时钟分频选择
- ClrBit(TIM3_CR0 , T3PSC1); //000-->24M 001-->12M 010-->6M 011-->3M
- ClrBit(TIM3_CR0 , T3PSC0); //100-->1.5M 101-->750K 110-->375K 111-->187.5K
- SetBit(TIM3_CR0 , T3MOD); //0-->Timer模式 1-->输出模式
- ClrBit(TIM3_CR0 , T3OCM);
- ClrBit(TIM3_CR0 , T3IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
- ClrBit(TIM3_CR0 , T3OPM); //0-->计数器不停止 1-->单次模式
- ClrBit(TIM3_CR1 , T3IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
- ClrBit(TIM3_CR1 , T3IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
- SetBit(TIM3_CR1 , T3NM1); //输入噪声脉宽选择
- ClrBit(TIM3_CR1 , T3NM0); //00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
- ClrBit(IP2,PTIM31 );
- SetBit(IP2,PTIM30 ); //中断优先级设置为1
- TIM3__CNTR = 1;
- TIM3__ARR =1200;
- TIM3__DR = 1200;
- SetBit(TIM3_CR1 , T3EN); //TIM3使能 0-->Disable 1-->Enable
- }
- /**
- * @brief TIM4初始化配置
- */
- void Timer4_Init(void)
- {
- ClrBit(TIM4_CR1, T4EN); // 0 - 停止计数;1 - 使能计数
- SetBit(PH_SEL, T4SEL); //端口复用为 Timer4
-
- ClrBit(PH_SEL1,T4CT0); //Timer4 功能转移 00: P0.1 为 Timer4 输入输出
- ClrBit(PH_SEL1,T4CT1);
- SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2, T4PSC0 );
-
- SetBit(TIM4_CR0 , T4OCM);
- ClrBit(TIM4_CR0 , T4IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
- ClrBit(TIM4_CR0 , T4OPM); //0-->计数器不停止 1-->单次模式
-
- ClrBit(TIM4_CR1 , T4IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
- ClrBit(TIM4_CR1 , T4IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
- ClrBit(TIM4_CR1 , T4NM1); //输入噪声脉宽选择
- ClrBit(TIM4_CR1 , T4NM0); //00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
- ClrBit(IP2,PTIM40);
- SetBit(IP2,PTIM41);
-
- TIM4__DR = 12000; //输入模式,DR和ARR的值由硬件写;
- TIM4__ARR = 24000.;
- TIM4__CNTR = 0;
- SetBit(TIM4_CR0 , T4MOD); //0-->Timer模式 1-->输出模式
- ClrBit(TIM4_CR1 , T4EN); //TIM4使能 0-->Disable 1-->Enable
- }
|