|
關(guān)于STM32的正交編碼找了很久,網(wǎng)上許多錯誤的代碼,花了大概一天時間,弄懂了怎么寫這個程序,發(fā)現(xiàn)其中有許多引腳不可以作為正交的輸入引腳(可能存在引腳復(fù)用之類的情況),做了一些不完全的測試(未測試一些未引出引腳以及JTAG引腳),具體可用引腳為:
定時器: TIM2 TIM5 TIM3
通道1: PA5/PA15 PA0 PA6/PB4
通道2: PA1/PB3 PA1 PA7
測試平臺為STM32F429IGT6,野火最小系統(tǒng)板。具體程序見附件。
單片機源程序如下:
- #include "stm32f4xx.h"
- #include "./led/bsp_led.h"
- #include "./timing/bsp_basic_tim.h"
- #include "./PWMencode/bsp_pwmencode_tim.h"
- #include "./pwm/bsp_pwmoutput_tim.h"
- #include "./oled/bsp_oled.h"
- #include "./usart/bsp_usart.h"
- #include "./adc/bsp_adc.h"
- uint32_t Task_Delay[NumOfTask]={0};
- __IO int16_t ADC_ConvertedValue;
- #define OLED__
- int main(void)
- {
- LED_GPIO_Init();
-
- #ifdef OLED__
- OLED_Init();
- USARTx_Config();
- #else /*OLED__*/
- USARTx_Config();
- #endif /*OLED__*/
-
- Rheostat_Init();
- Timing_Config();
- PWMencode_Config();
- PWM_Output_Init();
- while(1);
- }
復(fù)制代碼- #include "./PWMencode/bsp_PWMencode_tim.h"
- #include "stm32f4xx_tim.h"
- #include "./led/bsp_led.h"
- int circle_count = 0;
- //void ADVANCE_Time_IRQ(void)
- //{
- // GPIO_ToggleBits(LED_R_GPIO_PORT, LED_R_GPIO_PIN);
- // if((GENERAL_TIMx->CR1>>4 & 0x01)==0) //DIR==0
- // circle_count++;
- // else if((GENERAL_TIMx->CR1>>4 & 0x01)==1)//DIR==1
- // circle_count--;
- //}
- short Get_Encode(void)
- {
- short Count;
-
- Count = GENERAL_TIMx->CNT;
- TIM_SetCounter(GENERAL_TIMx, 0);
-
- return Count;
- }
- void GPIO_PWMencode_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(GENERAL_ICPWM1_GPIO_CLK | GENERAL_ICPWM2_GPIO_CLK, ENABLE);//使能引腳的時鐘
-
- /* GPIO初始化 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-
- GPIO_InitStructure.GPIO_Pin = GENERAL_ICPWM1_PIN;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GENERAL_ICPWM2_PIN;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- /* 連接到 GPIO_AF_TIM2*/
- GPIO_PinAFConfig(GENERAL_ICPWM1_GPIO_PORT , GENERAL_ICPWM1_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
- GPIO_PinAFConfig(GENERAL_ICPWM2_GPIO_PORT , GENERAL_ICPWM2_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
- }
- //void NVIC_PWMencode_Config(void)
- //{
- // NVIC_InitTypeDef NVIC_InitStructure;
- // NVIC_InitStructure.NVIC_IRQChannel = GENERAL_TIMx_IRQn;
- // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
- // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- // NVIC_Init(&NVIC_InitStructure);
- //}
- void TIMx_PWMencode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- RCC_GENERAL_TIMx_CONFIG(GENERAL_TIMx_CLK, ENABLE);//使能定時器2的時鐘
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 預(yù)分頻器
- TIM_TimeBaseStructure.TIM_Period = 65535; //設(shè)定計數(shù)器自動重裝值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //選擇時鐘分頻:不分頻
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)
- TIM_TimeBaseInit(GENERAL_TIMx, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(GENERAL_TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用編碼器模式3,上升沿捕捉
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10; //配置輸入濾波器
- TIM_ICInit(GENERAL_TIMx, &TIM_ICInitStructure);
- TIM_SetCounter(GENERAL_TIMx,0);
- // TIM_ClearFlag(GENERAL_TIMx, TIM_IT_Update);
- // TIM_ITConfig(GENERAL_TIMx, TIM_IT_Update, ENABLE);
- TIM_Cmd(GENERAL_TIMx, ENABLE);
-
- }
- void PWMencode_Config(void)
- {
- // NVIC_PWMencode_Config();
- GPIO_PWMencode_Init();
- TIMx_PWMencode_Config();
- }
復(fù)制代碼
所有資料51hei提供下載:
正交編碼.7z
(417.64 KB, 下載次數(shù): 35)
2019-8-3 01:28 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|