|
这是我关于《AVR单片机嵌入式系统原理与应用实践》书中P337页中的关于如何进一步降低被测信号频率的程序,请马老师和各位高手一起批评指正,其他都是和书上一样,所以就只要看主程序就可以了:
void main(void)
{
// Declare your local variables here
long feq_1val = 0,feq_2val = 0;
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0xFF;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0xFF;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x41;
DDRD=0x01;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500.000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Rising Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x41;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x0c;
TCNT2=0x00;
OCR2=0x7c;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0xa4;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
icp_n = 0;
max_icp = 1;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if(timer1_cap_ok==1)
{
if(feq_2>=feq_1)
{
feq_1val = feq_2-feq_1;
}
else
{
feq_1val = 65536-feq_1+feq_2;
}
if(!(feq_2>=feq_1&&over_flow))
{
if(feq_1val==feq_2val)
{
feq_times = 6000000*(long)max_icp/feq_1val;
if(feq_times>=4000)
{
max_icp = 64;
}
else
{
max_icp = 1;
}
begin_new = 1;
}
}
else
{
feq_1val = feq_1val+65536*(long)(over_flow);
if(feq_1val==feq_2val)
{
feq_times = 6000000*(long)max_icp/feq_1val;
begin_new = 1;
}
}
feq_2val = feq_1val;
timer1_cap_ok = 0;
begin_next = 1;
}
if(times_2ms_ok==1)
{
if(begin_new==1)
{
begin_new = 0;
PL_to_dis_buf();
}
else if(begin_next==1)
{
begin_next = 0;
icp_n = 0;
//over_flow = 0;
TIFR = 0x24;
TIMSK = 0xa4;
}
times_2ms_ok = 0;
}
}
} |
|