Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:55 07 Oct 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Counting pulses, while the MM sleeps....

     Page 1 of 3    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9660
Posted: 05:35am 30 Sep 2025
Copy link to clipboard 
Print this post

I have a project, where I need to count the pulses from a charger module, when the MM2 chip wakes up, which it does every five seconds.

When the MM2 wakes up, it currently looks to see if a tilt-sensor is active.
If it is, the normal code runs.
If it is not, the MM2 goes back to sleep for another five seconds.
This repeats forever.

What I want to be able to do, is count the pulses from the charger module, when the MM2 wakes up, BUT - it must count in the correct place in the sequence, and must be able to work out the correct pulses vs just whatever the charger module is doing at the time the MM2 happens to wake up.  

The charger modules are these ones HERE, and the "Battery Status" LED is ground referenced, and the G+ output has the pulses on it that I want to monitor.

The charger module outputs pulses based on the condition of the battery.
4 pulses - Battery good.
3 pulses - Battery OK.
2 pulses - Battery weak
1 pulse  - Battery flat, recharge immediatley.

Each of these sequences of pulses, is seperatted by a 1000mS delay.

So, I need a way to first detect the 1000mS delay period, and THEN count the pulses on the pin.  However, as the MM2 chip can wake at any point in this sequence, I need a way to sync the pulses from the charger module, to the MM2 count input pin.

My very first thought was that upon waking, the MM2 should IGNORE what is going on on the count input pin, and WAIT till it gets NO PULSES for AT LEAST 500mS, then start counting, but the issue with that, is that as the MM2 can wake at any point in the pulse sequence from the charger module, waiting 500mS may still not work.

Perhaps I need to count at least TWO sets of pulse sequences?

Kinda scratching my head with this one, and how to approach WHEN I try to start counting the pulses, cos with the MM2 chip asleep, when it WAKES UP, the pulse sequence from the charger module can be literally ANYWHERE inside that pulse sequence, and it is doing my head in trying to think of how I sync the MM2 to the pulses when it wakes up and looks to count the pulses from the charger module.    

Anyone got any ideas?
Smoke makes things work. When the smoke gets out, it stops!
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5306
Posted: 06:50am 30 Sep 2025
Copy link to clipboard 
Print this post

Hi Grogster,

#1 why not keep the MM2 awake ? (*)
#2 use a picaxe 08M2 to keep track real time
#3 use hardware (counter + restart timer)

(*) I cannot get (in Europe) the charger details (Ali  blocks it) but when the G+ output is 3.3V logic, it could power (shottky diode, capacitor) the 08M2 or counter/timer, or maybe even the MM2, as well as supply the pulses.

But I would look at a way to keep the MM2 alive, maybe in some state (low CPU speed, all IO stopped) so it uses minimal power. But the 08M2 is also a solution.

You describe an alternative, where you wake up random, and start looking for the IWG (inter word gap). See the pulses as data (words), and the time in between 2 sequences is called the IWG. So you scan for X seconds without pulses, and start counting from there. But that IWG could be 2 seconds. So you would be awake 1 seconds of the 5 seconds. Still saving power, but not so much. Just make sure you detect the 1 second without pulses. Then you automatically skip being in the middle of a pulse train.

Volhout
Edited 2025-09-30 16:56 by Volhout
PicomiteVGA PETSCII ROBOTS
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6318
Posted: 07:04am 30 Sep 2025
Copy link to clipboard 
Print this post

What is the timing of the pulses?
Use the pulses to wake the micromite up. A micromite should wake fast enough to detect the trailing edge of the first pulse.

Continue counting the pulses for the time-span of the 4 pulses.
Process the count and go back to sleep.

Every 5th wakeup, do the things you were previously doing at the 5 second wakeup.

This relies on the battery system continuing to send its pulses.

Jim
VK7JH
MMedit
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5306
Posted: 07:06am 30 Sep 2025
Copy link to clipboard 
Print this post

