![]() |
|
Tài trợ cho PIC Vietnam |
Cảm biến Camera, siêu âm, hồng ngoại, gyro, la bàn... |
|
Ðiều Chỉnh | Xếp Bài |
![]() |
#11 |
Đệ tử 2 túi
Tham gia ngày: Jun 2007
Bài gửi: 44
: |
SRF05 + PIC16F877A do khoảng cách
em đang dùng SRF05 + PIC16F877A do khoảng cách nhưng công lực còn yếu quá mong các bác có công lực thâm hậu chỉ em với .
về phần cứng cụ thể như sau : con srf05 có hai chế độ hoạt động đó là : -MOde1 : kết nối 2 chân trigger và echo vào 2 chân i/o của VDK -Mode2 : sử dụng chân của VDK để gửi tín hiệu trigger và nhận tín hiệu echo từ con srf05 em sư dụng chế độ Mode 2.kết nối chân RB0 với chân trigger/echo của con srf05. trên con srf05 chân mode nối với GND (0v). em dùng timer 1 để đếm độ rộng xung echo do con srf05 gởi phản hồi. sau đó lấy độ rộng xung/ 72 để đổi khoang cách tính toán ra đơn vị cm. đây là code của em , các bác xem qua chỉ em giúp em chỗ nào ko ổn. Code:
#include "D:\PIC PROGRAM\16F877A\SRF05\main.h" #include <LCD.C> #define TrgEch RB0 #define TrgDir TRISB0 unsigned int value=0; int get_srf05(void); void outlcd(unsigned int kcach); void setup(void); /* #int_EXT void EXT_isr(void) { } */ void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); lcd_init(); setup(); // enable_interrupts(INT_EXT); // enable_interrupts(GLOBAL); while (1) { value=get_srf05(); // printf("\n\rvalue= %u",value); outlcd(value); TMR1H = 0; // 52mS delay - this is so that the srf05 ranging is not too rapid TMR1L = 0; // and the previous pulse has faded away before we start the next one T1CON = 0x21; // 1:4 prescale and running TMR1IF = 0; while(!TMR1IF); // wait for delay time TMR1ON = 0; // stop timer } } unsigned int get_srf05(void) { TMR1H = 0xff; // prepare timer for 10uS pulse TMR1L = -14; T1CON = 0x21; // 1:4 prescale and running TMR1IF = 0; TrgDir = 0; //TRISB=0 // make trigger/echo pin an output TrgEch = 1; //RB0=1 // start trigger pulse while(!TMR1IF); // wait 10uS TrgEch = 0; //RB0=0; // end trigger pulse TMR1ON = 0; // stop timer TrgDir = 1; //TRISB=0; // make trigger/echo pin an input TMR1H = 0; // prepare timer to measure echo pulse TMR1L = 0; T1CON = 0x20; // 1:4 prescale but not running yet TMR1IF = 0; while(!TrgEch && !TMR1IF); // wait for echo pulse to start (go high) TMR1ON = 1; // start timer to measure pulse while(TrgEch && !TMR1IF); //Wait RB0=0 // wait for echo pulse to stop (go low) TMR1ON = 0; // stop timer TrgDir = 0; //TRISB=0 // make trigger/echo pin an output again return (TMR1H<<8)+TMR1L; // TMR1H:TMR1L contains flight time of the pulse in 0.8uS units } void outlcd(unsigned int kcach) { unsigned char tram=0,chuc=0,donvi=0; unsigned int ra=0; ra=kcach/72; lcd_gotoxy(1,1); tram=ra/100+48;lcd_putc(tram); chuc=(ra%100/10)+48;lcd_putc(chuc); donvi=(ra%100%10)+48;lcd_putc(donvi); } void setup(void) { unsigned long x; PORTB = 0xfe; // RB0 (trig) is output TRISB = 0xfe; // and starts low TRISC = 0xff; PORTC = 0xff; SSPSTAT = 0x80; SSPCON = 0x38; SSPCON2 = 0x00; SSPADD = 50; // SCL = 91khz with 20Mhz Osc /// for(x=0; x<300000L; x++); // wait for LCD03 to initialise } đây là file main.h Code:
#include <16F877A.h> #device adc=8 #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES NOPUT //No Power Up Timer #FUSES NOPROTECT //Code not protected from reading #FUSES NODEBUG //No Debug mode for ICD #FUSES NOBROWNOUT //No brownout reset #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NOCPD //No EE protection #FUSES NOWRT //Program memory not write protected #use delay(clock=20000000) #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) // register definitions #define W 0 #define F 1 // register files #byte INDF =0x00 #byte TMR0 =0x01 #byte PCL =0x02 #byte STATUS =0x03 #byte FSR =0x04 #byte PORTA =0x05 #byte PORTB =0x06 #byte PORTC =0x07 #byte PORTD =0x08 #byte PORTE =0x09 #byte EEDATA =0x10C #byte EEADR =0x10D #byte EEDATH =0x10E #byte EEADRH =0x10F #byte ADCON0 =0x1F #byte ADCON1 =0x9F #byte ADRESH =0x9F #byte ADSESL =0x9F #byte PCLATH =0x0a #byte INTCON =0x0b #byte PIR1 =0x0c #byte PIR2 =0x0d #byte PIE1 =0x8c #byte PIE2 =0x8d #byte OPTION_REG =0x81 #byte TRISA =0x85 #byte TRISB =0x86 #byte TRISC =0x87 #byte TRISD =0x88 #byte TRISE =0x89 #byte EECON1 =0x18C #byte EECON2 =0x18D //dinh nghia timer #byte TMR1H =0x0F #byte TMR1L =0x0E #byte T1CON =0x10 #byte SSPSTAT =0x94 #byte SSPCON =0x14 #byte SSPCON2 =0x91 #byte SSPADD =0x93 //dinh nghia bit #bit TMR1IF = 0x0c.0 #bit TRISB0 = 0x86.0 #bit RB0 = 0x06.0 #bit TMR1ON = 0x10.0 lcd của em em chỉ hiện giá trị 000-003, và nó dao động liên tục trong khoảng này. em cũng chưa thật hiểu cách làm việc của cảm biến siêu âm lắm,mong các bác tư vấn thêm. bác nào giúp em với .........
__________________
VÔ DANH !!! thay đổi nội dung bởi: picthanh, 31-03-2008 lúc 09:07 PM. |
![]() |
![]() |
|
|