|
庫(kù)函數(shù)版編碼器測(cè)試程序(編碼器5V供電。AB相分別接PA0 PA1 串口1以9600波特率輸出速度)
單片機(jī)源程序如下:
- #include "encoder.h"
- #include "stm32f10x_gpio.h"
- /**************************************************************************
- 作者:平衡小車之家
- 我的淘寶小店:http://shop114407458.taobao.com/
- **************************************************************************/
- /**************************************************************************
- 函數(shù)功能:把TIM2初始化為編碼器接口模式
- 入口參數(shù):無(wú)
- 返回 值:無(wú)
- **************************************************************************/
- void Encoder_Init_TIM2(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能定時(shí)器4的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PB端口時(shí)鐘
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化GPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 預(yù)分頻器
- TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //設(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(TIM2, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用編碼器模式3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM2, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清除TIM的更新標(biāo)志位
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM_SetCounter(TIM2,0);
- TIM_Cmd(TIM2, ENABLE);
- }
- /**************************************************************************
- 函數(shù)功能:把TIM4初始化為編碼器接口模式
- 入口參數(shù):無(wú)
- 返回 值:無(wú)
- **************************************************************************/
- void Encoder_Init_TIM4(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能定時(shí)器4的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能PB端口時(shí)鐘
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化GPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 預(yù)分頻器
- TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //設(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(TIM4, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用編碼器模式3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM4, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM的更新標(biāo)志位
- TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM_SetCounter(TIM4,0);
- TIM_Cmd(TIM4, ENABLE);
- }
- /**************************************************************************
- 函數(shù)功能:?jiǎn)挝粫r(shí)間讀取編碼器計(jì)數(shù)
- 入口參數(shù):定時(shí)器
- 返回 值:速度值
- **************************************************************************/
- int Read_Encoder(u8 TIMX)
- {
- int Encoder_TIM;
- switch(TIMX)
- {
- case 2: Encoder_TIM= (short)TIM2 -> CNT; TIM2 -> CNT=0;break;
- case 3: Encoder_TIM= (short)TIM3 -> CNT; TIM3 -> CNT=0;break;
- case 4: Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0;break;
- default: Encoder_TIM=0;
- }
- return Encoder_TIM;
- }
- /**************************************************************************
- 函數(shù)功能:TIM4中斷服務(wù)函數(shù)
- 入口參數(shù):無(wú)
- 返回 值:無(wú)
- **************************************************************************/
- void TIM4_IRQHandler(void)
- {
- if(TIM4->SR&0X0001)//溢出中斷
- {
- }
- TIM4->SR&=~(1<<0);//清除中斷標(biāo)志位
- }
- /**************************************************************************
- 函數(shù)功能:TIM2中斷服務(wù)函數(shù)
- 入口參數(shù):無(wú)
- 返回 值:無(wú)
- **************************************************************************/
- void TIM2_IRQHandler(void)
- {
- if(TIM2->SR&0X0001)//溢出中斷
- {
- }
- TIM2->SR&=~(1<<0);//清除中斷標(biāo)志位
- }
復(fù)制代碼
所有資料51hei提供下載:
庫(kù)函數(shù)版編碼器測(cè)試程序(編碼器5V供電。AB相分別接PA0 PA1 串口1以9600波特率輸出速度).rar
(303.62 KB, 下載次數(shù): 979)
2018-12-8 01:13 上傳
點(diǎn)擊文件名下載附件
stm32f103 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|