新手上路
- 积分
- 21
- 金钱
- 21
- 注册时间
- 2020-7-10
- 在线时间
- 5 小时
|
1金钱
这是我的代码,定时器5可以正确读出编码器的值,但是定时器8读出的都是0,请求大佬协助
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"
#include "touch.h"
//ALIENTEK 探索者STM32F407开发板 实验28
//触摸屏实验 --库函数版本
//技术支持:www.openedv.com
//淘宝店铺:http://eboard.taobao.com
//广州市星翼电子科技有限公司
//作者:正点原子 @ALIENTEK
/**************************************************************************
函数功能:把TIM2初始化为编码器接口模式
入口参数:无
返回 值:无
A0,A1
**************************************************************************/
void Encoder_Init_TIM5(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能定时器2的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIOA0,1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOA
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //GPIOA15配置成TIM2_CH1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5); //GPIOB3 配置成TIM2_CH2
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(TIM5, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM5,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//使用编码器模式3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);//清除TIM的更新标志位
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM5,0);
TIM_Cmd(TIM5, ENABLE);
}
/**************************************************************************
函数功能:把TIM4初始化为编码器接口模式
入口参数:无
返回 值:无
B6 B7
**************************************************************************/
void Encoder_Init_TIM8(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//使能定时器4的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//GPIOC6.7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8); //GPIOB6.7 配置成TIM4_CH1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM8); //GPIOB6.7 配置成TIM4_CH2
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
TIM_TimeBaseStructure.TIM_Period = 65535; //设定计数器自动重装值65535
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM8, &TIM_ICInitStructure);
TIM_ClearFlag(TIM8, TIM_FLAG_Update);//清除TIM的更新标志位
TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM8,0);
TIM_Cmd(TIM8, ENABLE);
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //LCD初始化
KEY_Init(); //按键初始化
tp_dev.init(); //触摸屏初始化
Encoder_Init_TIM5();
Encoder_Init_TIM8();
while(1){
LCD_ShowNum(20,20,(short)TIM8 -> CNT,5,16);
LCD_ShowNum(20,40,(short)TIM5 -> CNT,5,16);
}
}
|
|