1
0

TIMER.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <MyProject.h>
  2. void Timer2_Init(void)
  3. {
  4. ClrBit(PH_SEL , T2SEL); //P10
  5. ClrBit(PH_SEL , T2SSEL); //P07
  6. SetReg(TIM2_CR0, T2PSC0 | T2PSC1 | T2PSC2, T2PSC0 );
  7. ClrBit(TIM2_CR0 , T2OCM);
  8. SetBit(TIM2_CR0 , T2IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  9. ClrBit(TIM2_CR0 , T2CES);
  10. SetBit(TIM2_CR1 , T2IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  11. ClrBit(TIM2_CR1 , T2IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
  12. ClrBit(TIM2_CR1 , T2FE); //输入噪声滤波使能,小于4个时钟周期脉宽滤除
  13. ClrBit(TIM2_CR1 , T2DIR); //QEP&ISD&步进模式专用:当前的方向 0-->正向 1-->反向
  14. TIM2__CNTR = 0;
  15. TIM2__DR = 1200;
  16. TIM2__ARR = 24000.;
  17. ClrBit(TIM2_CR0 , T2MOD1); //00-->输入Timer模式 01-->输出模式
  18. SetBit(TIM2_CR0 , T2MOD0); //10-->输入Counter模式 11-->QEP&ISD&步进模式
  19. }
  20. /* -------------------------------------------------------------------------------------------------
  21. Function Name : Timer3_Init
  22. Description : 定时器3初始化
  23. Date : 2021-11-08
  24. Parameter : None
  25. ------------------------------------------------------------------------------------------------- */
  26. void Timer3_Init(void)
  27. {
  28. ClrBit(TIM3_CR1 , T3EN);
  29. SetBit(PH_SEL , T3SEL); //Timer3端口使能
  30. ClrBit(PH_SEL1 , T3CT0); //默认端口为P11,功能转移后为P01,需TIMER4转移到P00
  31. ClrBit(TIM3_CR0 , T3PSC2); //计数器时钟分频选择
  32. ClrBit(TIM3_CR0 , T3PSC1); //000-->24M 001-->12M 010-->6M 011-->3M
  33. ClrBit(TIM3_CR0 , T3PSC0); //100-->1.5M 101-->750K 110-->375K 111-->187.5K
  34. SetBit(TIM3_CR0 , T3MOD); //0-->Timer模式 1-->输出模式
  35. ClrBit(TIM3_CR0 , T3OCM);
  36. ClrBit(TIM3_CR0 , T3IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  37. ClrBit(TIM3_CR0 , T3OPM); //0-->计数器不停止 1-->单次模式
  38. ClrBit(TIM3_CR1 , T3IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  39. ClrBit(TIM3_CR1 , T3IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
  40. SetBit(TIM3_CR1 , T3NM1); //输入噪声脉宽选择
  41. ClrBit(TIM3_CR1 , T3NM0); //00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
  42. ClrBit(IP2,PTIM31 );
  43. SetBit(IP2,PTIM30 ); //中断优先级设置为1
  44. TIM3__CNTR = 1;
  45. TIM3__ARR =1200;
  46. TIM3__DR = 1200;
  47. SetBit(TIM3_CR1 , T3EN); //TIM3使能 0-->Disable 1-->Enable
  48. }
  49. /**
  50. * @brief TIM4初始化配置
  51. */
  52. void Timer4_Init(void)
  53. {
  54. ClrBit(TIM4_CR1, T4EN); // 0 - 停止计数;1 - 使能计数
  55. SetBit(PH_SEL, T4SEL); //端口复用为 Timer4
  56. ClrBit(PH_SEL1,T4CT0); //Timer4 功能转移 00: P0.1 为 Timer4 输入输出
  57. ClrBit(PH_SEL1,T4CT1);
  58. SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2, T4PSC0 );
  59. SetBit(TIM4_CR0 , T4OCM);
  60. ClrBit(TIM4_CR0 , T4IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  61. ClrBit(TIM4_CR0 , T4OPM); //0-->计数器不停止 1-->单次模式
  62. ClrBit(TIM4_CR1 , T4IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  63. ClrBit(TIM4_CR1 , T4IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
  64. ClrBit(TIM4_CR1 , T4NM1); //输入噪声脉宽选择
  65. ClrBit(TIM4_CR1 , T4NM0); //00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
  66. ClrBit(IP2,PTIM40);
  67. SetBit(IP2,PTIM41);
  68. TIM4__DR = 12000; //输入模式,DR和ARR的值由硬件写;
  69. TIM4__ARR = 24000.;
  70. TIM4__CNTR = 0;
  71. SetBit(TIM4_CR0 , T4MOD); //0-->Timer模式 1-->输出模式
  72. ClrBit(TIM4_CR1 , T4EN); //TIM4使能 0-->Disable 1-->Enable
  73. }