123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /*
- * @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"
- #define ENCODEPERIOD 65536
- //高速输入接口X30X31
- #define HIGH_SPEED_PORT D
- #define HIGH_SPEED_PIN1 13
- #define HIGH_SPEED_PIN2 12
- static unsigned char TimerX30,TimerX31,OldTimerX30,OldTimerX31,EXTimerFilter[2];
- float BMGearRatio = 1,BM_SEVORGearRatio=1;
- //机器硬件资源配置
- unsigned char USE_ENCODE;
- unsigned char USE_X30_EXTI;
- unsigned char USE_X31_EXTI;
- unsigned char USE_X20_EXTI;
- unsigned char USE_TIMER_X30_EXTI;
- unsigned char USE_TIMER_X31_EXTI;
- unsigned char USE_EXTEND_INOUTPUT;
- unsigned char USE_EXTEND_ALARM;
- unsigned long EXTI_TIMER_FREQ;
- unsigned char FILTER_P;
- unsigned char FILTER_N;
- //0配置高速接口上升沿中断,1为下降沿,3为上下沿中断
- unsigned char X30EXTI_DIR;
- unsigned char X31EXTI_DIR;
- unsigned char X20EXTI_DIR;
- long axis_position_encode=0;//编码器方式计数
- /*
- void hw_power_off_init(void) {
- RCC->AHBENR |= 1 << (((uint32_t)(POWER_OFF_PORT)-GPIOA_BASE) / 0x400);
- GPIO_Set(POWER_OFF_PORT, 1 << POWER_OFF_PIN,
- GPIO_MODE_IPU,
- GPIO_SPEED_2M,
- GPIO_PUPD_NONE); //PB0,PB1设置
- }
- int hw_power_is_off(void) {
- return POWER_IS_OFF();
- }
- int hw_power_is_on(void) {
- return POWER_IS_ON();
- }
- */
- cEncodeAxis_t cEncodeAxis;
- void EncoderInit(void)
- {
- if (USE_ENCODE)
- {
- RCC->APB2ENR |= 0x20 ;
- GPIO_Set(GPIOD, PIN12, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
- GPIO_Set(GPIOD, PIN13, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
- AFIO->MAPR |=1<<12;
- RCC->APB1ENR |= 0x04;
- //GPIO_AF_Set(GPIOD, 12, 2);
- // GPIO_AF_Set(GPIOD, 13, 2);
- Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
- TIM4->CR1 = 0;
- TIM4->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
- TIM4->CR2 = 0;
- TIM4->PSC = 0; //72
- TIM4->ARR = ENCODEPERIOD - 1;
- TIM4->CCER = 0;
- TIM4->CCMR1 = 0;
- TIM4->SMCR = 0;
- TIM4->DIER = 0;
- TIM4->DIER |= 1; // 允许更新中断
- TIM4->CNT = 0;
- TIM4->SMCR |=(3 << 0); // 编码器模弿1
- TIM4->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
- TIM4->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
- TIM4->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
- TIM4->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
- TIM4->CR1 |= 1 << 0;
- TIM4->SR = 0;
- }
- else
- {
- if (USE_X30_EXTI)
- {
-
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
- AFIO->EXTICR[3] &= 0xFF0F; // clear used pin
- AFIO->EXTICR[3] |= (0x00F0 & 0x0030); // set pin to use PD13
- EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
- EXTI->EMR |= ((1 << 13) & 0x00000000); // unmask event
-
- switch(X30EXTI_DIR)
- {
- case 0:
- {
- EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
- EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
- EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
- }break;
- default:
- {
- EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
- EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
- }
- }
- if (0x00002000 & (1 << 13)) { // interrupt used
- NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
- }
- Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
- }
-
- if (USE_X31_EXTI)
- {
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
- AFIO->EXTICR[3] &= 0xFFF0; // clear used pin
- AFIO->EXTICR[3] |= (0x000F & 0x00000003); // set pin to usePD12
- EXTI->IMR |= ((1 << 12) & 0x00001000); // unmask interrupt
- EXTI->EMR |= ((1 << 12) & 0x00000000); // unmask event
-
- switch(X31EXTI_DIR)
- {
- case 0:
- {
- EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
- EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
- EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
- }break;
- default:
- {
- EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
- EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
- }
- }
-
- if (0x00001000 & (1 << 12)) { // interrupt used
- NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
- }
- Sys_NVIC_Init(0, 0, EXTI15_10_IRQn, 1);
- }
-
- if (USE_TIMER_X30_EXTI || USE_TIMER_X31_EXTI)
- {
- RCC->APB1ENR |= 0x04;
- Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
- TIM4->CR1 = 0;
- TIM4->CR1 |= (0 << 8) | (1 << 7) | (0 << 5) | (1 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
- TIM4->CR2 = 0;
- TIM4->PSC = 0;
- TIM4->ARR = 72000000 / EXTI_TIMER_FREQ + 1;
- TIM4->CCER = 0;
- TIM4->CCMR1 = 0;
- TIM4->DIER = 0;
- TIM4->DIER |= 1; // 允许更新中断
- TIM4->CNT = 72000000 / EXTI_TIMER_FREQ + 1;;
-
- TIM4->CR1 |= 1 << 0;
- TIM4->SR = 0;
- }
- }
-
- if (USE_X20_EXTI)
- {
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
- AFIO->EXTICR[0] &= 0xFFF0; // clear used pin
- AFIO->EXTICR[0] |= (0x000F & 0x0003); // set pin to use PD0
- EXTI->IMR |= ((1 << 0) & 0x00000001); // unmask interrupt
- EXTI->EMR |= ((1 << 0) & 0x00000000); // unmask event
-
- switch(X20EXTI_DIR)
- {
- case 0:
- {
- EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
- EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
- EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
- }break;
- default:
- {
- EXTI->RTSR |= ((1 << 0) & 0x00000001); // set rising edge
- EXTI->FTSR |= ((1 << 0) & 0x00000001); // set falling edge
- }
- }
- Sys_NVIC_Init(1, 1, EXTI0_IRQn, 1);
- }
- }
- //定时器检测高速输入接口信号
- void TIM4_IRQHandler(void)
- {
- if (USE_ENCODE)
- {
- if (TIM4->SR & TIM_SR_UIF)
- {
- if ((TIM4->CR1 & TIM_CR1_DIR))
- {
- cEncodeAxis.cRealPosi--;
- }
- else if ((TIM4->CR1 & TIM_CR1_DIR) == 0)
- {
- cEncodeAxis.cRealPosi++;
- }
- TIM4->SR &= ~TIM_SR_UIF;
- }
- }
- else
- {
- if (TIM4->SR & TIM_SR_UIF)
- {
- if (USE_TIMER_X30_EXTI)
- {
- OldTimerX30 = TimerX30;
- if(hw_io_pin_input(14))
- {
- if(EXTimerFilter[0] < FILTER_P)
- {
- EXTimerFilter[0]++;
- }
- else
- TimerX30 = 1;
- }
- else if(EXTimerFilter[0] > FILTER_N)
- {
- EXTimerFilter[0]--;
- }
- else
- TimerX30 = 0;
-
- switch(X30EXTI_DIR)
- {
- case 0:if(TimerX30 && !OldTimerX30)ExtiAcitionX30();break;
- case 1:if(!TimerX30 && OldTimerX30)ExtiAcitionX30();break;
- default:if((TimerX30 && !OldTimerX30) || (!TimerX30 && OldTimerX30))ExtiAcitionX30();
- }
- }
- if (USE_TIMER_X31_EXTI)
- {
- OldTimerX31 = TimerX31;
-
- if(hw_io_pin_input(15))
- {
- if(EXTimerFilter[1] < FILTER_P)
- {
- EXTimerFilter[1]++;
- }
- else
- TimerX31 = 1;
- }
- else if(EXTimerFilter[1] > FILTER_N)
- {
- EXTimerFilter[1]--;
- }
- else
- TimerX31 = 0;
-
- switch(X31EXTI_DIR)
- {
- case 0:if(TimerX31 && !OldTimerX31)ExtiAcitionX31();break;
- case 1:if(!TimerX31 && OldTimerX31)ExtiAcitionX31();break;
- default:if((TimerX31 && !OldTimerX31) || (!TimerX31 && TimerX31))ExtiAcitionX31();
- }
- }
-
- TIM4->SR &= ~TIM_SR_UIF;
- }
- }
- }
- long GetEncodePos(void)
- {
- float pulse_buff;
- pulse_buff = (int)TIM4->CNT;
- pulse_buff += ENCODEPERIOD * cEncodeAxis.cRealPosi;
- //return pulse_buff;
- return (long)(pulse_buff/BMGearRatio);
- }
- void SetEncodePos(long pos)
- {
- //cEncodeAxis.cRealPosi = pos*BMGearRatio;
- cEncodeAxis.cRealPosi=(long)((pos*BMGearRatio)/ENCODEPERIOD);
- TIM4->CNT=(int)(pos*BMGearRatio)%ENCODEPERIOD;
- }
- void EXTI15_10_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 12))
- {
- EXTI->PR |= (1 << 12);
-
- if (USE_X31_EXTI)
- ExtiAcitionX31();
- }
-
- if(EXTI->PR & (1 << 13))
- {
- EXTI->PR |= (1 << 13);
- if (USE_X30_EXTI)
- ExtiAcitionX30();
- }
- }
- void EXTI0_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 0))
- {
- EXTI->PR |= (1 << 0);
- if (USE_X20_EXTI)
- ExtiAcitionX20();
- }
- }
- void EXTI30Enable(void)
- {
- EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
- }
- void EXTI30Disable(void)
- {
- EXTI->IMR &= ~((1 << 13) & 0x00002000); // unmask interrupt
- }
- void EXTI20Enable(void)
- {
- //EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
- EXTI->IMR |= ((1 << 0) & 0x00000001); // unmask interrupt
- }
- void EXTI20Disable(void)
- {
- //EXTI->IMR &= ~((1 << 13) & 0x00002000); // unmask interrupt
- EXTI->IMR &= ~((1 << 0) & 0x00000001); // unmask interrupt
- }
|