Hi Grogster,

You can also connect G+ to pin 16 of the MM2. Then you wake up every time a new sequence of pulses starts at the first edge of the first pulse, count the pulses and go back to sleep. The housekeeping you do every 5 seconds or so, would then be triggered by 1 in every so many wakeups from pulse counting.

(you can't sleep with a XX seconds AND also sleep with wake up, since MMBasic prevents that).

Volhout
Edited 2025-09-30 17:18 by Volhout
PicomiteVGA PETSCII ROBOTS
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1568
Posted: 08:33am 30 Sep 2025
Copy link to clipboard 
Print this post

As usual, I have difficulty in fully comprehending the requirements but...

FWIW: On the E100, I have used the Click Counter module. The cool feature in my case is that; this SPI device, if powered separately, keeps counting even when the E100 is powered off. Power up the E100, open the SPI port and the count is there.

It uses the LS7366 and many modes of counting are available.

20MHz @ 3V3
40MHz @ 5V
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 608
Posted: 11:40am 30 Sep 2025
Copy link to clipboard 
Print this post

  Quote   need to count at least TWO sets of pulse sequences?


You have to detect and synch using the first set, then decode the second.
[eg as per sampling a signal can only work up to half the frequency]

Failing that, contact customer service  
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2450
Posted: 01:07pm 30 Sep 2025
Copy link to clipboard 
Print this post

the device used seems to be an ETA9640, see:
http://www.eta-semi.com/wp-content/uploads/2022/03/ETA9640_V1.5.pdf
and:
https://www.ebay.com/itm/203199357809

simplest solution: integrate the pulses at the LED's anode, using a relatively high value resistor and a largish capacitor. then measure the voltage across the capacitor with one of the MX170's analog inputs. you'll need to experiment with the component values to find what is optimal, and may need a blocking diode in series with the resistor (depending on if pin 7 is pulled low when the LED is off).


cheers,
rob   :-)
 
Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 872
Posted: 09:27pm 30 Sep 2025
Copy link to clipboard 
Print this post

Hi Grogs,
An interesting problem. I'll leave the electronic solutions to others (I'm not competent) but, a thought:

Assuming the battery condition doesn't change quickly, and is constant between most changes; by knowing its current/last status, can you alter the 5 second wakeup to put the MM in a better position to receive the next sequence?
Yes, it will foul-up at some condition changes but that should be infrequent - you can wait until a valid 1000ms pause to update its "current status" for the next reading.

Cheers,

Andrew
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9660
Posted: 11:04pm 30 Sep 2025
Copy link to clipboard 
Print this post

Hi everyone - thanks for the suggestions.

@ Volhout:

#1 - MM2 must sleep, as maximizing the battery life is what this project is all about.  Leaving the MM2 running - even at CPU 5 for example, has a DRASTIC effect on the life of the battery.  Sleeping the MM2, and only waking to check the tilt switch, and if inactive, go back to sleep, dramatically increases battery life before a recharge is needed.

#2 - That's interesting!  Never thought of that, but we'd have the same issue with the O8M2 running all the time, it will kill the battery much much faster, so although I like that idea, I can't do it that way either.

I like the idea of using the WAKEUP concept and counting the pulses then going back to sleep.  That is an idea I think I might play with on the test-bench.

I'll also look up the counter chip and the cap idea by Rob - all worthy of further investigation.  

It's only tricky, cos I need to keep the MM2 asleep as much as possible, to maximize the battery life.
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6318
Posted: 11:48pm 30 Sep 2025
Copy link to clipboard 
Print this post

@Grog,
How long are the pulses?
Their timing will make a big difference to the options available.

Jim
VK7JH
MMedit
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2755
Posted: 01:34am 01 Oct 2025
Copy link to clipboard 
Print this post

Jim makes an important point, It can take up to 1mS to respond to the WAKEUP Pin.

