Encode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * @Author: mikey.zhaopeng
  3. * @Date: 2018-12-20 19:51:02
  4. * @Last Modified by: mikey.zhaopeng
  5. * @Last Modified time: 2018-12-20 19:51:02
  6. */
  7. #include "global.h"
  8. #include "Encode.h"
  9. #define ENCODEPERIOD 65536
  10. //高速输入接口X30X31
  11. #define HIGH_SPEED_PORT D
  12. #define HIGH_SPEED_PIN1 13
  13. #define HIGH_SPEED_PIN2 12
  14. static unsigned char TimerX30,TimerX31,OldTimerX30,OldTimerX31,EXTimerFilter[2];
  15. float BMGearRatio = 1,BM_SEVORGearRatio=1;
  16. //机器硬件资源配置
  17. unsigned char USE_ENCODE;
  18. unsigned char USE_X30_EXTI;
  19. unsigned char USE_X31_EXTI;
  20. unsigned char USE_X20_EXTI;
  21. unsigned char USE_TIMER_X30_EXTI;
  22. unsigned char USE_TIMER_X31_EXTI;
  23. unsigned char USE_EXTEND_INOUTPUT;
  24. unsigned char USE_EXTEND_ALARM;
  25. unsigned long EXTI_TIMER_FREQ;
  26. unsigned char FILTER_P;
  27. unsigned char FILTER_N;
  28. //0配置高速接口上升沿中断,1为下降沿,3为上下沿中断
  29. unsigned char X30EXTI_DIR;
  30. unsigned char X31EXTI_DIR;
  31. unsigned char X20EXTI_DIR;
  32. long axis_position_encode=0;//编码器方式计数
  33. /*
  34. void hw_power_off_init(void) {
  35. RCC->AHBENR |= 1 << (((uint32_t)(POWER_OFF_PORT)-GPIOA_BASE) / 0x400);
  36. GPIO_Set(POWER_OFF_PORT, 1 << POWER_OFF_PIN,
  37. GPIO_MODE_IPU,
  38. GPIO_SPEED_2M,
  39. GPIO_PUPD_NONE); //PB0,PB1设置
  40. }
  41. int hw_power_is_off(void) {
  42. return POWER_IS_OFF();
  43. }
  44. int hw_power_is_on(void) {
  45. return POWER_IS_ON();
  46. }
  47. */
  48. cEncodeAxis_t cEncodeAxis;
  49. void EncoderInit(void)
  50. {
  51. if (USE_ENCODE)
  52. {
  53. RCC->APB2ENR |= 0x20 ;
  54. GPIO_Set(GPIOD, PIN12, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
  55. GPIO_Set(GPIOD, PIN13, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
  56. AFIO->MAPR |=1<<12;
  57. RCC->APB1ENR |= 0x04;
  58. //GPIO_AF_Set(GPIOD, 12, 2);
  59. // GPIO_AF_Set(GPIOD, 13, 2);
  60. Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
  61. TIM4->CR1 = 0;
  62. TIM4->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  63. TIM4->CR2 = 0;
  64. TIM4->PSC = 0; //72
  65. TIM4->ARR = ENCODEPERIOD - 1;
  66. TIM4->CCER = 0;
  67. TIM4->CCMR1 = 0;
  68. TIM4->SMCR = 0;
  69. TIM4->DIER = 0;
  70. TIM4->DIER |= 1; // 允许更新中断
  71. TIM4->CNT = 0;
  72. TIM4->SMCR |=(3 << 0); // 编码器模弿1
  73. TIM4->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
  74. TIM4->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
  75. TIM4->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
  76. TIM4->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
  77. TIM4->CR1 |= 1 << 0;
  78. TIM4->SR = 0;
  79. }
  80. else
  81. {
  82. if (USE_X30_EXTI)
  83. {
  84. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  85. AFIO->EXTICR[3] &= 0xFF0F; // clear used pin
  86. AFIO->EXTICR[3] |= (0x00F0 & 0x0030); // set pin to use PD13
  87. EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
  88. EXTI->EMR |= ((1 << 13) & 0x00000000); // unmask event
  89. switch(X30EXTI_DIR)
  90. {
  91. case 0:
  92. {
  93. EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
  94. EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
  95. }break;
  96. case 1:
  97. {
  98. EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
  99. EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
  100. }break;
  101. default:
  102. {
  103. EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
  104. EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
  105. }
  106. }
  107. if (0x00002000 & (1 << 13)) { // interrupt used
  108. NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
  109. }
  110. Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
  111. }
  112. if (USE_X31_EXTI)
  113. {
  114. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  115. AFIO->EXTICR[3] &= 0xFFF0; // clear used pin
  116. AFIO->EXTICR[3] |= (0x000F & 0x00000003); // set pin to usePD12
  117. EXTI->IMR |= ((1 << 12) & 0x00001000); // unmask interrupt
  118. EXTI->EMR |= ((1 << 12) & 0x00000000); // unmask event
  119. switch(X31EXTI_DIR)
  120. {
  121. case 0:
  122. {
  123. EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
  124. EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
  125. }break;
  126. case 1:
  127. {
  128. EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
  129. EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
  130. }break;
  131. default:
  132. {
  133. EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
  134. EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
  135. }
  136. }
  137. if (0x00001000 & (1 << 12)) { // interrupt used
  138. NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
  139. }
  140. Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
  141. }
  142. if (USE_TIMER_X30_EXTI || USE_TIMER_X31_EXTI)
  143. {
  144. RCC->APB1ENR |= 0x04;
  145. Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
  146. TIM4->CR1 = 0;
  147. TIM4->CR1 |= (0 << 8) | (1 << 7) | (0 << 5) | (1 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  148. TIM4->CR2 = 0;
  149. TIM4->PSC = 0;
  150. TIM4->ARR = 72000000 / EXTI_TIMER_FREQ + 1;
  151. TIM4->CCER = 0;
  152. TIM4->CCMR1 = 0;
  153. TIM4->DIER = 0;
  154. TIM4->DIER |= 1; // 允许更新中断
  155. TIM4->CNT = 72000000 / EXTI_TIMER_FREQ + 1;;
  156. TIM4->CR1 |= 1 << 0;
  157. TIM4->SR = 0;
  158. }
  159. }
  160. if (USE_X20_EXTI)
  161. {
  162. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  163. AFIO->EXTICR[0] &= 0xFFF0; // clear used pin
  164. AFIO->EXTICR[0] |= (0x000F & 0x0003); // set pin to use PD0
  165. EXTI->IMR |= ((1 << 0) & 0x00000001); // unmask interrupt
  166. EXTI->EMR |= ((1 << 0) & 0x00000000); // unmask event
  167. switch(X20EXTI_DIR)
  168. {
  169. case 0:
  170. {
  171. EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
  172. EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
  173. }break;
  174. case 1:
  175. {
  176. EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
  177. EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
  178. }break;
  179. default:
  180. {
  181. EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
  182. EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
  183. }
  184. }
  185. Sys_NVIC_Init(1, 1, EXTI0_IRQn, 1);
  186. }
  187. }
  188. //定时器检测高速输入接口信号
  189. void TIM4_IRQHandler(void)
  190. {
  191. if (USE_ENCODE)
  192. {
  193. if (TIM4->SR & TIM_SR_UIF)
  194. {
  195. if ((TIM4->CR1 & TIM_CR1_DIR))
  196. {
  197. cEncodeAxis.cRealPosi--;
  198. }
  199. else if ((TIM4->CR1 & TIM_CR1_DIR) == 0)
  200. {
  201. cEncodeAxis.cRealPosi++;
  202. }
  203. TIM4->SR &= ~TIM_SR_UIF;
  204. }
  205. }
  206. else
  207. {
  208. if (TIM4->SR & TIM_SR_UIF)
  209. {
  210. if (USE_TIMER_X30_EXTI)
  211. {
  212. OldTimerX30 = TimerX30;
  213. if(hw_io_pin_input(14))
  214. {
  215. if(EXTimerFilter[0] < FILTER_P)
  216. {
  217. EXTimerFilter[0]++;
  218. }
  219. else
  220. TimerX30 = 1;
  221. }
  222. else if(EXTimerFilter[0] > FILTER_N)
  223. {
  224. EXTimerFilter[0]--;
  225. }
  226. else
  227. TimerX30 = 0;
  228. switch(X30EXTI_DIR)
  229. {
  230. case 0:if(TimerX30 && !OldTimerX30)ExtiAcitionX30();break;
  231. case 1:if(!TimerX30 && OldTimerX30)ExtiAcitionX30();break;
  232. default:if((TimerX30 && !OldTimerX30) || (!TimerX30 && OldTimerX30))ExtiAcitionX30();
  233. }
  234. }
  235. if (USE_TIMER_X31_EXTI)
  236. {
  237. OldTimerX31 = TimerX31;
  238. if(hw_io_pin_input(15))
  239. {
  240. if(EXTimerFilter[1] < FILTER_P)
  241. {
  242. EXTimerFilter[1]++;
  243. }
  244. else
  245. TimerX31 = 1;
  246. }
  247. else if(EXTimerFilter[1] > FILTER_N)
  248. {
  249. EXTimerFilter[1]--;
  250. }
  251. else
  252. TimerX31 = 0;
  253. switch(X31EXTI_DIR)
  254. {
  255. case 0:if(TimerX31 && !OldTimerX31)ExtiAcitionX31();break;
  256. case 1:if(!TimerX31 && OldTimerX31)ExtiAcitionX31();break;
  257. default:if((TimerX31 && !OldTimerX31) || (!TimerX31 && TimerX31))ExtiAcitionX31();
  258. }
  259. }
  260. TIM4->SR &= ~TIM_SR_UIF;
  261. }
  262. }
  263. }
  264. long GetEncodePos(void)
  265. {
  266. float pulse_buff;
  267. pulse_buff = (int)TIM4->CNT;
  268. pulse_buff += ENCODEPERIOD * cEncodeAxis.cRealPosi;
  269. //return pulse_buff;
  270. return (long)(pulse_buff/BMGearRatio);
  271. }
  272. void SetEncodePos(long pos)
  273. {
  274. //cEncodeAxis.cRealPosi = pos*BMGearRatio;
  275. cEncodeAxis.cRealPosi=(long)((pos*BMGearRatio)/ENCODEPERIOD);
  276. TIM4->CNT=(int)(pos*BMGearRatio)%ENCODEPERIOD;
  277. }
  278. void EXTI15_10_IRQHandler(void)
  279. {
  280. if(EXTI->PR & (1 << 12))
  281. {
  282. EXTI->PR |= (1 << 12);
  283. if (USE_X31_EXTI)
  284. ExtiAcitionX31();
  285. }
  286. if(EXTI->PR & (1 << 13))
  287. {
  288. EXTI->PR |= (1 << 13);
  289. if (USE_X30_EXTI)
  290. ExtiAcitionX30();
  291. }
  292. }
  293. void EXTI0_IRQHandler(void)
  294. {
  295. if(EXTI->PR & (1 << 0))
  296. {
  297. EXTI->PR |= (1 << 0);
  298. if (USE_X20_EXTI)
  299. ExtiAcitionX20();
  300. }
  301. }
  302. void EXTI30Enable(void)
  303. {
  304. EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
  305. }
  306. void EXTI30Disable(void)
  307. {
  308. EXTI->IMR &= ~((1 << 13) & 0x00002000); // unmask interrupt
  309. }
  310. void EXTI20Enable(void)
  311. {
  312. //EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
  313. EXTI->IMR |= ((1 << 0) & 0x00000001); // unmask interrupt
  314. }
  315. void EXTI20Disable(void)
  316. {
  317. //EXTI->IMR &= ~((1 << 13) & 0x00002000); // unmask interrupt
  318. EXTI->IMR &= ~((1 << 0) & 0x00000001); // unmask interrupt
  319. }