2013年2月22日 星期五

DIY - 智能小車:超聲波測距模塊 US-020 程式 (十九)

DIY - 智能小車:超聲波測距模塊 US-020 程式 (十九) 

PIC16F877A 程式是要設定了 RE0 (Pin 8)RE1 (Pin 9),開始時 RE0 (Pin 8) 會發出訊號到聲波測距模塊 (Ultrasonic Sensor Module) US-020 觸發 (Trig / Trigger) 腳,觸發 (Trig / Trigger) 的訊號由低變成高要達到 10us,然後監察 RE1 (Pin 9) 接收訊號,當訊號由低變成高,啓動計時器,繼續監察 RE1 (Pin 9) 接收訊號,當訊號由高變成低,停止計時器,讀出計時器的資料,計算出物件的距離便可。

超聲波測距智能小車
16F877A 超聲波測距模塊 US-020程式:
void wait_for_low_to_high() {
   while(input(PIN_E1)) ;       /* if it's high, wait for a low */
  delay_us(3);                 /* account for fall time */
   while(!input(PIN_E1));       /* wait for signal to go high */
}

void wait_for_low() {
   delay_us(3);                 /* account for rise time */
   while(input(PIN_E1));        /* wait for signal to go high */
}

void main(void)
{
  long int time;

   lcd_putc("f");                   // Clear Screen before printing buffer  
   printf(lcd_putc, "f%sn","BWS US-020  v1.0");

   do {
      output_bit(PIN_A0, 1);     // output logical 1 to PIN_A0
      scale = get_scale();

      output_bit(PIN_E0, 1);     // output logical 1 to PIN_A0
      delay_us(12);
      output_bit(PIN_E0, 0);     // output logical 1 to PIN_A0    
      wait_for_low_to_high();
      set_timer0(0);
      wait_for_low();
      time = get_timer0();

      printf(lcd_putc, "Time:%4Ldn", time);
      output_bit(PIN_A0, 0);     // output logical 1 to PIN_A0
      delay_ms(100);             // Delay 0.05u
   } while (TRUE);
} // End main

相關網址:
DIY - PIC:超聲波傳感器介紹 (一百六十三): 
DIY - PIC:超聲波測距模塊 US-020 介紹 (一百六十四): 
DIY - PIC:PIC18F4550 超聲波測距 Range Finder 電路 (一百六十五): 
DIY - PIC:PIC18F4550 CCP 的 Capture 輸入捕捉函數庫程式 (一百六十六): 
DIY - PIC:PIC18F4550 超聲波測距 Range Finder 程式 (一百六十七): 
DIY - PIC:PIC18F4550 超聲波測距 Range Finder 電路實驗 (一百六十八):

2013222 天氣報告
氣溫:18.8 @ 23:00
相對濕度:百分之81%
天氣:大致多雲

沒有留言:

張貼留言