|
软件平台:AVR Stadio4
硬件平台:ATmega16最小系统
程序功能:用输入捕获功能计算方波频率
存在问题:输出频率有时对有时不对
想请教大家程序哪里有问题?
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include "LCD1602.h"
#define uint unsigned int
#define uchar unsigned char
uint Rising_Edge,Falling_Edge;
volatile uchar i=0;
uint count; //the number of Overflow
uint Freq;
uchar F_Num[6];
void Convert_Num(uint Num)
{
F_Num[0]=Num/10000+0x30;
F_Num[1]=Num%10000/1000+0x30;
F_Num[2]=Num%1000/100+0x30;
F_Num[3]=Num%100/10+0x30;
F_Num[4]=Num%10+0x30;
F_Num[5]='\0';
}
int main(void)
{
LCD_Init();
DDRD&=0x00;
PORTD|=0xFF;
TCCR1A=0x00; //all normal mode
TCNT1=0x0000;
TIMSK=0x24; //input capture interrupt enable,and overflow enable
TIFR=(1<<ICF1);
TCCR1B=0x44; //input capture enable rising_edge,Freq:F(I/O)/256,
sei();
LCD_Display(1,1,"Freq:");
while(i<2)
{;}
TCCR1B&=0xF8;
TCNT1=0x0000;
Convert_Num(Rising_Edge);
LCD_Display(1,6,F_Num);
Convert_Num(Falling_Edge);
LCD_Display(1,12,F_Num);
Convert_Num(count);
LCD_Display(2,1,F_Num);
Freq=(uint)(31250.0/((float)(Falling_Edge)+((float)(count)*65536.0)-(float)(Rising_Edge))); //the cycle of the waveform
Convert_Num(Freq);
LCD_Display(2,7,F_Num);
}
ISR(TIMER1_CAPT_vect)
{
if(i==0)
Rising_Edge=ICR1;
else if(i==1)
Falling_Edge=ICR1;
else ;
i++;
}
ISR(TIMER1_OVF_vect)
{
count++;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|