Jump to content
lawrence_jeff

SR04 Ultrasonic Driver

Recommended Posts

Attached find a driver for an HC-SR04 ultrasonic distance sensor (configurable for other similar devices)

I had a lot of issues with the sensors operating very unreliably when they operate beyond max distance so there is a bit of additional code to handle this

It supports single pings, multiple pings (with and without averaging) and a continual ping mode driven by a timer - so you can start it and then just check a variable at any time to get the most current measurement. Also supports returning statistics on the ping history

 

I'm still new to Stellaris and C in general so probably more bloated than it needs to be feel free to suggest better ways of doing things - I'm still learning and open to feedback.

 

 

Also a few questions

1) Is there a better way to dynamically pass the port/pin assignments dynamically instead of using a header file - I guess I'm more used to object oriented approaches where it would be easier to instantiate multiple instances of a driver to support multiple devices.

2) Is there a better way to return data from an ISR than a global variable?

3) Any good examples of a driver for a device that returns data -most of the examples I found are one way

 

Anyway hope someone can use it.

 

 

HC_SR04.zip

Share this post


Link to post
Share on other sites

Thanks for sharing your driver!

 

 

 

 

 

1) Is there a better way to dynamically pass the port/pin assignments dynamically instead of using a header file - I guess I'm more used to object oriented approaches where it would be easier to instantiate multiple instances of a driver to support multiple devices.

yes, you can change port assingments on the fly.. but you will have to setup that pin again.. as an SSI, output or input etc.

2) Is there a better way to return data from an ISR than a global variable?

Not that I know off. You can do a get/set type function call within the interrupt. but there will be a little time penalty

3) Any good examples of a driver for a device that returns data -most of the examples I found are one way

 

It's possible with pointers. Look up "pass by reference" function calls.

Share this post


Link to post
Share on other sites

lawrence_jeff,

How did you connect the HC-SR04 module to the Stellaris Launchpad?
From the source code I see you used ports PE1 and PE2 for Trig and Echo but how did you provide 5V to HC-SR04?

Thanks

Share this post


Link to post
Share on other sites
I tried porting this to tiva c and i get the following two errors when trying to compile
 
Description Resource Path Location Type
#20 identifier "INT_GPIOE_" is undefined HC_SR04.c /RGB line 43 C/C++ Problem
Description Resource Path Location Type
#20 identifier "INT_TIMER0A_" is undefined HC_SR04.c /RGB line 75 C/C++ Problem
 

can anyone try and help as i am trying to make the HC-SR04 work with the Tiva C

Share this post


Link to post
Share on other sites

 

I tried porting this to tiva c and i get the following two errors when trying to compile
 
Description Resource Path Location Type
#20 identifier "INT_GPIOE_" is undefined HC_SR04.c /RGB line 43 C/C++ Problem
Description Resource Path Location Type
#20 identifier "INT_TIMER0A_" is undefined HC_SR04.c /RGB line 75 C/C++ Problem
 

can anyone try and help as i am trying to make the HC-SR04 work with the Tiva C

 

 

can i ask wich board are you using and wich compiler?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...