đây là cod ecura em ạ. em chủ yếu dựa vào code trong CCS là chính:
Trích:
#include "F:\NCKH 2009\PIC 16\CCP1-2\CCP1-2.h"
#include <F:\NCKH 2009\PIC 16\timer01---\lcd_lib_4bit.c>
#include <F:\NCKH 2009\PIC 16\timer01---\DEFS_16F877A.h>
#define SRF05_IN PIN_D0
///
#define SRF05_OUT PIN_C3
#define CONVERT_TO_MICROSECONDS (20000000/((4)*1000000))
//
long rise,fall,pulse_width;
int16 tram1,chuc1,donvi1;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise; // CCP_1 is the time the pulse went high
} // CCP_2 is the time the pulse went low
void convert_bcd1(int16 x)
{
donvi1=(((x%1000)%100)%10) + 0x30; //chia lay phan du, so hang don vi
tram1=((x%1000)/100) + 0x30; //tach hang tram va hang chuc
chuc1=((x%1000)%100)/10 + 0x30;
///// hien thi len gia tri LCD//////
LCD_putcmd(0xC5);
LCD_putchar(tram1);
LCD_putcmd(0xc6);
LCD_putchar(chuc1);
LCD_putcmd(0xC7);
LCD_putchar(donvi1);
}
void main()
{
LCD_init();
//---hien thi LCD---
LCD_putcmd(0x85);
LCD_putchar("Anh_Gioi");
LCD_putcmd(0xC0);
LCD_putchar("S = ");
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
int16 mm2;
// 10us cho trigger
output_high(SRF05_IN);
delay_us(10);
output_low(SRF05_IN);
//tinh ra quang duong
mm2 = pulse_width/(5*58);
convert_bcd1(mm2);
delay_ms(100);
}
}
|
cái này em nối 2 chân CCP với nhau.
anh xem em có đúng khong ạ. kết quả hiển thị gần như chính xác ạ.