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 support@ccsinfo.com

Counting Pulses during sleep mode

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

Counting Pulses during sleep mode
PostPosted: Fri Feb 27, 2009 4:17 am     Reply with quote

Hello i am working on a project that measures water flow. The method i use to do that is by counting high to low pulses. Because system works with battery i want to save as much energy as i can. This leads to sleep mode. But is it possible to count the pulses during sleep mode?

Pulses are driven to timer0 pin T0CKI.

I use PIC 18F2520.
Thank you.
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Fri Feb 27, 2009 9:21 am     Reply with quote

I would rather suggest to use the INT0 (or INT1, INT2) input, you will have more control counting the pulses in software. (you might need some kind of debouncing anyway: like reading the port again 10-20-50 msec later, to make sure, one pluse is one pulse, and not just the switch bounces,
Do you have a reed switch as the sensing device or is it some kind of electronics ? if it is electronics, then the debouncing may not be required.

So the strategy should be, the pulse egde wakes up the PIC, it increments a counter in software (a int32 variable will do, I think), does some housekeeping if needed and goes back sleeping again waiting for the next pulse.

You also have to decide solving the problem of how to retrieve the data (waking up on data request (data polling) or sending a status message to a host at certain time intervals (data pushing)

There is also the problem of storing the counter in a non-volatile way. or else you will lose the counter value if the battery fails, you cannot however update the value each time in eeprom (writing it uses lots of energy, and also would wear out too quickly)

...these were just a few thoughts.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Feb 27, 2009 11:12 am     Reply with quote

Quote:
Pulses are driven to timer0 pin T0CKI
doesn't work in sleep mode, cause PIC timers are synchronous units that depend on a system clock, see data sheet block diagrams. Interrupts as suggested by tibor are the only suitable solution, or possibly polling in a sufficient fast wake-up cycle.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 6:38 pm     Reply with quote

Timer1 and Timer3 on many chips have an asynchronous mode. Would these continue counting in power down mode?
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 12:23 am     Reply with quote

Yes. Counting occurs on the rising edge of the input though. I tested it on the 18F2520. If it's a reed switch then contact bounce could be a problem - should be easily fixed with a resistor and capacitor because the input has a Schmitt buffer.

Would need to count over a fixed period to measure water flow rate, so instead might be better to use either timer 1 or timer 3 with an external 32.768kHz crystal to do the timing and use the external interrupt to do the wakeup and variable incrementing suggested by libor.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 1:50 am     Reply with quote

Quote:
Timer1 and Timer3 on many chips have an asynchronous mode.
Yes, you're right, I wasn't aware of.
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 3:59 am     Reply with quote

Hello again sorry for the delayed response. Using the INT0 interrupt to wake the PIC and then go to sleep I think it wouldn't make any sense cause pulses have high frequency lets say sysyem could have 50 pulses/sec. So I think wake the PIC too often it would be equal from staying awake all the time. I don't know if I am correct.


P.S. The sensing device is a Hall effect Sensor like Hi300.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 6:48 am     Reply with quote

Depending on the oscillator options, e. g. PLL usage, you can expect start-up times in the 0.2 to 2 ms range. The wakeup cycle code can be held rather short in most applications. So there's still a considerable power saving potential with 50 Hz pulse rate, I think. Asynchronous counter operation combined with WDT based wakeup is probably more effective. The sensors static power consumption must be expected higher in case of an electronical sensor, however.
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 1:07 pm     Reply with quote

jojos wrote:
pulses have high frequency lets say sysyem could have 50 pulses/sec.

Do you have a 1 pulse / ml resolution ? what kind of watermeter has such a flowrate or resolution that you have 50 Hz pulses ? A typical household watermeter that has 1 pulse / liter, so a maximum achievable pulserate of a 1Hz only with a 3600 liters / hour flow when you bath your hippo. But it is more common to see 10 or even 100 liters per pulse watermeters.

jojos wrote:
The sensing device is a Hall effect Sensor like Hi300.

According to the datasheet this device draws 3.5mA (typical) (6 mA maximum)
1. this is a 1000 times (!!) higher current consumption than a low-power hall-sensors used in watermeters today like this one here: AH-182 draws 5uA. For a battery-powered watermeter application 3mA is way to high, do you really want to replace an expensive lithium primary battery every 2 weeks ?

2. compared to this 3.5mA current of the sensor you really do not need to worry about the PIC's power consumption, even without sleeping, a constantly running PIC (in the LP oscillator modes) draws as little as 8-30uA (with full operation at 32kHz e.g., no sleep, or 100-200uA at 1Mhz if you need more MIPS). This is as low as 1/300 - 1/100th of the power need for the Hi300 sensor. Why sleep ? .. to save 0.3 - 1% of power?

3. FvM is right, even if waking up at a rate of 50 Hz for 0.2 - 2 ms will give you a 1-10 % duty rate, lowering the power consumption of your application to 1-10%*, giving you a 10 - 100 times more (!!!) battery life (does this really make no sense ?)

*not counting the sleep current of 0.1 - 1uA

4. The timer counter during sleep approach may work. But you would have to leave on the timer which draws more current than a completely turned off PIC waiting for an INT.
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 12:59 am     Reply with quote

Thank you for your response .About hall effect sensor you are right.I was aware of the consumption i mentioned it to my project manager earlier to take care of it.I am sure we will choose your proposal Libor.As for the pulses i will turmn to the interrupt on change solution.I calculated the time and i saw that the claculations i have to make in every interrupt are maximum of 2 msec.So if i get 20 pulses/sec i will wake the processor for maximum lets say 100 msec with all the times (Stability time after wake e.t.c.)
and so i will have 900 msec sleep and 100 msec awake.Am i right?

The water meter will be used for fields (agricultural purposes)

Libor can you explain to me the debounching problem that you mentioned in your first reply.Thanks again
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 1:50 am     Reply with quote

jojos wrote:
can you explain to me the debounching problem that you mentioned in your first reply.Thanks again

Bouncing occurs in swithces with mechanically moving contacts (if you were using a reed switch for example). Bouncing: you got many small pulses instead of one firm pulse this occurs especially when the contacts close.

The hall-effect switches have their own built-in electronics and hysteresis in sensing the changing magnetic field. You do not need to worry about this effect with them.

There is however another catch: if you have the actuator magnet mounted on a too sensitive, fast moving gear in the counting mechaninsm, it can occur that the water itself bounce back-and-forth a little (e.g. when you close the tap abruptly, especially with rubber tubing used in agriculture sometimes), if the actuator magnet happens to stop about the edge between the open and closed position, this can give you some false signals as if the water was flowing in one direction, but in fact it was only moving a little back and forth.
There are solutions with two and even three sensors sensing flow direction also to avoid this, but I would just make a filter in software (like filtering out the pulses that come too fast for a real flowrate) counting some false pulses occasionally can be acceptable within the tolerance limits of your solution.

edited:
PS. The low-power hall-effect sensors (those with a few uA-s operating current) are themselves working in a low-duty cycle. To reach this low-power operation in average, they wake up every x-th second for some ms to see the magnetic field (consuming several mA-s in this short period)and then go back sleeping again for most of the the time indepently of the magnetic field, leaving theit output in the corresponding state durring this long sleep.
The "price" and also the benefit* of this low-power mode is that they are not suitable for fast moving measerement. (check the datasheet for their reaction-time)
* by benefit I mean that they filter out any short glitches per se.

Depending on the magnet placement I would suggest using a unipolar sensor giving you 1 pulse per revolution with a magnet rotating aroung its own axis, or the more common omnipolar type reacting on both poles to be used with a magnet moving along the perimeter of some rotating part, so you would not have to worry about which pole it was mounted outwards.

Btw. most of these sensors are meant to be used e.g. in flip-open cell-phones to sense the user's will to illuminate the keyboard, picking up the phone... so no fast counting is required.
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 4:00 am     Reply with quote

Libor i saw the datasheet of AH182/183 and to be honest i didn't understand the thing about duty cycle.First of all it has 0.1% duty cycle for AH182 and 25% for AH183.Also there is an awake time of 50 microsec for both and Tperiod time of 50 millisec for AH182 and 200 microsec for AH183.
Which of this characteristics refer to the sample rate of the sensor you mentioned before.


Edit:Ok i think i got it The whole period is for 50 millisec and wakes for 50 microsec.Am i right ?For my case if i have 20 pulses/sec will i get correct measuring or not?
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 5:20 am     Reply with quote

AH-182: 0.1% duty cycle = 50us awake 49950us sleeping (TPeriod 50ms = 50000us is the whole cycle)

AH-183 25% duty cycle = 50us awake 150us sleeping (TPeriod 200us is the whole cycle)

For 20 pulses / sec signal, that's a 50 ms period time: the same 50 ms sample rate of the AH-182 would be not enough, a 3-10x oversampling rate is a minimum I would go for a reliable detection.

The AH-183 with its period time of 200us can be a good candidate. It would "listen" to the events (change in magnetic field) 250 times during a 50 ms period of your signal, this is even too much (but no problem)
However you should check the datasheet if they do not artificially slow down the detection rate of this type.

Btw: do you need these counters data to be remotely collected using an RF transceiver for example? or they are just data-loggers to be read at the site.
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 5:37 am     Reply with quote

Its on the site for now. No rf transmission. Ok so I will pick some samples of the AH183 to test them. Thanks again.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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