Part Number:EK-TM4C123GXL
Hi all,
I have connected the ultrasonic sensor to my MCU and flashed MCU with this simple code for obtaining the ultrasonic distance measurement and send it via UART:
#include "Wire.h" //Processing incoming serial data #include "Messenger.h" //Contain definition of maximum limits of various data type #include <limits.h> Messenger Messenger_Handler = Messenger(); /////////////////////////////////////////////////////////////////////////////////////// //DMP options //Set true if DMP init was successful bool dmpReady = false; //Holds actual interrupt status byte from MPU uint8_t mpuIntStatus; //return status after each device operation uint8_t devStatus; //Expected DMP paclet size uint16_t packetSize; //count of all bytes currently in FIFO uint16_t fifoCount; //FIFO storate buffer uint8_t fifoBuffer[64]; float ypr[3]; // ================================================================ // === INTERRUPT DETECTION ROUTINE === // ================================================================ volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high void dmpDataReady() { mpuInterrupt = true; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Ultrasonic pins definition const int echo = 9, Trig = 10; long duration, cm; ///////////////////////////////////////////////////////////////////////////////////////// //Time update variables unsigned long LastUpdateMicrosecs = 0; unsigned long LastUpdateMillisecs = 0; unsigned long CurrentMicrosecs = 0; unsigned long MicrosecsSinceLastUpdate = 0; float SecondsSinceLastUpdate = 0; //Setup serial, encoders, ultrasonic, MPU6050 and Reset functions void setup() { //Init Serial port with 115200 baud rate Serial.begin(115200); SetupUltrasonic(); //Setup Reset pins SetupReset(); //Set up Messenger Messenger_Handler.attach(OnMssageCompleted); } //SetupEncoders() Definition ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Setup UltrasonicsSensor() function void SetupUltrasonic() { pinMode(Trig, OUTPUT); pinMode(echo, INPUT); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Setup Reset() function void SetupReset() { pinMode(GREEN_LED,OUTPUT); pinMode(RESET_PIN,OUTPUT); ///Conenect RESET Pins to the RESET pin of launchpad,its the 16th PIN digitalWrite(RESET_PIN,HIGH); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //MAIN LOOP void loop() { //Read from Serial port Read_From_Serial(); //Send time information through serial port Update_Time(); //Send ultrasonic values through serial port Update_Ultra_Sonic(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Read from Serial Function void Read_From_Serial() { while(Serial.available() > 0) { int data = Serial.read(); Messenger_Handler.process(data); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //OnMssg Complete function definition void OnMssageCompleted() { char reset[] = "r"; char set_speed[] = "s"; if(Messenger_Handler.checkString(reset)) { Serial.println("Reset Done"); Reset(); } if(Messenger_Handler.checkString(set_speed)) { //This will set the speed Set_Speed(); return; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Reset function void Reset() { digitalWrite(GREEN_LED,HIGH); delay(1000); digitalWrite(RESET_PIN,LOW); digitalWrite(GREEN_LED,LOW); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Will update ultrasonic sensors through serial port void Update_Ultra_Sonic() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig, HIGH); delayMicroseconds(10); digitalWrite(Trig, LOW); // The echo pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. duration = pulseIn(echo, HIGH); // convert the time into a distance cm = microsecondsToCentimeters(duration); //Sending through serial port Serial.print("u"); Serial.print("\t"); Serial.print(cm); Serial.print("\n"); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Update time function void Update_Time() { CurrentMicrosecs = micros(); LastUpdateMillisecs = millis(); MicrosecsSinceLastUpdate = CurrentMicrosecs - LastUpdateMicrosecs; if (MicrosecsSinceLastUpdate < 0) { MicrosecsSinceLastUpdate = INT_MIN - LastUpdateMicrosecs + CurrentMicrosecs; } LastUpdateMicrosecs = CurrentMicrosecs; SecondsSinceLastUpdate = MicrosecsSinceLastUpdate / 1000000.0; Serial.print("t"); Serial.print("\t"); Serial.print(LastUpdateMicrosecs); Serial.print("\t"); Serial.print(SecondsSinceLastUpdate); Serial.print("\n"); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Conversion of microsecond to centimeter long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }
So I have connected my ultrasonic sensor to 5V, GND and PA7 pin and then after flashing the software I changed the mode to DEVICE and used pin PB1 to send the serial messages. Then, this PB1 pin is connected to my Odroid (like rasbperry) to UART Rx pin and all I get there are some garbage values. I have used the baud rate of 115200 on both devices.