123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- /*
- * @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 4
- #define USE_X02_EXTI 1
- #define X02EXTI_DIR 0 //1->上升沿,0->下降沿 (对应外部输入)
- extern void ExtiAcitionX02(void);
- //高速输入接口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;
- /*
- 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();
- }
- */
- typedef struct
- {
- long cRealPosi;
- } cEncodeAxis_t;
- static cEncodeAxis_t cEncodeAxis;
- void EncoderInit(void)
- {
- /*
- if USE_ENCODE
- {
- #if 0
- 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;
- #endif
- }
- 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) & 0x00000000); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 13) & 0x00000000); // 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
- }
- #endif
- #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) & 0x00000000); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR |= ((1 << 12) & 0x00000000); // 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
- }
- #endif
- #if (USE_TIMER_X30_EXTI | USE_TIMER_X31_EXTI)
- #if 0
- 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;
- #endif
- #endif
- }
- */
- #if USE_X02_EXTI //PG2
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
- AFIO->EXTICR[0] &= 0xF0FF; // clear used pin
- AFIO->EXTICR[0] |= (0x0F00 & 0x0600); // set pin to use PD0
- EXTI->IMR |= ((1 << 2) & 0x00000004); // unmask interrupt
- EXTI->EMR &= ~((1 << 2) & 0x00000004); // unmask event
- switch(X02EXTI_DIR)
- {
- case 0:
- {
- EXTI->RTSR |= ((1 << 2) & 0x00000004); // set rising edge
- EXTI->FTSR &= ~((1 << 2) & 0x00000004); // set falling edge
- }break;
- case 1:
- {
- EXTI->RTSR &= ~((1 << 2) & 0x00000004); // set rising edge
- EXTI->FTSR |= ((1 << 2) & 0x00000004); // set falling edge
- }break;
- default:
- {
- EXTI->RTSR |= ((1 << 2) & 0x00000004); // set rising edge
- EXTI->FTSR &= ~((1 << 2) & 0x00000004); // set falling edge
- }
- }
- Sys_NVIC_Init(1, 1, EXTI2_IRQn, 1);
- #endif
- }
- //定时器检测高速输入接口信号
- /*void TIM4_IRQHandler(void)
- {
- if USE_ENCODE
- {
- if (TIM4->SR & TIM_SR_UIF)
- {
- if(X_USE_OLD_DRIVE)
- {
- if ((TIM4->CR1 & TIM_CR1_DIR))
- {
- if(JIN_SHU_DIN_CUN_CONFIG | MXC_NILONG_CONFIG);
- else
- cEncodeAxis.cRealPosi--;
- }
- else if ((TIM4->CR1 & TIM_CR1_DIR)==0)
- {
- cEncodeAxis.cRealPosi++;
- }
- }
- else
- {
- // pwm_handle_maps[HW_PWM_1](pwm_hanlde_parames[HW_PWM_1]);
- }
- TIM4->SR &= ~TIM_SR_UIF;
- }
- }
- else
- {
- if (TIM4->SR & TIM_SR_UIF)
- {
- #if USE_TIMER_X30_EXTI
- OldTimerX30 = TimerX30;
- if(hw_io_pin_input(12))
- {
- 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();
- }
- #endif
- #if USE_TIMER_X31_EXTI
- OldTimerX31 = TimerX31;
- if(hw_io_pin_input(13))
- {
- 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();
- }
- #endif
- TIM4->SR &= ~TIM_SR_UIF;
- }
- }
- }*/
- /*
- long GetEncodePos(void)
- {
- float pulse_buff;
- pulse_buff = cEncodeAxis.cRealPosi;
- return (long)(pulse_buff/BMGearRatio);
- }
- void SetEncodePos(long pos)
- {
- cEncodeAxis.cRealPosi = pos*BMGearRatio;
- }
- void EXTI15_10_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 12))
- {
- EXTI->PR |= (1 << 12);
- #if USE_X31_EXTI
- ExtiAcitionX31();
- #endif
- }
- if(EXTI->PR & (1 << 13))
- {
- EXTI->PR |= (1 << 13);
- #if USE_X30_EXTI
- ExtiAcitionX30();
- #endif
- }
- }
- void EXTI0_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 0))
- {
- EXTI->PR |= (1 << 0);
- #if USE_X20_EXTI
- ExtiAcitionX20();
- #endif
- }
- }
- */
- void EXTI2_IRQHandler(void)
- {
- if(EXTI->PR & (1 << 2))
- {
- EXTI->PR |= (1 << 2);
- #if USE_X02_EXTI
- ExtiAcitionX02();
- // YXExtiAcitionX02();
- #endif
- }
- }
|