CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

HC-SR04 Ultrasonic Sensor with 16F877A
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Jan 24, 2013 2:32 pm     Reply with quote

The data sheet for the ultrasonic unit leaves a LOT to be desired - as the timing diagram is quite VAGUE, when you look at it closely.
\IS there no delay associated with discriminating that the received signal is actually the excitation "chirp" and not environmental in-band noise ??
I would program the PIC to ONLY make a 10us GO signal at about a 1/2 second interval, and then set up a Test rig with the gizmo an accurate 3 meters from a hard reflective object.

Next use a DSO to nail down the TRUE timing between trigger pulse tail and the return TTL indication. I have no clue as to if there is any discrimination in HOW the 8 cycles of 40khz are timed RE the "valid" echo pulse begin.

Nothing else matters until that ambiguity is nailed down with test results. IMHO'

If this is a school project, then its no big deal- but if destined to be part of a product, diving in "code first" is not a good way to start off.
umutso



Joined: 23 Jan 2013
Posts: 39
Location: turkey

View user's profile Send private message MSN Messenger

PostPosted: Thu Jan 24, 2013 2:47 pm     Reply with quote

Actually it is weird that my simple code is not working and it is looping to the beginning where I printf the "press a key to start". And when i send a key press the trigger pin is triggered and echo is received (check with the scope) but code does not continue to the print finish and loops back. Since sometime I'm using Windows 8 and I'm using ccs for the first time in Windows 8. Don't check the requirements but start to think there is a problem while compiling. İf btw there is anyone capable of make this hc-sr04 ultrasonic ranger module work with a 16f877a, please share with me if applicable and it will be appreciated.

İ really need help and don't have much time to complete.

İ will try to compile the code on a windows 7 pc tomorrow.

Thanks
_________________
Umut Sonkurt
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 24, 2013 2:58 pm     Reply with quote

Quote:
İf btw there is anyone capable of make this hc-sr04 ultrasonic ranger module work

You still didn't set the trigger pin low at the start of main(). You're not
doing the things that we suggest:
Code:

void main()
{
output_low(TRIG);
 


Are you testing this program in real hardware or in Proteus ISIS ?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Jan 26, 2013 7:25 am     Reply with quote

Quote:
İ really need help and don't have much time to complete.

Sounds like a school project only taken seriously too late.

Stop messing about with the code and do as others and I have told you. Make some meaningful measurements on real hardware so that you have something concrete to present.

Mike
umutso



Joined: 23 Jan 2013
Posts: 39
Location: turkey

View user's profile Send private message MSN Messenger

PostPosted: Sat Jan 26, 2013 11:26 am     Reply with quote

Hi all,

First I have to say that this is not a school Project and I have a way to go also implementing a GSM board and get short ranged level on some corporate sites. And I'm working on real hardware as well.

Also I applied regarding your advices and the problem is solved.

I handled an oscilloscope and found that the echo pin is not reacting and when it went high entering a loop. So easily the problem was the wiring on my breadboard (defected pin) and the 16f877a was not grounded because of this defection. I fix it and all worked well.

Btw, in the code beginning to get the trigger pin low on start approach is not working. I did not go over this to find out why but the first code I sent is working. after some optimizations I can get a range of 300 cm with a tolerance of +-5cm.

Thank you all for your approach and advices.

Regards,
_________________
Umut Sonkurt
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Jan 26, 2013 11:58 am     Reply with quote

Congratulations on getting the job done.

I hope you can see why it's so frustrating doing long range fixes.

In your first post you're going on about code.
My immediate reaction was to suggest you look at the hardware.
Many posts, and more code later, you tell us you've fixed the fault (guess where).

I'd like to assume that you've learned a real lesson. Make sure the hardware's working properly, BEFORE embarking on detailed coding.

Mike

BTW. ALL of us were trying to help you.
umutso



Joined: 23 Jan 2013
Posts: 39
Location: turkey

View user's profile Send private message MSN Messenger

PostPosted: Sat Jan 26, 2013 1:14 pm     Reply with quote

Thank you for your patience mike and all. But as you know sometimes people can get stuck on some point and it is hard to rewind.

İ now need to learn one thing i do not know. İ m designing this prototype to work with a lion battery. Actually it works fine. But i have to optimize power usage to have the battery more life time. So i think to make a routine to make the pic sleep with a specific time period and when the time comes it should awaken and does the tasks then sleep again that certain time i set.

But a little problem i do not know how to code this. İ research on the datasheet and found the wdt. But i m not sure it can give me a long time range like 24 hours or 12 hours that i need.

İf you have any sample code or else info i will be pleased to have. İ m still searching on the net but cant find still a useful information yet.

Thanks
_________________
Umut Sonkurt
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Jan 26, 2013 6:26 pm     Reply with quote

Depends on exactly what you're trying to do.

Getting very low sleep current is not easy as my recent post shows.

Two approaches spring to mind:-

1) 32kHz crystal secondary oscillator on PIC.
2) 32kHz driven dedicated clock chip.

