Ultrasonic Sensor With Keyboard & Display: PIC16F887

by Alex Johnson 53 views

Introduction to the Ultrasonic Sensor Project

This project delves into the fascinating world of embedded systems, focusing on building a distance measurement device using an ultrasonic sensor HC-SR04, a PIC16F887 microcontroller, a 4x4 keyboard, and a display. The project is written in assembly language using the MPLAB MPASM assembler. The main goal is to measure distance using the ultrasonic sensor, display the measured distance on a 7-segment display, and allow the user to configure minimum and maximum distance thresholds using the keyboard. This project offers a hands-on learning experience for anyone interested in electronics, programming microcontrollers, and understanding how sensors work. The use of assembly language provides a deeper understanding of the microcontroller's architecture and how it interacts with the hardware components. We will explore the code, the hardware setup, and the functionality of each component in detail.

Hardware Components and Connections

The core of this project relies on a few key hardware components. First, the HC-SR04 ultrasonic sensor is responsible for emitting ultrasonic waves and measuring the time it takes for the echo to return, thus calculating the distance to an object. The PIC16F887 microcontroller acts as the brain of the system, processing the sensor data, controlling the display, and handling keyboard input. A 4x4 keyboard provides a user interface for entering configuration values, and the 7-segment display shows the measured distance and any configuration parameters. The connections are crucial for the project's functionality. The HC-SR04's trigger pin is connected to a PIC16F887 output pin, which sends a short pulse to initiate the distance measurement. The echo pin is connected to the CCP1 module of the PIC16F887, which captures the duration of the echo pulse. The 7-segment display segments are connected to PORTD of the microcontroller, and the digits are selected using PORTB. The keyboard rows and columns are connected to PORTC. Proper wiring ensures correct operation and allows all components to communicate. The code provided is specifically designed to work with these connections, and any changes require corresponding modifications to the code.

Understanding the Code: Assembly Language

The code is written in MPASM assembly language, which offers direct control over the microcontroller's hardware. The code is structured in different sections: configuration bits, variable declarations, initialization routines, interrupt service routines (ISRs), and main program loop. The configuration bits set up the microcontroller's operating parameters, such as the oscillator type, watchdog timer, and low-voltage programming. The variable declarations define memory locations for storing sensor readings, configuration values, and display data. Initialization routines set up the ports, timers, and CCP module. The main loop reads sensor data, processes it, and updates the display. The ISR handles the interrupts generated by the CCP module and the timer, ensuring that the distance measurement and display multiplexing operate smoothly. The code is meticulously commented to explain each step, which is important for understanding the functionality. The assembly language provides a low-level view of how the system operates, allowing for fine-tuning of performance and resource utilization. This approach provides a deep dive into embedded systems programming.

Distance Measurement and Configuration

The HC-SR04 ultrasonic sensor works by emitting a short ultrasonic pulse and measuring the time it takes for the echo to return. The code uses the CCP1 module of the PIC16F887 to capture the duration of the echo pulse. The distance is calculated based on the speed of sound and the time elapsed. The program allows users to configure the minimum and maximum distance thresholds. This configuration is done using the 4x4 keyboard. Pressing the 'A' key (code 3) initiates the configuration mode, allowing the user to enter the minimum and maximum distance values using the numeric keys on the keyboard. The '#' key (code 14) is used to confirm the entered values. These thresholds are useful for setting up warning systems or controlling other devices based on the measured distance. The code handles the keyboard input, converts the key presses to numeric values, and stores the configured minimum and maximum distances in memory. This combination allows for a flexible and interactive system.

Display Multiplexing

To display the measured distance on a 7-segment display, the code uses display multiplexing. Display multiplexing allows using a single display to show multiple digits by rapidly switching between them. In this project, the Timer0 interrupt is used to control the multiplexing. The ISR cycles through each digit of the display, turning on the appropriate segments to show the corresponding digit's value. The digits are updated frequently enough that the human eye perceives them as being displayed simultaneously. This approach reduces the number of pins required to control the display. The code calculates the individual digits from the distance measurement, and the multiplexing routine ensures they appear correctly on the display. This is a common technique in embedded systems design, making efficient use of hardware resources.

Keyboard Input and Processing

The 4x4 keyboard provides a user interface for entering configuration values. The code scans the keyboard matrix to detect key presses. It reads the rows and columns of the keyboard to determine which key has been pressed. The key codes are then processed to determine their function, either for entering numbers or for confirming the entered values. The code includes a routine to debounce the keyboard, which prevents multiple key presses from being registered due to mechanical bouncing. The keyboard processing is done in the main loop and in the configuration routines. The keyboard input allows users to customize the system's behavior, configuring minimum and maximum distances or other parameters. This makes the system more versatile and user-friendly. The keyboard handling logic is critical for allowing user interaction and making the system functional.

Conclusion and Further Exploration

This project offers a comprehensive introduction to building a distance measurement device using an ultrasonic sensor and a microcontroller. The use of assembly language provides a deep understanding of the microcontroller's operation, while the integration of the keyboard and display adds user interaction capabilities. This project can be extended in many ways. You could add more advanced features, such as data logging, wireless communication, or more complex user interfaces. You could also integrate the distance readings with other devices. This project serves as a solid foundation for exploring the world of embedded systems and electronics. The combination of hardware and software knowledge is key to this project's success. The skills learned here can be applied to a wide range of embedded projects.

For more information on the PIC16F887, you can visit the official Microchip website. For additional details on the HC-SR04 ultrasonic sensor, check out the many online resources available.

External Links: