我想通过捕捉边沿计算出电机的转速并显示到LCD屏上TIM5通道1肯定是不行了,TIM5通道2和PA1是对应的,为什么配置了也不行呢?
麻烦大家看一下我的配置有没有问题
还有就是F407的板子有的IO口在开发手册上都说了不是独立IO为什么还是引出来了?就比如PA1,没懂
GPIO的配置写到了KEY_Init里面
[mw_shl_code=c,true]
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "key.h"
#include "delay.h"
#include "timer.h"
extern u8 TIM5CH1_CAP_STA;
extern u32 TIM5CH1_CAP_VAL;
int main(void)
{
long long temp = 0;
char Data[20];
KEY_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LCD_Init();
Timer_Init(0xFFFFFFFF,84-1);
POINT_COLOR = BLACK;
LCD_Clear(WHITE);
while(1)
{
delay_ms(10);
if(TIM5CH1_CAP_STA&0X80)
{
temp=TIM5CH1_CAP_STA&0X3F;
temp*=0XFFFFFFFF;
temp+=TIM5CH1_CAP_VAL;
temp = 60000000/temp;
sprintf(Data,"Speed:%10lld r/min",temp);
LCD_ShowString(50,100,400,24,24,(unsigned char *)Data);
TIM5CH1_CAP_STA=0;
}
}
}
#include "key.h"
#include "sys.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5);
}
#include "timer.h"
void Timer_Init(u32 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM5,&TIM_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_Init(&NVIC_InitStructure);
}
u8 TIM5CH1_CAP_STA = 0;
u32 TIM5CH1_CAP_VAL;
void TIM5_IRQHandler(void)
{
if ((TIM5CH1_CAP_STA & 0x80)==0)
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update)==1)
{
if (TIM5CH1_CAP_STA & 0x40)
{
if ((TIM5CH1_CAP_STA & 0x3F)==0x3F)
{
TIM5CH1_CAP_STA |= 0x80;
TIM5CH1_CAP_VAL = 0xFFFFFFFF;
}
else TIM5CH1_CAP_STA++;
}
}
if (TIM_GetITStatus(TIM5, TIM_IT_CC1)==1)
{
if (TIM5CH1_CAP_STA & 0x40)
{
TIM5CH1_CAP_STA |= 0x80;
TIM5CH1_CAP_VAL = TIM_GetCapture1(TIM5);
}
else
{
TIM5CH1_CAP_STA = 0;
TIM5CH1_CAP_VAL = 0;
TIM5CH1_CAP_STA |= 0x40;
TIM_Cmd(TIM5,DISABLE);
TIM_SetCounter(TIM5,0);
TIM_Cmd(TIM5,ENABLE);
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>