With 1), PIC runs all the time at 32kHz in sleep mode.
With 2), PIC effectively turns off, woken by external clock chip.

I don't have enough recent experience to help further.

Do a search on this forum, similar tasks have been done loads of times.

Mike
umutso



Joined: 23 Jan 2013
Posts: 39
Location: turkey

View user's profile Send private message MSN Messenger

PostPosted: Sun Jan 27, 2013 9:55 am     Reply with quote

Actually I think of this and I handled a DS1302 RTC timer. I can get time properly. And I can make the controller sleep. There the problem starts for me. I do not know how to make the controller start for eg. when the clock is after 5 hours? If anyone sends me a piece of sample code to wake up the controller after a while it will be great.

Thanks,

US
_________________
Umut Sonkurt
temtronic



Joined: 01 Jul 2010
Posts: 9634
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 11, 2015 7:33 pm     Reply with quote

Please, do NOT accept Proteus as a 'reliable working' simulator. read PIC101 sticky.
I've used the DS1307 for remote data loggers, cheap, accurate and easy to get a 1 Hz interrupt from it. Just be sure the coin cell battery is wired up else it won't work and obviously won't keep time. Within the RTC ISR simply increment a variable called 'seconds'. In main() check the value of 'seconds'. If it's say 18,000 (5 hours) then go do 'something'. There's a nice software RTC in the code library that shows how to do this.
As for 'battery life', use a bigger (more capacity) battery. For just a few pennies you can get 10X the life in a slightly bigger package. Also consider using a 'supercap' across the battery. This will help if you need high current power like turning on a relay, motor, etc.


food for thought
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 20062

View user's profile Send private message

PostPosted: Thu Feb 12, 2015 4:46 am     Reply with quote

Key is to understand that a processor that wakes for a handful of machine cycles, draws a fabulously small amount of power.
If (for instance) you have a 'tick' once per second that wakes a chip, and it uses an internal RC oscillator to run (so almost immediate start-up), running at 4Mhz. It can simply decrement a counter (handful of machine instructions), test if it has reached zero, and if not, go straight back to sleep. The chip wakes for a total of perhaps 20cycles every second, drawing perhaps 0.5mA when awake, then returning to being fully asleep (drawing only a fraction of a uA - say 1uA). The 'average' power consumption is just (20/1000000)*0.5E-3 + 1E-6 = 1.01uA. As you can see the amount 'added' by the short time awake is tiny.
So you don't put the chip to sleep for 5 hours, but some nice time that the RTC chip supports (typically 1 to 10 seconds), and have the CPU 'know' that it wants to sleep for perhaps 18000 of these cycles.
temtronic



Joined: 01 Jul 2010
Posts: 9634
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 12, 2015 6:07 am     Reply with quote

Also if you use a 'high speed' xtal (say 4MHz), the PIC does the waking up, testing, returns to sleep very, very fast, just a few microseconds. It can do lot in a very short time. If, as others have tried, use a slow xtal, like 32KHz (watch xtal), it takes the PIC a LONG time to wake up, do the testing, then go back to sleep. This actually consumes a LOT more power and the battery will be used up very quickly.

There is a software RTC in the code library, that might work for you but remember that should the power to the PIC fail, you'll lose the RTC data. An external device like the DS1307, with it's own backup battery, could last decades.

just ideas.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group