Đo tấn số
Xin chào! Tôi là thành viên mới. Thấy anh em trong diễn đàn tao đổi hay quá, tôi xin tham gia cùng.
Tôi gửi chương trình làm với PIC6F876A cho anh em tham khảo cùng. Mạch này đo được tần số lên đến 50MHz. Tôi muốn anh em giúp tôi khi muốn đo tần số sóng mang trong tín hiệu đã điều chế biên độ thì làm thế nào?
Code:
/*==============================================================================
* Author :DRAGON(Hungnv0401@gmail.com)
* File Name :Do tan so den 50MHz va hien thi tren LCD
* Data :16/5/2007
* Hardware :PIC16F876A
* Compiler :CCS C 3.249
* Description :50MHz frequency Counter & Display on LCD, Input is PIN_C0(Pin11)
*=============================================================================*/
#include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#bit TMR1IF = 0x0C.0
#use delay(clock=20000000)
#define RS PIN_C4
#define RW PIN_C5
#define CE PIN_C7
int8 ch_tr, tr, tr_ngh, van, ngh; // chuc trieu, trieu, tram nghin, van, nghin
int8 cycles8, cycles;
int16 freqc_high;
int16 freqc_low;
int32 freq;
//-----------------------------------------------------------------------------//
//* --------------------- Khoi tao cho LCD -----------------------------------*//
void lcd_init()
{
output_low(RS);
output_low(RW);
output_low(CE);
OUTPUT_b(0x0f);
delay_us(100);
output_high(CE);
delay_us(100);
output_low(CE);
}
//-----------------------------------------------------------------------------//
//*---------------------- Dua byte ra LCD -----------------------------------*//
void lcd_put(byte data)
{
output_high(RS);
output_low(RW);
output_low(CE);
OUTPUT_b(data);
delay_us(500);
output_high(CE);
delay_us(500);
output_low(CE);
}
//-----------------------------------------------------------------------------//
//*-------------------------- Chon 2 dong cho LCD ---------------------------*//
void lcd_2lines()
{
output_low(RS);
output_low(RW);
output_low(CE);
OUTPUT_b(0x38);
delay_us(500);
output_high(CE);
delay_us(500);
output_low(CE);
}
//-----------------------------------------------------------------------------//
//*----------------------------- Gui dia chi cho LCD ------------------------*//
void lcd_add(byte add)
{
byte real_add;
real_add = 0x80 + add;
output_low(RS);
output_low(RW);
output_low(CE);
OUTPUT_b(real_add);
delay_us(500);
output_high(CE);
delay_us(500);
output_low(CE);
}
//-----------------------------------------------------------------------------//
//*--------------------------- Chuong trinh chinh ---------------------------*//
void main()
{
lcd_init();
lcd_2lines();
while (TRUE)
{
cycles8=0;
cycles=0;
freqc_high=0;
TMR1IF = 0;
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
//* -------------------------- Doi 1 giay ---------------------------------- *//
while (cycles!=0xFF)
{
cycles8=0;
while (cycles8!=0xFF)
{
if (TMR1IF)
{
TMR1IF = 0;freqc_high++;
}
else
{
delay_cycles(5);
}
delay_cycles(62);
cycles8++;
}
delay_cycles(216);
cycles++;
}
delay_cycles(211);
//*----------------------Ket thuc 1 giay ------------------------------------*//
//* ----------------Lay gia tri tu bo dem tan so ----------------------------*//
freqc_low = get_timer1();
freq=(65536*freqc_high + freqc_low)/5; //Vi thuc chat thach anh ta su dung
//o day la 4MHz = 1/5 cua 20MHz
ch_tr = freq/10000000 + 48;
tr = (freq%10000000)/1000000 + 48;
tr_ngh = ((freq%10000000)%1000000)/100000 + 48;
van = (((freq%10000000)%1000000)%100000)/10000 + 48;
ngh=((((freq%10000000)%1000000)%100000)%10000)/1000 + 48;
lcd_add(0x02);
lcd_put('F'); lcd_put('r'); lcd_put('e');lcd_put('q'); lcd_put(32);
lcd_put('C'); lcd_put('o'); lcd_put('u');lcd_put('n'); lcd_put('t');
lcd_put('e'); lcd_put('r'); lcd_put(32);lcd_put(32); lcd_put(32);
lcd_add(0x42);
lcd_put('F'); lcd_put('r'); lcd_put('e');lcd_put('q'); lcd_put(':');
lcd_put(ch_tr); lcd_put(tr); lcd_put('.'); lcd_put(tr_ngh);
lcd_put(van); lcd_put(ngh); lcd_put(32);
lcd_put('M'); lcd_put('H'); lcd_put('z');lcd_put(32);lcd_put(32);
lcd_put(32);lcd_put(32);lcd_put(32);
delay_ms(50);
}
} //End