123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- /*
- * @Author: mikey.zhaopeng
- * @Date: 2018-12-20 19:51:02
- * @Last Modified by: mikey.zhaopeng
- * @Last Modified time: 2018-12-20 19:51:02
- */
- #include "global.h"
- #include "Encode.h"
- //机器硬件资源配置
- unsigned char USE_X20_EXTI,X20EXTI_DIR;
- unsigned char FILTER_P;
- unsigned char FILTER_N;
- #define ENCODEPERIOD 65536
-
- typedef struct
- {
- unsigned short encode1_use;
- unsigned short encode2_use;
- float BMGearRatio_ch1;
- float BMGearRatio_ch2;
- long cRealPosi_ch1;
- long cRealPosi_ch2;
- } cEncodeAxis_t;
- static cEncodeAxis_t cEncodeAxis;
- void EncoderInit(void)
- {
- if (cEncodeAxis.encode1_use)//编码器1
- {
- RCC->APB2ENR |= 0x10 ;
- GPIO_Set(GPIOA, PIN15, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
- GPIO_Set(GPIOB, PIN13, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
- AFIO->MAPR |=3<<8;//用TIME2 完全映射
- RCC->APB1ENR |=1<<0;//开启TIME3定时器时钟
- Sys_NVIC_Init(1, 1, TIM2_IRQn, 1);
- TIM2->CR1 = 0;
- TIM2->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
- TIM2->CR2 = 0;
- TIM2->PSC = 0; //72
- TIM2->ARR = ENCODEPERIOD - 1;
- TIM2->CCER = 0;
- TIM2->CCMR1 = 0;
- TIM2->SMCR = 0;
- TIM2->DIER = 0;
- TIM2->DIER |= 1; // 允许更新中断
- TIM2->CNT = 0;
- TIM2->SMCR |=(3 << 0); // 编码器模弿1
- TIM2->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
- TIM2->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
- TIM2->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
- TIM2->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
- TIM2->CR1 |= 1 << 0;
- TIM2->SR = 0;
- }
- if (cEncodeAxis.encode2_use)//编码器2
- {
- RCC->APB2ENR |= 0x10 ;
- GPIO_Set(GPIOC, PIN6, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
- GPIO_Set(GPIOC, PIN7, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
- AFIO->MAPR |=3<<10;//用TIME3 完全映射
- RCC->APB1ENR |=1<<1;//开启TIME3定时器时钟
- Sys_NVIC_Init(1, 1, TIM3_IRQn, 1);
- TIM3->CR1 = 0;
- TIM3->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
- TIM3->CR2 = 0;
- TIM3->PSC = 0; //72
- TIM3->ARR = ENCODEPERIOD - 1;
- TIM3->CCER = 0;
- TIM3->CCMR1 = 0;
- TIM3->SMCR = 0;
- TIM3->DIER = 0;
- TIM3->DIER |= 1; // 允许更新中断
- TIM3->CNT = 0;
- TIM3->SMCR |=(3 << 0); // 编码器模弿1
- TIM3->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
- TIM3->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
- TIM3->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
- TIM3->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
- TIM3->CR1 |= 1 << 0;
- TIM3->SR = 0;
- }
- if(USE_X20_EXTI)
- {
- RCC->APB1ENR |= RCC_APB1ENR_TIM2EN ; // enable clock for Alternate Function
- AFIO->EXTICR[3] &= 0x0FFF; // clear used pin
- AFIO->EXTICR[3] |= (0xF000 & 0x0000); // set pin to use PA15
-
- EXTI->IMR |= ((1 << 15) & 0x00008000); // unmask interrupt
- EXTI->EMR |= ((1 << 15) & 0x00000000); // unmask event
-
- switch(X20EXTI_DIR)
- {
- case 0:
- {
- EXTI->RTSR |= ((1 << 15) & 0x00008000); // set rising edge
- EXTI->FTSR |= ((1 << 15) & 0x00000000); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 15) & 0x00000000); // set rising edge
- EXTI->FTSR |= ((1 << 15) & 0x00008000); // set falling edge
- }break;
- default:
- {
- EXTI->RTSR |= ((1 << 15) & 0x00008000); // set rising edge
- EXTI->FTSR |= ((1 << 15) & 0x00008000); // set falling edge
- }
- }
- // if (0x00008000 & (1 << 15)) { // interrupt used
- // NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
- // }
- Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
- }
- }
- //外部中断
- #if JIN_HONG_MACHINE == 1
- void EXTI15_10_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 15))
- {
- EXTI->PR |= (1 << 15);
- if (USE_X20_EXTI)
- {
- //MXC_ExtiAcitionX20();
- }
- }
- }
- //外部信号,用轴脉冲滤波方式进行定位
- void EXIO_AXIS_DW_Filter(unsigned short axis)
- {
- switch(axis)
- {
- case X_AXIS:
- MXC_SBCHECK_PROC();//免修的色标定位滤波
- break;
- case Y_AXIS:
- break;
- case Z_AXIS:
- break;
- }
- }
- #endif
- //定时器检测高速输入接口信号
- void TIM2_IRQHandler(void)
- {
- if (cEncodeAxis.encode1_use)
- {
- if (TIM2->SR & TIM_SR_UIF)
- {
- if ((TIM2->CR1 & TIM_CR1_DIR))
- {
- cEncodeAxis.cRealPosi_ch1--;
- }
- else if ((TIM2->CR1 & TIM_CR1_DIR)==0)
- {
- cEncodeAxis.cRealPosi_ch1++;
- }
- TIM2->SR &= ~TIM_SR_UIF;
- }
- }
- // else if(USE_X20_EXTI)
- // {
- // if (TIM2->SR & TIM_SR_UIF)
- // {
- // MXC_ExtiAcitionX20();
- // }
- // TIM2->SR &= ~TIM_SR_UIF;
- // }
- }
- //定时器检测高速输入接口信号
- void TIM3_IRQHandler(void)
- {
- if (TIM3->SR & TIM_SR_UIF)
- {
- if ((TIM3->CR1 & TIM_CR1_DIR))
- {
- cEncodeAxis.cRealPosi_ch2--;
- }
- else if ((TIM3->CR1 & TIM_CR1_DIR)==0)
- {
- cEncodeAxis.cRealPosi_ch2++;
- }
- TIM3->SR &= ~TIM_SR_UIF;
- }
- }
- long GetEncodeNum(int ch)
- {
- long encode_num;
- switch(ch)
- {
- case ENCODE_X20X21:
- encode_num = (int)TIM2->CNT;
- encode_num += ENCODEPERIOD * cEncodeAxis.cRealPosi_ch1;
- return encode_num;
- break;
- case ENCODE_X22X23:
- encode_num = (int)TIM3->CNT;
- encode_num += ENCODEPERIOD * cEncodeAxis.cRealPosi_ch2;
- return encode_num;
- break;
- }
- return 0;
- }
- long GetEncodePos(int ch)
- {
- float pulse_buff;
- switch(ch)
- {
- case ENCODE_X20X21:
- if(cEncodeAxis.BMGearRatio_ch1==0)cEncodeAxis.BMGearRatio_ch1=1;
- pulse_buff=(float)GetEncodeNum(ENCODE_X20X21);
- return (long)(pulse_buff/cEncodeAxis.BMGearRatio_ch1);
- break;
- case ENCODE_X22X23:
- if(cEncodeAxis.BMGearRatio_ch2==0)cEncodeAxis.BMGearRatio_ch2=1;
- pulse_buff=(float)GetEncodeNum(ENCODE_X22X23);
- return (long)(pulse_buff/cEncodeAxis.BMGearRatio_ch2);
- break;
- }
- return (0);
- }
- void SetEncodePos(int ch,long pos)
- {
- switch(ch)
- {
- case ENCODE_X20X21:
- cEncodeAxis.cRealPosi_ch1=(long)((pos*cEncodeAxis.BMGearRatio_ch1)/ENCODEPERIOD);
- TIM2->CNT=(int)(pos*cEncodeAxis.BMGearRatio_ch1)%ENCODEPERIOD;
- break;
- case ENCODE_X22X23:
- cEncodeAxis.cRealPosi_ch2=(long)((pos*cEncodeAxis.BMGearRatio_ch2)/ENCODEPERIOD);
- TIM3->CNT=(int)(pos*cEncodeAxis.BMGearRatio_ch2)%ENCODEPERIOD;
- break;
- }
- }
- //设置编码器的比例
- void SetEncodeGearRatio(int ch,float gearRatio)
- {
- switch(ch)
- {
- case ENCODE_X20X21:
- if(gearRatio==0)gearRatio=1;
- cEncodeAxis.BMGearRatio_ch1=gearRatio;
- break;
- case ENCODE_X22X23:
- if(gearRatio==0)gearRatio=1;
- cEncodeAxis.BMGearRatio_ch2=gearRatio;
- break;
- }
- }
- //编码器应用设置
- void SetEncode_enable(int ch)
- {
- switch(ch)
- {
- case ENCODE_X20X21:
- cEncodeAxis.encode1_use=1;
- break;
- case ENCODE_X22X23:
- cEncodeAxis.encode2_use=1;
- break;
- }
- }
|