Interfacing Code of Tiva C Launchpad

The following Energia code for Launchpad reads values from the ultrasound sensor and monitors the values through a serial port.

The following code defines the pins in Launchpad to handle ultrasonic echo and trigger pins and also defines variables for the duration of the pulse and the distance in centimeters:

const int echo = 9, Trig = 10; 
long duration, cm; 

The following code snippet is the setup() function. The setup() function is called when the program starts. Use this to initialize variables, pin modes, to start using libraries, and so on. The setup function will only run once, after each power up or reset of the Launchpad board. Inside setup(), we initialize serial communication with a baud rate of 115200 and set up the mode of ultrasonic handling pins by calling a SetupUltrasonic();function:

void setup() 
{ 
   
  //Init Serial port with 115200 baud rate 
  Serial.begin(115200);   
  SetupUltrasonic();   
} 

The following is the setup function for the ultrasonic sensor; it will configure the Trigger pin as OUTPUT and the Echo pin as INPUT. The pinMode() function is used to set the pin as INPUT or OUTPUT:

void SetupUltrasonic() 
{ 
 pinMode(Trig, OUTPUT); 
 pinMode(echo, INPUT);  
   
} 

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Launchpad board.

The main loop of this is in the following code. This function is an infinite loop and calls the Update_Ultra_Sonic() function to update and print the ultrasonic readings through a serial port:

void loop() 
{ 
    Update_Ultra_Sonic(); 
    delay(200); 
} 

The following code is the definition of the Update_Ultra_Sonic() function. This function will do the following operations. First it will take the trigger pin to the LOW state for 2 microseconds and HIGH for 10 microseconds. After 10 microseconds, it will again return the pin to the LOW state. This is according to the timing diagram. We already saw that 10 µs is the trigger pulse width.

After triggering with 10 µs, we have to read the time duration from the Echo pin. The time duration is the time taken for the sound to travel from the sensor to the object and from the object to the sensor receiver. We can read the pulse duration by using the pulseIn() function. After getting the time duration, we can convert the time into centimeters by using the microsecondsToCentimeters() function, as shown in the following code:

void Update_Ultra_Sonic() 
{ 
  digitalWrite(Trig, LOW); 
  delayMicroseconds(2); 
  digitalWrite(Trig, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(Trig, LOW); 
 
  duration = pulseIn(echo, HIGH); 
  // convert the time into a distance 
  cm = microsecondsToCentimeters(duration); 
   
  //Sending through serial port 
  Serial.print("distance="); 
  Serial.print("t"); 
  Serial.print(cm); 
  Serial.print("n"); 
   
} 

The following code is the conversion function from microseconds to distance in centimeters. The speed of sound is 340 m/s, that is, 29 microseconds per centimeter. So, we get the total distance by diving the total microseconds by 29/2:

long microsecondsToCentimeters(long microseconds) 
{ 
return microseconds / 29 / 2; 
} 

After uploading the code, open the serial monitor from the Energia menu under Tools | Serial Monitor and change the baud rate to 115200. The values from the ultrasonic sensor are shown in the following screenshot:

Output of the ultrasonic distance sensor in Energia serial monitor
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.45