งาน 1 Interfacing Ultrasonic Distance Sensor : ASCII Output with PIC Microcontroller


ในบางโครงการของเราเราอาจต้องการวัดระยะทางของวัตถุจากจุดหนึ่ง เซ็นเซอร์ระยะทางล้ำเสียงเป็นเซ็นเซอร์ที่ดีที่สุดซึ่งให้การวัดระยะทางที่เที่ยงตรงแม่นยำและไม่สัมผัสตั้งแต่ 2 ซม. ถึง 4 ม. เซ็นเซอร์อัลตราโซนิคสามารถใช้ในการวัดระยะทางระหว่างวัตถุเคลื่อนที่หรือวัตถุที่อยู่กับที่ ด้วยความแม่นยำและเสถียรภาพอุปกรณ์เหล่านี้จึงค้นหาแอปพลิเคชั่นจำนวนมากในสาขาหุ่นยนต์ ตัวอย่างเช่นสามารถใช้แทนเซ็นเซอร์ IR ใน Micromouse ได้อย่างยอดเยี่ยม ในบทช่วยสอนนี้เราจะทำการอินเทอร์เฟซเซ็นเซอร์ระยะทางอุลตร้าโซนิกกับ PIC Microcontroller สำหรับการสาธิตเราใช้ Rhydolabz Ultrasonic Distance Sensor พร้อมเอาท์พุต ASCII สามารถเชื่อมต่อกับ PIC Microcontroller ได้อย่างง่ายดายโดยใช้ USART เพียงแค่เชื่อมต่อขาออกของเซ็นเซอร์กับ RX pin ของไมโครคอนโทรลเลอร์ ในทุก ๆ 500ms เซ็นเซอร์นี้จะส่งคลื่นระเบิดอัลตราโซนิกและฟังเสียงก้อง เซ็นเซอร์จะส่งค่า ASCII ที่สอดคล้องกับเวลาที่ต้องการสำหรับการระเบิดแบบอัลตราโซนิกเพื่อกลับไปยังเซ็นเซอร์ UART ของเซ็นเซอร์ทำงานที่อัตราบอด 9600 และเซ็นเซอร์สามารถขับเคลื่อนโดย 5V DC ซัพพลาย เอาต์พุต ASCII ของเซ็นเซอร์จะเท่ากับระยะทางถึงสิ่งกีดขวางในหน่วยเซนติเมตร (ซม.)
The sensor has three pins…
  • Vcc  – +5V DC Supply to the Sensor
  • GND –  Ground Level of the Power Supply
  • SIG – Signal, Serial Data Out

Circuit Diagram



MikroC Program

// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections

void main() 
{
 int i;
 char dist[] = "000.0";
 Lcd_Init(); // Initialize LCD
 UART1_Init(9600); //Initialize the UART module
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,"Distance= cm");
 do
 {
   if(UART1_Data_Ready()) //if data ready
   {
     if(UART1_Read() == 0x0D) //check for new line character
     {
       for(i=0;i<5;)
       {
         if(UART1_Data_Ready()) // if data ready
         {
           dist[i] = UART1_Read();  // read data
           i++;
         }
       }
     }
   }
  Lcd_Out(1,10,dist);
}while(1);
}
I think the program is self explanatory. Every time program checks for new line character <CR> (0x0D), when it founds it, the program reads next 5 characters as the distance data. The distance data is assigned to a character array and is displayed in the LCD.

ความคิดเห็น

บทความที่ได้รับความนิยม