![]() |
|
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 |
![]() |
#7 | |
Nhập môn đệ tử
Tham gia ngày: Apr 2011
Bài gửi: 9
: |
SRf02
Trích:
Thôi k trình bày gì nhiều cả mình post code nên bạn giúp đc thì tốt quá Mà mình k hay vào đây lắm vì cũng hơi bận nếu đc bạn mail cho mình nhé,cảm ơn bạn nhiều lắm.Mail của mình là duong.dktd@gmail.com code pic #include <16F877A.h> #FUSES hs,NOWDT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD #use delay(clock=20000000) #use rs232(baud=9600,parity=N,xmit=PIN_D4,rcv=PIN_D3,bi ts=8,stop=2) #include <PIC16F877A_registers.h> #include <LCD_EBLOCK.C> void main() { INT16 I; UNSIGNED CHAR HSB,LSB,TR,CH,DV; TRISB=0; TRISA=0; DELAY_MS(1000); PORTB=0; LCD_INIT(); WHILE(1) { PUTC(0x00); PUTC(0x54); WHILE(!KBHIT()); HSB=GETC(); WHILE(!KBHIT()); LSB=GETC(); I=HSB*256+LSB; IF(I>300) { lcd_gotoxy(1,1); LCD_PUTC("DISTANCE=ERROR "); } ELSE { DV=i%10; TR=i/100; CH=(I/10)%10; lcd_gotoxy(1,1); LCD_PUTC("DISTANCE="); LCD_PUTC(TR+0x30); LCD_PUTC(CH+0x30); LCD_PUTC(DV+0x30); LCD_PUTC(" CM"); } DELAY_MS(2000); } } code avr /************************************************** *** This program was produced by the CodeWizardAVR V2.03.4 Standard Automatic Program Generator © Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 21/09/2011 Author : Company : Comments: Chip type : ATmega16 Program type : Application Clock frequency : 1.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 ************************************************** ***/ #include <mega16.h> #include <delay.h> // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x12 ;PORTD #endasm #include <lcd.h> #define START_CLK 0x0A // Start 1:8 prescaler CTC mode #define START_CLK_N 0x02 // Start 1:8 prescaler normal mode #define STOP_CLK 0x08 // Stop // Declare your global variables here void startTimer(unsigned int time) { OCR1A = time; // set thoi gian dem TIFR = 0x10; // Clear co` nho' timer TCNT1 = 0x00; // Clear timer TCCR1B = 0x0A; // Start timer 1:8 prescaler } void wait(void) { while(!(TIFR&0x10)); // doi co` nho' so sanh TCCR1B = STOP_CLK; // Stop timer } void startRange(void) { PORTA = ( 1<<PORTA.0 ); // set trigger len 1 startTimer(0x0002); //cho` 10uS // wait(); PORTA = ( 0<<PORTA.0 ); // clear trigger ve 0 } unsigned int Doc_kq(void) { unsigned int range; while(!(PINA&0x02)); // cho` xung echo len muc 1 de bat dau do do rong TCNT1 = 0x00; // Clear timer TCCR1B = START_CLK_N; // khoi tao timer 1:8 prescaler o normal mode while((PINA&0x02)); // cho` xung echo ve muc 0 de stop timer TCCR1B = STOP_CLK; // Stop timer range = TCNT1/58; // doc gia tri cua timer và chia cho 58 de dc kq la cm return(range); } void disp(int x,int y, unsigned int data) { lcd_gotoxy(x,y); lcd_putchar((data/100)+48); lcd_putchar(((data/10)%10)+48); lcd_putchar((data%10)+48); delay_ms(2); } void main(void) { DDRA = 0x01; TCCR1A=0x00; TCCR1B=0x08; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; TWBR = 0x20; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // LCD module initialization lcd_init(16); while (1) { unsigned int range; lcd_putsf("srf05"); startRange(); //gui xung 10uS de bat dau do range = Doc_kq(); // cho` xung echo va do do rong xung disp(0,1,range); wait(); delay_ms(1000); lcd_clear(); delay_ms(1000); }; } còn về kết nối mình làm uart nên k dùng trở ji cả kết nối trực tiếp thôi nói chung kết nối mình làm như datasheet. mình nghĩ k sai đc vì đọc rất nhiều lần rồi . K biết thực tế có phải mắc khác với datasheet k thì tớ chịu hic |
|
![]() |
![]() |
|
|