大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-9-5
|
本帖最后由 qq4222 于 2016-10-14 17:52 编辑
使用平衡小车之家AB相编码器代码,移植到F429板子上,原程序使用定时器2和定时器四做编码器,因为开发板上没有找到TIM4引脚,后改用TIM3但移植过来后发现定时器3编码器并没有数值输出,定时器2却是有的,很是疑惑,程序上并没有发现有哪些不同,求个老师解惑。定时器2,3也没发现有多大区别啊。
代码贴上
- void Encoder_Init_TIM3(void)
- {
-
- /*定义一个GPIO_InitTypeDef类型的结构体*/
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能定时器4的时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能定时器4的时钟
- /* GPIO初始化 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- /* 配置Tx引脚为复用功能 */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- /* 连接 PA7 到 GPIO_AF_TIM2*/
- GPIO_PinAFConfig(GPIOA , GPIO_PinSource7 , GPIO_AF_TIM3);
- /* 连接 PA6 到 GPIO_AF_TIM2*/
- GPIO_PinAFConfig(GPIOA , GPIO_PinSource6 , GPIO_AF_TIM3);
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
- TIM_TimeBaseStructure.TIM_Period = 65535; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择时钟分频:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //使用编码器模式3,上升沿捕捉
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM3, &TIM_ICInitStructure);
- TIM_SetCounter(TIM3,0);
- TIM_Cmd(TIM3, ENABLE);
-
- }
- void Encoder_Init_TIM2(void)
- {
-
- /*定义一个GPIO_InitTypeDef类型的结构体*/
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能定时器2的时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能定时器2的时钟
- /* GPIO初始化 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- /* 配置Tx引脚为复用功能 */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_5;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- /* 连接 PA1 到 GPIO_AF_TIM2*/
- GPIO_PinAFConfig(GPIOA , GPIO_PinSource1 , GPIO_AF_TIM2);
- /* 连接 PA5 到 GPIO_AF_TIM2*/
- GPIO_PinAFConfig(GPIOA , GPIO_PinSource5 , GPIO_AF_TIM2);
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
- TIM_TimeBaseStructure.TIM_Period = 65535; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择时钟分频:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
- 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_SetCounter(TIM2,0);
- TIM_Cmd(TIM2, ENABLE);
-
- }
复制代码- int main(void)
- {
- u16 Count1,Count2;
- /*``````````延时配置``````````````````````````*/
- SysTick_Init();
- /*``````````串口配置``````````````````````````*/
- Debug_USART_Config();
- /*``````````系统时钟``````````````````````````*/
- TIMx6_Configuration();
- /*``````````编码计数器配置``````````````````````````*/
- // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- Encoder_Init_TIM2();
- Encoder_Init_TIM3();
- /*``````````串口打印测试``````````````````````````*/
- printf(" 这是一个串口中断接收 \n\n");
-
- while(1)
- {
-
- if(tick_5ms==40)
- {
- Count1 = TIM3->CNT;
- Count2 = TIM2->CNT;
- printf("\r\n 左编码器值:%d 右编码器值: %d \r\n",Count1,Count2);
- TIM3->CNT=0;
- TIM2->CNT=0;
- tick_5ms=0;
- tick_200ms++;
- if(tick_200ms==20)
- {
- tick_200ms=0;
- }
- }
- }
- }
复制代码
|
|