Encode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. //机器硬件资源配置
  10. unsigned char USE_X20_EXTI,X20EXTI_DIR;
  11. unsigned char FILTER_P;
  12. unsigned char FILTER_N;
  13. #define ENCODEPERIOD 65536
  14. typedef struct
  15. {
  16. unsigned short encode1_use;
  17. unsigned short encode2_use;
  18. float BMGearRatio_ch1;
  19. float BMGearRatio_ch2;
  20. long cRealPosi_ch1;
  21. long cRealPosi_ch2;
  22. } cEncodeAxis_t;
  23. static cEncodeAxis_t cEncodeAxis;
  24. void EncoderInit(void)
  25. {
  26. if (cEncodeAxis.encode1_use)//编码器1
  27. {
  28. RCC->APB2ENR |= 0x10 ;
  29. GPIO_Set(GPIOA, PIN15, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
  30. GPIO_Set(GPIOB, PIN13, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
  31. AFIO->MAPR |=3<<8;//用TIME2 完全映射
  32. RCC->APB1ENR |=1<<0;//开启TIME3定时器时钟
  33. Sys_NVIC_Init(1, 1, TIM2_IRQn, 1);
  34. TIM2->CR1 = 0;
  35. TIM2->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  36. TIM2->CR2 = 0;
  37. TIM2->PSC = 0; //72
  38. TIM2->ARR = ENCODEPERIOD - 1;
  39. TIM2->CCER = 0;
  40. TIM2->CCMR1 = 0;
  41. TIM2->SMCR = 0;
  42. TIM2->DIER = 0;
  43. TIM2->DIER |= 1; // 允许更新中断
  44. TIM2->CNT = 0;
  45. TIM2->SMCR |=(3 << 0); // 编码器模弿1
  46. TIM2->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
  47. TIM2->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
  48. TIM2->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
  49. TIM2->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
  50. TIM2->CR1 |= 1 << 0;
  51. TIM2->SR = 0;
  52. }
  53. if (cEncodeAxis.encode2_use)//编码器2
  54. {
  55. RCC->APB2ENR |= 0x10 ;
  56. GPIO_Set(GPIOC, PIN6, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
  57. GPIO_Set(GPIOC, PIN7, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
  58. AFIO->MAPR |=3<<10;//用TIME3 完全映射
  59. RCC->APB1ENR |=1<<1;//开启TIME3定时器时钟
  60. Sys_NVIC_Init(1, 1, TIM3_IRQn, 1);
  61. TIM3->CR1 = 0;
  62. TIM3->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  63. TIM3->CR2 = 0;
  64. TIM3->PSC = 0; //72
  65. TIM3->ARR = ENCODEPERIOD - 1;
  66. TIM3->CCER = 0;
  67. TIM3->CCMR1 = 0;
  68. TIM3->SMCR = 0;
  69. TIM3->DIER = 0;
  70. TIM3->DIER |= 1; // 允许更新中断
  71. TIM3->CNT = 0;
  72. TIM3->SMCR |=(3 << 0); // 编码器模弿1
  73. TIM3->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
  74. TIM3->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
  75. TIM3->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
  76. TIM3->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
  77. TIM3->CR1 |= 1 << 0;
  78. TIM3->SR = 0;
  79. }
  80. if(USE_X20_EXTI)
  81. {
  82. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN ; // enable clock for Alternate Function
  83. AFIO->EXTICR[3] &= 0x0FFF; // clear used pin
  84. AFIO->EXTICR[3] |= (0xF000 & 0x0000); // set pin to use PA15
  85. EXTI->IMR |= ((1 << 15) & 0x00008000); // unmask interrupt
  86. EXTI->EMR |= ((1 << 15) & 0x00000000); // unmask event
  87. switch(X20EXTI_DIR)
  88. {
  89. case 0:
  90. {
  91. EXTI->RTSR |= ((1 << 15) & 0x00008000); // set rising edge
  92. EXTI->FTSR |= ((1 << 15) & 0x00000000); // set falling edge
  93. }break;
  94. case 1:
  95. {
  96. EXTI->RTSR |= ((1 << 15) & 0x00000000); // set rising edge
  97. EXTI->FTSR |= ((1 << 15) & 0x00008000); // set falling edge
  98. }break;
  99. default:
  100. {
  101. EXTI->RTSR |= ((1 << 15) & 0x00008000); // set rising edge
  102. EXTI->FTSR |= ((1 << 15) & 0x00008000); // set falling edge
  103. }
  104. }
  105. // if (0x00008000 & (1 << 15)) { // interrupt used
  106. // NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
  107. // }
  108. Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
  109. }
  110. }
  111. //外部中断
  112. #if JIN_HONG_MACHINE == 1
  113. void EXTI15_10_IRQHandler(void)
  114. {
  115. if(EXTI->PR & (1 << 15))
  116. {
  117. EXTI->PR |= (1 << 15);
  118. if (USE_X20_EXTI)
  119. {
  120. //MXC_ExtiAcitionX20();
  121. }
  122. }
  123. }
  124. //外部信号,用轴脉冲滤波方式进行定位
  125. void EXIO_AXIS_DW_Filter(unsigned short axis)
  126. {
  127. switch(axis)
  128. {
  129. case X_AXIS:
  130. MXC_SBCHECK_PROC();//免修的色标定位滤波
  131. break;
  132. case Y_AXIS:
  133. break;
  134. case Z_AXIS:
  135. break;
  136. }
  137. }
  138. #endif
  139. //定时器检测高速输入接口信号
  140. void TIM2_IRQHandler(void)
  141. {
  142. if (cEncodeAxis.encode1_use)
  143. {
  144. if (TIM2->SR & TIM_SR_UIF)
  145. {
  146. if ((TIM2->CR1 & TIM_CR1_DIR))
  147. {
  148. cEncodeAxis.cRealPosi_ch1--;
  149. }
  150. else if ((TIM2->CR1 & TIM_CR1_DIR)==0)
  151. {
  152. cEncodeAxis.cRealPosi_ch1++;
  153. }
  154. TIM2->SR &= ~TIM_SR_UIF;
  155. }
  156. }
  157. // else if(USE_X20_EXTI)
  158. // {
  159. // if (TIM2->SR & TIM_SR_UIF)
  160. // {
  161. // MXC_ExtiAcitionX20();
  162. // }
  163. // TIM2->SR &= ~TIM_SR_UIF;
  164. // }
  165. }
  166. //定时器检测高速输入接口信号
  167. void TIM3_IRQHandler(void)
  168. {
  169. if (TIM3->SR & TIM_SR_UIF)
  170. {
  171. if ((TIM3->CR1 & TIM_CR1_DIR))
  172. {
  173. cEncodeAxis.cRealPosi_ch2--;
  174. }
  175. else if ((TIM3->CR1 & TIM_CR1_DIR)==0)
  176. {
  177. cEncodeAxis.cRealPosi_ch2++;
  178. }
  179. TIM3->SR &= ~TIM_SR_UIF;
  180. }
  181. }
  182. long GetEncodeNum(int ch)
  183. {
  184. long encode_num;
  185. switch(ch)
  186. {
  187. case ENCODE_X20X21:
  188. encode_num = (int)TIM2->CNT;
  189. encode_num += ENCODEPERIOD * cEncodeAxis.cRealPosi_ch1;
  190. return encode_num;
  191. break;
  192. case ENCODE_X22X23:
  193. encode_num = (int)TIM3->CNT;
  194. encode_num += ENCODEPERIOD * cEncodeAxis.cRealPosi_ch2;
  195. return encode_num;
  196. break;
  197. }
  198. return 0;
  199. }
  200. long GetEncodePos(int ch)
  201. {
  202. float pulse_buff;
  203. switch(ch)
  204. {
  205. case ENCODE_X20X21:
  206. if(cEncodeAxis.BMGearRatio_ch1==0)cEncodeAxis.BMGearRatio_ch1=1;
  207. pulse_buff=(float)GetEncodeNum(ENCODE_X20X21);
  208. return (long)(pulse_buff/cEncodeAxis.BMGearRatio_ch1);
  209. break;
  210. case ENCODE_X22X23:
  211. if(cEncodeAxis.BMGearRatio_ch2==0)cEncodeAxis.BMGearRatio_ch2=1;
  212. pulse_buff=(float)GetEncodeNum(ENCODE_X22X23);
  213. return (long)(pulse_buff/cEncodeAxis.BMGearRatio_ch2);
  214. break;
  215. }
  216. return (0);
  217. }
  218. void SetEncodePos(int ch,long pos)
  219. {
  220. switch(ch)
  221. {
  222. case ENCODE_X20X21:
  223. cEncodeAxis.cRealPosi_ch1=(long)((pos*cEncodeAxis.BMGearRatio_ch1)/ENCODEPERIOD);
  224. TIM2->CNT=(int)(pos*cEncodeAxis.BMGearRatio_ch1)%ENCODEPERIOD;
  225. break;
  226. case ENCODE_X22X23:
  227. cEncodeAxis.cRealPosi_ch2=(long)((pos*cEncodeAxis.BMGearRatio_ch2)/ENCODEPERIOD);
  228. TIM3->CNT=(int)(pos*cEncodeAxis.BMGearRatio_ch2)%ENCODEPERIOD;
  229. break;
  230. }
  231. }
  232. //设置编码器的比例
  233. void SetEncodeGearRatio(int ch,float gearRatio)
  234. {
  235. switch(ch)
  236. {
  237. case ENCODE_X20X21:
  238. if(gearRatio==0)gearRatio=1;
  239. cEncodeAxis.BMGearRatio_ch1=gearRatio;
  240. break;
  241. case ENCODE_X22X23:
  242. if(gearRatio==0)gearRatio=1;
  243. cEncodeAxis.BMGearRatio_ch2=gearRatio;
  244. break;
  245. }
  246. }
  247. //编码器应用设置
  248. void SetEncode_enable(int ch)
  249. {
  250. switch(ch)
  251. {
  252. case ENCODE_X20X21:
  253. cEncodeAxis.encode1_use=1;
  254. break;
  255. case ENCODE_X22X23:
  256. cEncodeAxis.encode2_use=1;
  257. break;
  258. }
  259. }