hw_pwm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "board.h"
  2. typedef struct
  3. {
  4. uint32_t base; //对象实体
  5. int unit_in_hz; //1秒多少hz
  6. uint16_t ch; //通道选择
  7. uint16_t irqn; //中断源
  8. } hw_pwm_t;
  9. typedef struct
  10. {
  11. uint32_t pulse_port; //端口
  12. unsigned short pulse_pin; //引脚
  13. unsigned short pulse_af; //复用功能位置
  14. uint32_t dir_port; //方向功能端口
  15. unsigned short dir_pin; //方向功能引脚
  16. unsigned short dir_level; //有效电平 0时,表示高电平是正方向,低电平是反方向,1时表示高电平是反方向,低电平是正方向
  17. uint32_t en_port; //使能功能端口
  18. unsigned short en_pin; //使能功能引脚
  19. unsigned short en_level; //有效电平 0时,表示高电平是正方向,低电平是反方向,1时表示高电平是反方向,低电平是正方向
  20. } hw_pwm_pin_t;
  21. static hw_pwm_pin_t pin_maps[HW_PWM_NUMBER] = {
  22. { GPIOA, 8, 2, GPIOF, 5, 0, GPIOC, 3 ,0},
  23. { GPIOB, 15, 1, GPIOC, 13,0, GPIOA, 6 ,0}
  24. };
  25. static hw_pwm_t hw_pwm_maps[HW_PWM_NUMBER] = {
  26. { TIMER0, 8000000, 0, TIMER0_BRK_UP_TRG_COM_IRQn },
  27. { TIMER14, 8000000, 1, TIMER14_IRQn }
  28. };
  29. //=======================================编码器定义========================================//
  30. typedef struct
  31. {
  32. uint32_t base; //对象实体
  33. uint16_t irqn; //中断源
  34. uint16_t period; //一圈的总脉冲数
  35. } hw_tim_encoder_t;
  36. typedef struct
  37. {
  38. uint32_t aport; //端口
  39. unsigned short apin; //引脚
  40. uint32_t bport; //方向功能端口
  41. unsigned short bpin; //方向功能引脚
  42. unsigned short af; //复用功能位置
  43. } hw_encode_pin_t;
  44. static hw_encode_pin_t encoder_pin_maps[HW_ENCODER_NUMBER] = {
  45. { GPIOA, 6, GPIOA, 7, 1 }
  46. };
  47. static hw_tim_encoder_t encoders[HW_ENCODER_NUMBER] = {
  48. { TIMER2, TIMER2_IRQn, 10000 }
  49. };
  50. //PWM编号分配,必须与硬件一致
  51. #define TIM0_TO_PWM HW_PWM_1
  52. #define TIM14_TO_PWM HW_PWM_2
  53. //编码器编号分配,必须与硬件一致 注:GD32E230仅TIM0、TIM2支持
  54. #define HW_TIM2_TO_ENCODER 0
  55. #if defined(HW_TIM0_TO_ENCODER) && defined(TIM0_TO_PWM)
  56. #error "TIM0 can't as encoder and pwm at the same time!"
  57. #endif
  58. #if defined(HW_TIM2_TO_ENCODER) && defined(TIM2_TO_PWM)
  59. #error "TIM2 can't as encoder and pwm at the same time!"
  60. #endif
  61. //周期寄存器表
  62. uint32_t *hw_pwm_period_registers[HW_PWM_NUMBER];
  63. //占空比寄存器表
  64. uint32_t *hw_pwm_dutycycle_registers[HW_PWM_NUMBER];
  65. //配置寄存器表
  66. uint16_t *hw_pwm_cr_registers[HW_PWM_NUMBER];
  67. //配置寄存器表
  68. uint16_t *hw_pwm_egr_registers[HW_PWM_NUMBER];
  69. //方向管脚读写表
  70. hw_pin_readwrite_t hw_pwm_dir_rw_map[HW_PWM_NUMBER];
  71. //使能管脚读写表
  72. hw_pin_readwrite_t hw_pwm_en_rw_map[HW_PWM_NUMBER];
  73. //编码器圈数保存
  74. static int encoder_cycles[HW_ENCODER_NUMBER] = { 0 };
  75. //回调函数,需要使用PWM前进行注册
  76. static pwm_handle pwm_handle_maps[HW_PWM_NUMBER];
  77. //回调参数
  78. static void *pwm_hanlde_parames[HW_PWM_NUMBER];
  79. #if defined(TIM0_TO_PWM) || defined(HW_TIM0_TO_ENCODER)
  80. void TIMER0_BRK_UP_TRG_COM_IRQHandler(void) {
  81. #if defined(TIM0_TO_PWM)
  82. if (TIMER_INTF(TIMER0) & (1 << 0)) {
  83. TIMER_INTF(TIMER0) &= ~(1 << 0);
  84. pwm_handle_maps[TIM0_TO_PWM](pwm_hanlde_parames[TIM0_TO_PWM]);
  85. }
  86. #endif
  87. #if defined(HW_TIM0_TO_ENCODER)
  88. if (TIMER_INTF(TIMER0) & (1 << 0)) {
  89. if (TIMER_CTL0(TIMER0) & (1 << 4)) {
  90. encoder_cycles[HW_TIM0_TO_ENCODER]--;
  91. }
  92. else if ((TIMER_CTL0(TIMER0) & (1 << 4)) == 0) {
  93. encoder_cycles[HW_TIM0_TO_ENCODER]++;
  94. }
  95. TIMER_INTF(TIMER0) &= ~(1 << 0);
  96. }
  97. #endif
  98. }
  99. #endif
  100. #if defined(TIM2_TO_PWM) || defined(HW_TIM2_TO_ENCODER)
  101. void TIMER2_IRQHandler(void) {
  102. #if defined(TIM2_TO_PWM)
  103. if (TIMER_INTF(TIMER2) & (1 << 0)) {
  104. TIMER_INTF(TIMER2) &= ~(1 << 0);
  105. pwm_handle_maps[TIM2_TO_PWM](pwm_hanlde_parames[TIM2_TO_PWM]);
  106. }
  107. #endif
  108. #if defined(HW_TIM2_TO_ENCODER)
  109. if (TIMER_INTF(TIMER2) & (1 << 0)) {
  110. if (TIMER_CTL0(TIMER2) & (1 << 4)) {
  111. encoder_cycles[HW_TIM2_TO_ENCODER]--;
  112. }
  113. else if ((TIMER_CTL0(TIMER2) & (1 << 4)) == 0) {
  114. encoder_cycles[HW_TIM2_TO_ENCODER]++;
  115. }
  116. TIMER_INTF(TIMER2) &= ~(1 << 0);
  117. }
  118. #endif
  119. }
  120. #endif
  121. #if defined(TIM13_TO_PWM)
  122. void TIMER13_IRQHandler(void) {
  123. if (TIMER_INTF(TIMER13) & (1 << 0)) {
  124. TIMER_INTF(TIMER13) &= ~(1 << 0);
  125. pwm_handle_maps[TIM13_TO_PWM](pwm_hanlde_parames[TIM13_TO_PWM]);
  126. }
  127. }
  128. #endif
  129. #if defined(TIM14_TO_PWM)
  130. void TIMER14_IRQHandler(void) {
  131. if (TIMER_INTF(TIMER14) & (1 << 0)) {
  132. TIMER_INTF(TIMER14) &= ~(1 << 0);
  133. pwm_handle_maps[TIM14_TO_PWM](pwm_hanlde_parames[TIM14_TO_PWM]);
  134. }
  135. }
  136. #endif
  137. #if defined(TIM15_TO_PWM)
  138. void TIMER15_IRQHandler(void) {
  139. if (TIMER_INTF(TIMER15) & (1 << 0)) {
  140. TIMER_INTF(TIMER15) &= ~(1 << 0);
  141. pwm_handle_maps[TIM15_TO_PWM](pwm_hanlde_parames[TIM15_TO_PWM]);
  142. }
  143. }
  144. #endif
  145. #if defined(TIM16_TO_PWM)
  146. void TIMER16_IRQHandler(void) {
  147. if (TIMER_INTF(TIMER16) & (1 << 0)) {
  148. TIMER_INTF(TIMER16) &= ~(1 << 0);
  149. pwm_handle_maps[TIM16_TO_PWM](pwm_hanlde_parames[TIM16_TO_PWM]);
  150. }
  151. }
  152. #endif
  153. /**
  154. * 默认指针函数
  155. *
  156. * @author LXZ (121919)
  157. *
  158. * @param handler
  159. */
  160. void hw_pwm_default_handler(void *handler) {
  161. }
  162. /**
  163. * 设置方向
  164. *
  165. * @author lxz
  166. *
  167. * @param no
  168. * @param dir
  169. */
  170. void hw_pwm_set_dir(unsigned char no, int dir) {
  171. if (pin_maps[no].dir_port != 0) {
  172. if (dir) {
  173. GPIO_BOP(pin_maps[no].dir_port) = 1 << pin_maps[no].dir_pin;
  174. }
  175. else {
  176. GPIO_BOP(pin_maps[no].dir_port) = 1 << (pin_maps[no].dir_pin+16);
  177. }
  178. }
  179. }
  180. /**
  181. * 获取方向
  182. *
  183. * @author lxz
  184. *
  185. * @param no
  186. *
  187. * @return int
  188. */
  189. int hw_pwm_get_dir(unsigned char no) {
  190. if (pin_maps[no].dir_port != 0) {
  191. return (GPIO_ISTAT(pin_maps[no].dir_port) >> pin_maps[no].dir_pin) & (0x01) == 0x01;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * 设置使能状态
  197. *
  198. * @author lxz
  199. *
  200. * @param no
  201. * @param dir
  202. */
  203. void hw_pwm_set_enable(unsigned char no, int en) {
  204. if (pin_maps[no].en_port != 0) {
  205. if (en) {
  206. GPIO_BOP(pin_maps[no].en_port) = 1 << pin_maps[no].en_pin;
  207. }
  208. else {
  209. GPIO_BOP(pin_maps[no].en_port) = 1 << (pin_maps[no].en_pin +16);
  210. }
  211. }
  212. }
  213. /**
  214. * 获取使能状态
  215. *
  216. * @author lxz
  217. *
  218. * @param no
  219. *
  220. * @return int
  221. */
  222. int hw_pwm_get_enable(unsigned char no) {
  223. if (pin_maps[no].en_port != 0) {
  224. return (GPIO_ISTAT(pin_maps[no].en_port) >> pin_maps[no].en_pin) & (0x01);
  225. }
  226. return 0;
  227. }
  228. /**
  229. * 获取脉冲的频率
  230. *
  231. * @author lxz
  232. *
  233. * @param no
  234. *
  235. * @return int
  236. */
  237. int hw_pwm_get_clk(unsigned char no) {
  238. return hw_pwm_maps[no].unit_in_hz;
  239. }
  240. /**
  241. * 启动pwm
  242. *
  243. * @author lxz (2019/5/20/周一)
  244. *
  245. * @param no
  246. */
  247. void hw_pwm_start(unsigned char no) {
  248. HW_PWM_ON(no);
  249. }
  250. /**
  251. * 停止PWM
  252. *
  253. * @author lxz (2019/5/20/周一)
  254. *
  255. * @param no
  256. */
  257. void hw_pwm_stop(unsigned char no) {
  258. HW_PWM_OFF(no);
  259. }
  260. /**
  261. * 设置PWM的周期
  262. *
  263. * @author lxz
  264. *
  265. * @param no
  266. * @param value
  267. */
  268. void hw_pwm_set_period(unsigned char no, int value) {
  269. HW_PWM_PERIOD(no) = value;
  270. HW_PWM_DUTYCYCLE(no) = value >> 1;
  271. }
  272. /**
  273. * 注册PWM中断的处理事件
  274. *
  275. * @author lxz
  276. *
  277. * @param no
  278. * @param handle
  279. * @param para
  280. */
  281. void hw_pwm_it_register(unsigned char no, pwm_handle handle, void *para) {
  282. if (no < HW_PWM_NUMBER) {
  283. pwm_hanlde_parames[no] = para;
  284. pwm_handle_maps[no] = handle;
  285. }
  286. }
  287. /**
  288. * 设置定时器的基频
  289. *
  290. * @author LXZ (051220)
  291. *
  292. * @param no
  293. * @param clock
  294. */
  295. void hw_pwm_set_clock(unsigned char no, int clock) {
  296. uint32_t tim_base = hw_pwm_maps[no].base;
  297. uint16_t psc = 1;
  298. HW_PWM_OFF(no);
  299. hw_pwm_maps[no].unit_in_hz=clock;
  300. //初始化外设时钟
  301. rcu_periph_clock_enable(rcu_periph_clock_bit(tim_base));
  302. psc = (HW_BOART_CORE_FREQ * 1000000) / (hw_pwm_maps[no].unit_in_hz) - 1;
  303. //初始化定时器
  304. TIMER_PSC(tim_base) = psc; //设置预分频,得到一个基本的时钟单元
  305. TIMER_CAR(tim_base) = 1000; //没有意义
  306. }
  307. /**
  308. * 初始化硬件
  309. */
  310. void hw_pwm_init(int clk) {
  311. //初始化IO
  312. int index = 0;
  313. while (index < HW_PWM_NUMBER) {
  314. //PULSE
  315. uint32_t port = pin_maps[index].pulse_port;
  316. uint32_t pin = 1 << pin_maps[index].pulse_pin;
  317. uint32_t af = pin_maps[index].pulse_af;
  318. if (port != 0) {
  319. //使能时钟
  320. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  321. //配置IO为AF
  322. gpio_mode_set(port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, pin);
  323. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  324. gpio_af_set(port, af, pin);
  325. }
  326. //DIR
  327. port = pin_maps[index].dir_port;
  328. pin = 1 << pin_maps[index].dir_pin;
  329. if (port != 0) {
  330. //使能时钟
  331. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  332. //配置IO为AF
  333. gpio_mode_set(port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, pin);
  334. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  335. hw_pwm_dir_rw_map[index].idr = (uint16_t *)(port + 16);
  336. hw_pwm_dir_rw_map[index].odr = (uint16_t *)(port + 24);
  337. hw_pwm_dir_rw_map[index].pin = (1 << pin);
  338. hw_pwm_dir_rw_map[index].level = pin_maps[index].dir_level;
  339. }
  340. //EN
  341. port = pin_maps[index].en_port;
  342. pin = 1 << pin_maps[index].en_pin;
  343. if (port != 0) {
  344. //使能时钟
  345. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  346. //配置IO为AF
  347. gpio_mode_set(port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, pin);
  348. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  349. hw_pwm_en_rw_map[index].idr = (uint16_t *)(port + 16);
  350. hw_pwm_en_rw_map[index].odr = (uint16_t *)(port + 24);
  351. hw_pwm_en_rw_map[index].pin = (1 << pin);
  352. hw_pwm_dir_rw_map[index].level = pin_maps[index].dir_level;
  353. HW_PWM_SET_EN(index, pin_maps[index].dir_level);
  354. }
  355. index++;
  356. }
  357. //设置默认参数变量
  358. index = 0;
  359. while (index < HW_PWM_NUMBER) {
  360. pwm_handle_maps[index] = hw_pwm_default_handler;
  361. pwm_hanlde_parames[index] = (void *)0;
  362. index++;
  363. }
  364. //初始化硬件
  365. index = 0;
  366. while (index < HW_PWM_NUMBER) {
  367. uint32_t tim_base = hw_pwm_maps[index].base;
  368. uint16_t psc = (clk * 1000000) / (hw_pwm_maps[index].unit_in_hz) - 1;
  369. //初始化外设时钟
  370. rcu_periph_clock_enable(rcu_periph_clock_bit(tim_base));
  371. timer_deinit(tim_base);
  372. //初始化定时器
  373. TIMER_PSC(tim_base) = psc; //设置预分频,得到一个基本的时钟单元
  374. TIMER_CAR(tim_base) = 1000; //没有意义
  375. TIMER_SMCFG(tim_base) = 0;
  376. TIMER_DMAINTEN(tim_base) |= 1 << 0; //使能更新中断
  377. TIMER_CTL0(tim_base) &= ~(3 << 8); //不分频
  378. TIMER_CTL0(tim_base) &= ~(3 << 5); //边沿计数模式
  379. TIMER_CTL0(tim_base) &= ~(1 << 4); //向上计数
  380. TIMER_CTL0(tim_base) |= (1 << 2); //限制只有上溢中断
  381. TIMER_CTL0(tim_base) |= 1 << 7;
  382. TIMER_CCHP(tim_base) |= 1 << 15; //MOE使能输出
  383. TIMER_CTL1(tim_base) = 0;
  384. TIMER_CHCTL2(tim_base) |= (1 << ((hw_pwm_maps[index].ch - 1) * 4)); //使能PWM通道
  385. TIMER_CHCTL2(tim_base) &= ~(1 << ((hw_pwm_maps[index].ch - 1) * 4 + 1)); //高电平有效
  386. if (hw_pwm_maps[index].ch <= 2) {
  387. uint32_t ccmr = TIMER_CHCTL0(tim_base);
  388. uint32_t offset = (hw_pwm_maps[index].ch - 1) * 8;
  389. ccmr &= ~(3 << (offset + 0)); //输出模式
  390. ccmr |= (1 << (offset + 2)); //比较快速使能
  391. ccmr |= (0 << (offset + 3)); //预装载使能禁止
  392. ccmr |= (7 << (offset + 4)); //PWM模式1,
  393. TIMER_CHCTL0(tim_base) = ccmr;
  394. }
  395. else {
  396. uint32_t ccmr = TIMER_CHCTL1(tim_base);
  397. uint32_t offset = (hw_pwm_maps[index].ch - 3) * 8;
  398. ccmr &= ~(3 << (offset + 0)); //输出模式
  399. ccmr |= (1 << (offset + 2)); //比较快速使能
  400. ccmr |= (0 << (offset + 3)); //预装载使能禁止
  401. ccmr |= (7 << (offset + 4)); //PWM模式1,
  402. TIMER_CHCTL1(tim_base) = ccmr;
  403. }
  404. //中断向量使能
  405. hw_pwm_period_registers[index] = (uint32_t *)(tim_base + 0x2C);
  406. hw_pwm_dutycycle_registers[index] = (uint32_t *)(tim_base + 0x34 + (hw_pwm_maps[index].ch - 1) * 4);
  407. hw_pwm_cr_registers[index] = (uint16_t *)(tim_base + 0x00);
  408. hw_pwm_egr_registers[index] = (uint16_t *)(tim_base + 0x10);
  409. HW_PWM_SET_PERIOD(index, 1000);
  410. HW_PWM_ON(index);
  411. hw_delay_us(1);
  412. HW_PWM_OFF(index);
  413. Sys_NVIC_Init(1, 3, hw_pwm_maps[index].irqn, 2);
  414. index++;
  415. }
  416. }
  417. /**
  418. * 关闭所有的脉冲输出
  419. *
  420. * @author lxz (2019/5/30/周四)
  421. */
  422. void hw_pwm_close_all(void) {
  423. int index = 0;
  424. while (index < HW_PWM_NUMBER) {
  425. HW_PWM_OFF(index);
  426. index++;
  427. }
  428. }
  429. /**
  430. * 读取编码器当前值
  431. *
  432. * @author LXZ (042020)
  433. *
  434. * @param index
  435. *
  436. * @return int
  437. */
  438. int hw_encoder_get(int index) {
  439. int cycle = 0;
  440. int value = 0;
  441. uint32_t tim_base = encoders[index].base;
  442. do {
  443. cycle = encoder_cycles[index];
  444. value = TIMER_CNT(tim_base);
  445. } while (cycle != encoder_cycles[index]);
  446. return cycle * encoders[index].period + value;
  447. }
  448. /**
  449. * 设置编码器当前值
  450. *
  451. * @author LXZ (042020)
  452. *
  453. * @param index
  454. * @param count
  455. */
  456. void hw_encoder_set(int index, int count) {
  457. uint32_t tim_base = encoders[index].base;
  458. encoder_cycles[index] = count / encoders[index].period;
  459. TIMER_CNT(tim_base) = count % encoders[index].period;
  460. }
  461. /**
  462. * 编码器接口初始化
  463. *
  464. * @author LXZ (010420)
  465. *
  466. * @param void
  467. */
  468. void hw_encoder_init(void) {
  469. int index = 0;
  470. while (index < HW_ENCODER_NUMBER) {
  471. //A PORT
  472. uint32_t port = encoder_pin_maps[index].aport;
  473. uint16_t pin = encoder_pin_maps[index].apin;
  474. uint16_t af = encoder_pin_maps[index].af;
  475. if (port != 0) {
  476. //使能时钟
  477. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  478. //配置IO为AF
  479. gpio_mode_set(port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, pin);
  480. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  481. gpio_af_set(port, af, pin);
  482. }
  483. //B PORT
  484. port = encoder_pin_maps[index].bport;
  485. pin = encoder_pin_maps[index].bpin;
  486. if (port != 0) {
  487. //使能时钟
  488. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  489. //配置IO为AF
  490. gpio_mode_set(port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, pin);
  491. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  492. gpio_af_set(port, af, pin);
  493. }
  494. index++;
  495. }
  496. index = 0;
  497. while (index < HW_ENCODER_NUMBER) {
  498. uint32_t tim_base = encoders[index].base;
  499. //初始化外设时钟
  500. rcu_periph_clock_enable(rcu_periph_clock_bit(tim_base));
  501. timer_deinit(tim_base);
  502. //初始化定时器
  503. TIMER_PSC(tim_base) = 0; //设置预分频,得到一个基本的时钟单元
  504. TIMER_CAR(tim_base) = encoders[index].period-1; //没有意义
  505. TIMER_SMCFG(tim_base) = 0;
  506. TIMER_DMAINTEN(tim_base) |= 1 << 0; //使能更新中断
  507. TIMER_CTL0(tim_base) &= ~(3 << 8); //不分频
  508. TIMER_CTL0(tim_base) &= ~(3 << 5); //边沿计数模式
  509. TIMER_CTL0(tim_base) &= ~(1 << 4); //向上计数
  510. TIMER_CTL0(tim_base) |= (1 << 2); //限制只有上溢中断
  511. // TIMER_CTL0(tim_base) |= 1 << 7;
  512. // TIMER_CCHP(tim_base) |= 1 << 15; //MOE使能输出
  513. TIMER_CTL1(tim_base) = 0;
  514. TIMER_CHCTL2(tim_base) =0 ;
  515. TIMER_CHCTL0(tim_base) = 0;
  516. TIMER_CHCTL1(tim_base) = 0;
  517. TIMER_SMCFG(tim_base) = 0;
  518. TIMER_CNT(tim_base) = 0;
  519. TIMER_SMCFG(tim_base) = 3<<0;
  520. TIMER_CHCTL0(tim_base) = (1 << 0) | (3 << 4) | (1 << 8) | (3 << 12); //使能T1、T2映射
  521. TIMER_CHCTL2(tim_base) |= (1 << 0) | (1 << 4); //使能捕获通道1、2
  522. TIMER_INTF(tim_base) = 0;
  523. TIMER_CTL0(tim_base)|= 1 << 0; //开启定时器
  524. Sys_NVIC_Init(1, 3, encoders[index].irqn, 2);
  525. index++;
  526. }
  527. }