標(biāo)題:
STM32F429IGT6正交編碼器輸入源程序
[打印本頁(yè)]
作者:
ms53zs
時(shí)間:
2019-8-3 00:19
標(biāo)題:
STM32F429IGT6正交編碼器輸入源程序
關(guān)于STM32的正交編碼找了很久,網(wǎng)上許多錯(cuò)誤的代碼,花了大概一天時(shí)間,弄懂了怎么寫這個(gè)程序,發(fā)現(xiàn)其中有許多引腳不可以作為正交的輸入引腳(可能存在引腳復(fù)用之類的情況),做了一些不完全的測(cè)試(未測(cè)試一些未引出引腳以及JTAG引腳),具體可用引腳為:
定時(shí)器: TIM2 TIM5 TIM3
通道1: PA5/PA15 PA0 PA6/PB4
通道2: PA1/PB3 PA1 PA7
測(cè)試平臺(tái)為STM32F429IGT6,野火最小系統(tǒng)板。具體程序見(jiàn)附件。
單片機(jī)源程序如下:
#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);//使能引腳的時(shí)鐘
/* 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);//使能定時(shí)器2的時(shí)鐘
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 預(yù)分頻器
TIM_TimeBaseStructure.TIM_Period = 65535; //設(shè)定計(jì)數(shù)器自動(dòng)重裝值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //選擇時(shí)鐘分頻:不分頻
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計(jì)數(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 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
ferry21
時(shí)間:
2019-12-4 09:43
請(qǐng)教一下樓主 怎么計(jì)數(shù)編碼器圈數(shù)
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1