主函数:
int main(void)
{
delay_init(); //延时函数初始化
OLED_Init(); //oled屏幕初始化
OLED_On();
OLED_Clear();
TIM2_Cap_Init(); //定时器初始化
while(1)
{
OLED_ShowString(0,0,"Freq:",16);
OLED_ShowString(100,0,"Hz",16);
OLED_ShowNum(50,0,72000000/(Rising_Last-Falling),6,16);
OLED_ShowString(0,2,"Duty:",16);
OLED_ShowString(95,2,"%",16);
OLED_Showdecimal(50,2,(float)(Rising-Falling)/(float)(Rising_Last-Falling)*100,2,3,16);
delay_ms(100);
}
}
变量Rising_Last为第二次检测到上升沿捕获到的值,Falling为第一次捕获到的值,两者之差为定时器计数的数值差,根据TIM_Prescaler=0(即不分频),主频为72M,频率f=72M/(Rising_Last-Falling)。而Rising-Falling为高电平的时间,除以一个周期就是占空比了。 |