|
在用proteus仿真stm32的输入捕获时发现,无论是用波形发生器还是定时器生成PWM方波,输入捕获下获得的频率在oled显示不会改变,一直为0
代码
IC.c
#include "IC.h"
void IC_Init(void)
{
//开启定时器2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//开启GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//GPIOA配置
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIOA初始化
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InternalClockConfig(TIM3);//TIM2 选择定时器的内部时钟源
//时基单元配置
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
//时钟分频:1,2,4分频
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
//计数器计数方式:向上、向下、中央对齐计数
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
//设置ARR时钟周期
TIM_TimeBaseInitStructure.TIM_Period=65536-1;//ARR:10000个数后重装
//设置PSC预分频的值
TIM_TimeBaseInitStructure.TIM_Prescaler=72-1;//PSC:720分频,计数频率100k
//重复计数器的值,高级定时器,这里用不到设置为0
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
//时基单元初始化
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
//输入捕获
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter=0xF;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
//从模式+触发
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
//使能TIM2
TIM_Cmd(TIM3,ENABLE);
}
uint32_t IC_GetFreq(void)
{
return 1000000 / (TIM_GetCapture1(TIM3));
}
main.c:
#include "stm32f10x.h"
#include "OLED.h"
#include "delay.h"
#include "IC.h"
//#include "PWM.h"
int main(void)
{
delay_init();
OLED_Init();
// PWM_Init();
// PWM_SetPrescaler(720-1);//Freq=1000HZ
// PWM_SetCompare1(50);//Duty=50/100
IC_Init();
OLED_Clear();
OLED_ShowString(1, 1, "Freq:"); //1行1列显示字符串Freq:00000Hz
while(1)
{
OLED_ShowNum(1,6,IC_GetFreq(),5);
}
}
|
|