-
大小: 3KB文件類型: .c金幣: 1下載: 0 次發(fā)布日期: 2021-05-12
- 語言: 其他
- 標(biāo)簽: STM32F103RC??編碼器??測速??
資源簡介
STM32F103之實驗2控制1個電機并采用編碼器讀取電機轉(zhuǎn)速(代碼)
代碼片段和文件信息
#include?“encoder.h“
#include?“l(fā)ed.h“
///TIM4?編碼器輸入,TIM4_CH1作A相,TIM4_CH2作B相GPIO_Pin_6|GPIO_Pin_7;
TIM_ICInitTypeDef??TIM_ICInitStructure;
void?TIM4_Encoder_Init(u16?arru16?psc)
{
GPIO_InitTypeDef??GPIO_InitStructure;
TIM_TimebaseInitTypeDef??TIM_TimebaseStructure;
?NVIC_InitTypeDef?NVIC_InitStructure;
?RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4?ENABLE);//?使能定時器時鐘
?
?RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB?ENABLE); //io口使能使用到的捕獲編碼器AB相借,
?
?GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_6|GPIO_Pin_7;
?
?GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING;? //浮空輸入 ?
?GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz; ?
?GPIO_Init(GPIOB?&GPIO_InitStructure); ??
?//推挽輸出?,IO口速度為50MHz
?//初始化TIM4的時機單元
????TIM_TimebaseStructure.TIM_Period?=?899;?//設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 ?80K
TIM_TimebaseStructure.TIM_Prescaler?=0;?//設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值??不分頻
TIM_TimebaseStructure.TIM_ClockDivision?=?0;?//設(shè)置時鐘分割:TDTS?=?Tck_tim
TIM_TimebaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up;??//TIM向上計數(shù)模式
TIM_TimebaseInit(TIM4?&TIM_TimebaseStructure);?//根據(jù)TIM_TimebaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位
?
?//配置對應(yīng)寄存器為編碼器接口模式以及配置相關(guān)的輸入捕獲配置
??//THE?MODE?OF?ENCODER?
??
???TIM_EncoderInterfaceConfig(TIM4?TIM_EncoderMode_TI12TIM_ICPolarity_Rising?TIM_ICPolarity_Rising);??
?//TIM_ICPolarity_Rising上升沿捕獲?
????
TIM_ICInitStructure.TIM_Channel?=?TIM_Channel_1;?//CC1S=01? 選擇輸入端?IC1映射到TI1上
?? TIM_ICInitStructure.TIM_ICPolarity?=?TIM_
- 上一篇:1602LCD 庫文件
- 下一篇:輪式移動機器人FastSLAM算法研
評論
共有 條評論