Synchronizing by timing alone has limitations as the main clock is stopped and the Wake Timer accuracy is ±20%. For a 5S interval, even if it starts perfectly synchronized, it would have to wake at least 1S early to be certain of catching a set of pulses, and stay awake till they arrived. Up to 1S if it just missed a set.

Both of these accuracy problems could be reduced by first measuring the actual Wake Timer and WAKEUP Pin response time for that specific chip and saving with VAR SAVE or putting them in the code. The times could possibly be affected by temperature and supply voltage so that will need more testing.

An external timer may be more accurate but also uses some power.
  Quote  CPU SLEEP or CPU SLEEP seconds [, abortpin]
Put the CPU to sleep. In this mode the running program will be halted and the current drain reduced to about 40 µA.
In the first form the WAKEUP pin (see the pinout tables at the start of this manual) will be automatically configured as a digital input and any change in its state (ie, from high to low or low to high) will wakeup the CPU. The time required to "wake up" in this mode is less than 1 ms.
The IR command shares the WAKEUP pin and if it is running the CPU will be awakened by the remote key press and MMBasic will immediately decode the signal and execute the IR interrupt.
In the second form (CPU SLEEP seconds) the command will put the CPU to sleep for the specified number of 'seconds' (accuracy is ±20%). The TIMER function and the internal clock/calendar will be updated when the sleep finishes.
The timed sleep can be terminated early if 'abortpin' is specified. This can be any I/O pin and any change in its state (ie, from high to low or low to high) will abort the sleep. It can take up to a second for the abort signal to be recognised so the change of state must be maintained for at least this time.

Edited 2025-10-01 11:39 by phil99
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2966
Posted: 03:09am 01 Oct 2025
Copy link to clipboard 
Print this post

Hey Grogster,

I think the solution is easy, (well famous last words).

Wake up at your 5s interval.
Check for pulses, and start a loop after each pulse that counts 500ms (or suitable delay) if another pulse comes in that 500ms then start the loop again when you get a 500ms with no pulse then start your count on the next incoming pulse, if no pulse after 1000ms then battery is too low.

What am I missing here?


Regards,

Mick (The big one)



.
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 608
Posted: 03:21am 01 Oct 2025
Copy link to clipboard 
Print this post

Maybe I am missing the problem ?

You can only get data from every second sequence at best.

A) On wake up, check tilt switch, if not active sleep again.
B) If active, monitor charger until there has been >=1000 msec from last pulse end.
C) Record next pulse sequence from charger and decode.
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2755
Posted: 03:29am 01 Oct 2025
Copy link to clipboard 
Print this post

If I have understood the issue, waiting 500mS would 10% of the total time awake and he would rather not use that much power.
  Quote  #1 - MM2 must sleep, as maximizing the battery life is what this project is all about.  Leaving the MM2 running - even at CPU 5 for example, has a DRASTIC effect on the life of the battery.  Sleeping the MM2, and only waking to check the tilt switch, and if inactive, go back to sleep, dramatically increases battery life before a recharge is needed.
So he wants to wake it just as the pulses are about to start, count them, check the tilt switch and if nothing needs to be done go back to sleep as quickly as possible.

I think Jim's point is using the pulses to wake it up may not work if the pulses last less than 1mS. The particular chip being used may respond faster than that but would have to be tested to find it's minimum wake pulse duration. Then compare that with the measured pulse durations being supplied. All unknown at this time.

Edit.
Perhaps a CMOS gate wired as a pulse-stretcher could be connected to the wake pin to ensure a 1mS pulse. A second CMOS gate wired as an integrator, as suggested by @robert.rozee, would provide an analogue voltage representing the number of pulses.
After waking the pin it is connected to would then be set to AIN.
After reading the voltage the AIN pin would be changed to DOUT to reset the integrator then the pin would be set to OFF.

