7 years, 7 months ago.

Nucleo L476RG wait_us timer issues

Hi there mbed community!

This is my first post! So please feel free to correct me if I've broken any rules or posting etiquette.

My problem is as follows; I'm using a STM Nucleo L476RG and a (very common) HC-SR04 ultrasonic sensor.

The data sheet which can be found here http://www.micropik.com/PDF/HCSR04.pdf specifies that to trigger the (outgoing) ultrasound pulse i need to send a 10uS pulse to the sensor.

My method for doing this is a pretty simple one

NOTE: I have declared trigger as a DigitalOut

my 10 microsecond pulse

    trigger = 1;
    wait_us(10);
    trigger = 0;

However upon checking the pules out with an oscilloscope I have found the pulse to be almost 2.5 times as long!

/media/uploads/lilmohxv3/my10uspulse.jpg

Any help on how to actually send a 10uS pulse would be so greatly appreciated! I would like to avoid the "dirty" method of adjusting the wait_us value till it actually outputs 10uS, since id like this code to be as portable as possible.

Lastly, one thing to possibly take into consideration is that the HC-SR04 is actually a 5V device (while my pin only supplies 3.3V). Intuitively I wouldn't expect that to mess with the Nucleo's timings, but saying that I'm anything but an expert, so i thought it best to give all the info I could, just incase =)

Thanking you all in advance

Moh =)

What happens if you remove the wait_us statement? The GPIO code should definately be that slow, but just to rule it out.

posted by Erik - 19 Apr 2017

Hey there Erik,

Ive done as you suggested and simply turned the pin high and then straight to low again taking away the wait statement and just this is taking 12uS!

Any ideas on how to progress from here? :S

posted by Moh Jibunoh 19 Apr 2017

There is some library overhead, but 12us for that is really excessive.

1. Can you print the value of the SystemCoreClock variable? This should be 80MHz I believe for yours.

2. You can use trigger.write(1) and trigger.write(0). This is faster, but shouldn't be that much of a difference.

3. Can you have a look at what the FastIO lib does for you: https://developer.mbed.org/users/Sissors/code/FastIO? It needs a bit different syntax. And your MCU is not supported, but there are 3 different STM32 MCUs supported, and good chance one of them works for you (remove the #ifdef guards on one of the three files and see if it compiles. If not, try the next one).

posted by Erik - 19 Apr 2017

Hey again,

Really sorry it took me so long to respond I had this component in a final year project of mine and had deadlines looming. However I found a nice work around, I didnt end up using the libraries, instead i just wrote directly to the ODR pin register of the GPIO block. Worked a treat, for anyone needing the code its

void ultrasonic::ping(uint8_t port_B_Pin_Number)
{   
    GPIOB->setBit(ODR, port_B_Pin_Number);
    wait_us(10);
    GPIOB->clearBit(ODR, port_B_Pin_Number);
}

The definitions for setBit and clearBit are

#define setBit(reg, bit) reg |= (1 << bit)
#define set2Bit(reg, val, bit) reg |= (val << 2 * bit)

Thanks for the help Erik!

posted by Moh Jibunoh 23 May 2017
Be the first to answer this question.