الان من یک برنامه با میکرو نوشتم که زمان تا میلی ثانیه خوب اندازه گیری میکنه برای فرکانس 50 هرتز اما پایین تر از میلی ثانیه تغییرات زیاد داره ؛ مثلا رو ال سی دی نشون میده 2.225 بعد میشه 2.265 ؟؟؟
کد:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Advanced
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#include <alcd.h>
#include <delay.h>
#include <stdlib.h>
unsigned int tcount1;
float delta_t;
char str[16],str1[16];
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
TCNT1=45536;//65536-45536=20000us=20ms=1/20ms=50HZ
TCCR1B=0x1;
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
tcount1=TCNT1;
TCCR1B=0x00;//stop Timer
tcount1=tcount1-45536;
delta_t=(float)tcount1/1000;
}
// Declare your global variables here
void main(void)
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: On
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x1;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: On
// INT1 Mode: Rising Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0F;
MCUCSR=0x00;
GIFR=0xC0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);//Start LCD
// Global enable interrupts
#asm("sei")
while (1)
{
lcd_clear(); //clear lcd
lcd_gotoxy(0,0); // Row 0 Colum 0
ftoa(delta_t,2,str1);
sprintf(str,"Delta=%sms",str1);
lcd_puts(str);
delay_ms(1000);
}
}




لطفا یک مقدار توضیح بدهید





دیدگاه