TIMER.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* --------------------------- (C) COPYRIGHT 2021 Fortiortech ShenZhen -----------------------------
  2. File Name : TIMER.c
  3. Author : Fortiortech Appliction Team
  4. Version : V1.0
  5. Date : 2021-04-11
  6. Description : This file contains .C file function used for Motor Control.
  7. ----------------------------------------------------------------------------------------------------
  8. All Rights Reserved
  9. ------------------------------------------------------------------------------------------------- */
  10. #include <MyProject.h>
  11. #include <TIMER.h>
  12. void Timer1_Init(void)
  13. {
  14. }
  15. void Timer2_Init(void)
  16. {
  17. ClrBit(TIM2_CR1, T2CEN); // TIM2使能 0-->Disable 1-->Enable
  18. ClrBit(PH_SEL, T2SEL); // P10复用作为 Timer2 的输入输出
  19. ClrBit(PH_SEL, T2SSEL); // P07复用作为 Timer2 的输入输出
  20. ClrBit(TIM2_CR0, T2PSC2); // 计数器时钟分频选择
  21. SetBit(TIM2_CR0, T2PSC1); // 000-->24M 001-->12M 010-->6M 011-->3M
  22. SetBit(TIM2_CR0, T2PSC0); // 100-->1.5M 101-->750K 110-->375K 111-->187.5K
  23. ClrBit(TIM2_CR0, T2MOD1); // 00-->输入Timer模式 01-->输出模式
  24. SetBit(TIM2_CR0, T2MOD0); // 10-->输入Counter模式 11-->QEP&ISD&步进模式
  25. ClrBit(TIM2_CR0, T2OCM); // 0: TIM2__CNTR < TIM2__DR,输出 0;TIM2__CNTR ≥ TIM2__DR,输出 1
  26. // 1: TIM2__CNTR < TIM2__DR,输出 1;TIM2__CNTR ≥ TIM2__DR,输出 0
  27. ClrBit(TIM2_CR0, T2CES); // 清零脉冲计数器不使能
  28. SetBit(TIM2_CR0, T2IRE); // 比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  29. SetBit(TIM2_CR1, T2IFE); // 计数器上溢中断使能 0-->Disable 1-->Enable
  30. ClrBit(TIM2_CR1, T2IR | T2IF | T2IP); // 清零中断标志位
  31. ClrBit(TIM2_CR1, T2IPE); // 输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  32. ClrBit(TIM2_CR1, T2FE); // 输入噪声滤波使能,小于4个时钟周期脉宽滤除
  33. ClrBit(TIM2_CR1, T2DIR); // QEP&ISD&步进模式专用:当前的方向 0-->正向 1-->反向
  34. SetBit(IP1, PTIM21);
  35. SetBit(IP1, PTIM20); // 中断优先级别为2
  36. TIM2__ARR = 30000; // //3M分频,1/3us,30000对应延迟10ms
  37. TIM2__DR = 0;
  38. TIM2__CNTR = 0;
  39. SetBit(TIM2_CR1, T2CEN); // TIM2使能 0-->Disable 1-->Enable
  40. }
  41. /* -------------------------------------------------------------------------------------------------
  42. Function Name : Timer3_Init
  43. Description : 定时器3初始化
  44. Date : 2021-11-08
  45. Parameter : None
  46. ------------------------------------------------------------------------------------------------- */
  47. void Timer3_Init(void)
  48. {
  49. SetBit(PH_SEL, T3SEL); // Timer3端口使能
  50. ClrBit(PH_SEL1, T3CT0);
  51. SetBit(PH_SEL1, T3CT1); // 00:P11 X1:P01 10:P47
  52. SetBit(TIM3_CR0, T3PSC2); // 计数器时钟分频选择
  53. SetBit(TIM3_CR0, T3PSC1); // 000-->24M 001-->12M 010-->6M 011-->3M
  54. ClrBit(TIM3_CR0, T3PSC0); // 100-->1.5M 101-->750K 110-->375K 111-->187.5K
  55. ClrBit(TIM3_CR0, T3OCM);
  56. ClrBit(TIM3_CR0, T3IRE); // 比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  57. ClrBit(TIM3_CR0, T3OPM); // 0-->计数器不停止 1-->单次模式
  58. SetBit(TIM3_CR1, T3IPE); // 输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  59. SetBit(TIM3_CR1, T3IFE); // 计数器上溢中断使能 0-->Disable 1-->Enable
  60. ClrBit(TIM3_CR1, T3NM1); // 输入噪声脉宽选择
  61. ClrBit(TIM3_CR1, T3NM0); // 00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
  62. // /* 定时器配置 */
  63. // SetBit(IP2, PTIM31); //1ms定时中断优先级别为1
  64. // SetBit(IP2, PTIM30);
  65. // TIM3__DR = 1200;
  66. // TIM3__ARR = 2400;
  67. ClrBit(TIM3_CR0, T3MOD); // 0-->Timer模式 1-->输出模式
  68. SetBit(TIM3_CR1, T3EN); // TIM3使能 0-->Disable 1-->Enable
  69. }
  70. // void Timer4_Init(void)
  71. //{
  72. // ClrBit(PH_SEL , T4SEL); //Timer4端口使能
  73. // ClrBit(PH_SEL1 , T4CT); //默认端口为P11,功能转移后为P01,需TIMER4转移到P00
  74. // ClrBit(TIM4_CR0 , T4PSC2); //计数器时钟分频选择
  75. // ClrBit(TIM4_CR0 , T4PSC1); //000-->24M 001-->12M 010-->6M 011-->3M
  76. // ClrBit(TIM4_CR0 , T4PSC0); //100-->1.5M 101-->750K 110-->375K 111-->187.5K
  77. // ClrBit(TIM4_CR0 , T4OCM);
  78. // ClrBit(TIM4_CR0 , T4IRE); //比较匹配中断/脉宽检测中断0-->Disable 1-->Enable
  79. // ClrBit(TIM4_CR0 , T4OPM); //0-->计数器不停止 1-->单次模式
  80. // ClrBit(TIM4_CR1 , T4IPE); //输入Timer PWM周期检测中断使能 0-->Disable 1-->Enable
  81. // ClrBit(TIM4_CR1 , T4IFE); //计数器上溢中断使能 0-->Disable 1-->Enable
  82. // ClrBit(TIM4_CR1 , T4NM1); //输入噪声脉宽选择
  83. // ClrBit(TIM4_CR1 , T4NM0); //00-->不滤波 01-->4cycles 10-->8cycles 11-->16cycles
  84. // TIM4__ARR = 4800;
  85. // TIM4__DR = TIM4__ARR>>1 ;
  86. // TIM4__CNTR = 1;
  87. // PTIM41 = 1;
  88. // PTIM40 = 1;
  89. // SetBit(TIM4_CR0 , T4MOD); //0-->Timer模式 1-->输出模式
  90. // ClrBit(TIM4_CR1 , T4EN); //TIM3使能 0-->Disable 1-->Enable
  91. //}
  92. void TIM4_Init_RF(void)
  93. {
  94. }