nga381lavender
11-10-2011, 02:37 AM
Các anh ơi cho em hỏi, em dùng Timer 1 đo tần số bộ dao động ngoài tạo bởi con IC Timer 555 ,đo số xung bởi timer1 chế độ counter trong thời gian 1s tạo bởi timer0 nhưng kết quả đo được không chính xác.Ban đầu proteus mô phỏng là 0 Hz (cảnh báo "Simulation is not running in real time due to excessive cpu load" ), đợi 1 lúc lâu sau thì đạt tầm >34kHz mà theo lý thuyết tính tần số ra của 555 với mạch của em,thì chỉ đạt tầm 6->7kHz thôi.:( Code của em như sau ạ :
#include <16F877A.h>
#include <DEFS_16F877A.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)//Fosc=1us
#byte T1CON =0x10//dia chi thanh ghi dieu khien cua timer1
#use fast_io(c)
#use fast_io(d)
#include <lcd.c>
int16 count;
int16 uiTimer1Ctr;//so xung lay ra tu timer 1 ~ tan so ra cua pin 3 of IC555
static void initCPU(void);
void main()
{
initCPU();
while(1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"\rTan so:%Lu",uiTimer1Ctr);
}
}
#INT_timer0 //Chuong trinh ngat timer0
void Timer0_isr() //Ham duoc goi khi timer 0 bi tran (255->0)
{
int16 t0ctr;//bien dem so lan tran cua timer0
set_timer0(56);//Tdinhthi=2*(256-56)*1us=400us
++t0ctr;
if(t0ctr==2500)//1s
{
bit_clear(T1CON,0);//stop bo dem timer1
uiTimer1Ctr=count*65535+get_Timer1();
t0ctr=0;
bit_set(T1CON,0);
set_TIMER1(0);
}
}
#INT_timer1
void Timer1_isr()
{
count++;
}
static void initCPU()
{
set_tris_d(0);
set_tris_c(0b00000001);
setup_TIMER_1(T1_EXTERNAL|T1_DIV_BY_1);
set_TIMER1(0);
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_2);
set_timer0 (56);
enable_interrupts (INT_timer0) ;
enable_interrupts (INT_timer1);
enable_interrupts (global) ;
count=0;
lcd_init () ;
}
#include <16F877A.h>
#include <DEFS_16F877A.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)//Fosc=1us
#byte T1CON =0x10//dia chi thanh ghi dieu khien cua timer1
#use fast_io(c)
#use fast_io(d)
#include <lcd.c>
int16 count;
int16 uiTimer1Ctr;//so xung lay ra tu timer 1 ~ tan so ra cua pin 3 of IC555
static void initCPU(void);
void main()
{
initCPU();
while(1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"\rTan so:%Lu",uiTimer1Ctr);
}
}
#INT_timer0 //Chuong trinh ngat timer0
void Timer0_isr() //Ham duoc goi khi timer 0 bi tran (255->0)
{
int16 t0ctr;//bien dem so lan tran cua timer0
set_timer0(56);//Tdinhthi=2*(256-56)*1us=400us
++t0ctr;
if(t0ctr==2500)//1s
{
bit_clear(T1CON,0);//stop bo dem timer1
uiTimer1Ctr=count*65535+get_Timer1();
t0ctr=0;
bit_set(T1CON,0);
set_TIMER1(0);
}
}
#INT_timer1
void Timer1_isr()
{
count++;
}
static void initCPU()
{
set_tris_d(0);
set_tris_c(0b00000001);
setup_TIMER_1(T1_EXTERNAL|T1_DIV_BY_1);
set_TIMER1(0);
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_2);
set_timer0 (56);
enable_interrupts (INT_timer0) ;
enable_interrupts (INT_timer1);
enable_interrupts (global) ;
count=0;
lcd_init () ;
}