chenccl 发表于 2011-8-18 11:25:43

ATmega16输入捕获功能测方波频率

软件平台: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;

void Convert_Num(uint Num)
{
        F_Num=Num/10000+0x30;
        F_Num=Num%10000/1000+0x30;
        F_Num=Num%1000/100+0x30;
        F_Num=Num%100/10+0x30;
        F_Num=Num%10+0x30;
        F_Num='\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++;       
}

chenccl 发表于 2011-8-18 11:32:17

回复【楼主位】chenccl
-----------------------------------------------------------------------

另外:在程序中加入输入捕获的滤波后,程序就不能正常运行了,似乎是停留在While死循环那里

chenccl 发表于 2011-8-18 14:48:49

程序调试通过了,出错原因是:信号的幅值不够,应到3v左右。
页: [1]
查看完整版本: ATmega16输入捕获功能测方波频率