Further thoughts.
The extra gates may not be needed.
The pulse stretcher:- diode --> capacitor to gnd. and Wake pin. After waking read the integrator. When ready to go back to sleep set it to DOUT to discharge the cap then OFF then SLEEP.
The integrator could be just as robert.rozee described:- diode --> resistor --> capacitor to gnd. and AIN pin. Measure and discharge as above.
Edited 2025-10-01 14:11 by phil99

Footnote added 2025-10-01 14:50 by phil99
Ok, forget all the above. See Jim's post below.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6318
Posted: 04:05am 01 Oct 2025
Copy link to clipboard 
Print this post

  phil99 said  
I think Jim's point is using the pulses to wake it up may not work if the pulses last less than 1mS. .... All unknown at this time.

The pulse is designed to light a LED so the user can count the 1 to 4 pulses.
I doubt if 1mS would be visible to human eye.

If it was 1mS on/1mS off, waking from sleep and counting the up to 4 pulses would be minimal drain on the battery.
That is easy to implement.

If is 10 mS on/10mS off, using the interrupt method would have high drain.
Integrating the pulses with a capacitor might be better. It would effectively be a slow PWM

Too little information to decide.

Jim
VK7JH
MMedit
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8156
Posted: 07:38am 01 Oct 2025
Copy link to clipboard 
Print this post

binary counter and a monostable?

The pulses go into the counter, the outputs of which go to GPIO pins. The monostable is triggered from the same pulse input as the counter input. It's output triggers the MM on its falling edge. The monostable has to be set to allow just a bit longer than the maximum number of pulses.

The pulses start, the monostable output goes high. When the monostable output falls the MM wakes up and reads the count.

Two CMOS chips aren't going to be much of a battery load.

I think you are going to have to do something like this to make sure things stay in synch. You can't allow for the inaccuracy of the charger pulses being at 1s and the MM being at 5s as one of them is bound to drift even if it's accurate initially. The MM doesn't need to do anything with the count until it's official wake up period, just store it.
.
Edited 2025-10-01 17:44 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6318
Posted: 08:10am 01 Oct 2025
Copy link to clipboard 
Print this post

If you want to use an external counter,
The 4040 12 bit binary counter would be ideal.
Just keep counting over the 5 seconds between the present wake-up.
If the 5sec is spot on, there will be 5, 10, 15 or 20 counts.
If the 5 secs is fast there could be 4, 8, 12 or 16 counts and
if the micromite is slow, you may get 6, 12, 18 or 24 counts.

For the purpose, that will be sufficient to determine the battery state.
5 pins needed to read the counter and one to reset after the read.

Jim
VK7JH
MMedit
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5306
Posted: 08:27am 01 Oct 2025
Copy link to clipboard 
Print this post

  phil99 said  I think Jim's point is using the pulses to wake it up may not work if the pulses last less than 1mS.


From the MM2 manual. The MM2 can wake up from pin 16 fast enough to also decode the IR remote control message. So you won't miss a pulse from this charger. Problem I see is when the charger does not output any pulse (=0) you sleep forever (unless you implement a watchdog).

Jim's idea can also work with a CD4024 (if that is in your parts bin).

Volhout
Edited 2025-10-01 18:35 by Volhout
PicomiteVGA PETSCII ROBOTS
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1143
Posted: 10:34pm 01 Oct 2025
Copy link to clipboard 
Print this post

Seems to me the external counter chip would need some sort of latch. When the monostable triggers after a few seconds of no pulse, the current count should latched to the output pins, and the counter should be cleared to start counting the next set of pulses.
Visit Vegipete's *Mite Library for cool programs.
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1568
Posted: 03:56am 02 Oct 2025
Copy link to clipboard 
Print this post

  vegipete said  Seems to me the external counter chip would need some sort of latch. When the monostable triggers after a few seconds of no pulse, the current count should latched to the output pins, and the counter should be cleared to start counting the next set of pulses.


LS7366 has multiple registers for this. Latched by the Index signal.
 
     Page 1 of 3    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025