I want to make a 4x single cell NiMH charger. using the pic 18F4550 DIP family using the MPLAB and C18

Q1. I need 4 independent 1 second timers.

Here I ran into my lack of knowledge on the way the 18F guts are shared for parallel tasks. I of course could use 4 x series 1Meg resistor with 10 uF on any port pin.

When a cell is inserted for charging a port pin will go high applying 5 V as a driving voltage.

Using an analogue input pin , maybe AN0, the 63% point can be detected and flip that driving port pin back to zero. As there are four cells, they will require four analogue input pins, sounds a bit wasteful of analogue pins .

I also thought about using four digital input pins to change from low to high as this is a cmos chip and they flip at 2/2 Vdd.

The toggle will take place when 1 second has elapsed at the 63% charge voltage.

But I really want to use the onboard digital timers, taking me back to my initial question Q1

Help please


  • \$\begingroup\$ What are you using the timers for (ie what triggers them and what does your code want to do when the 1 second has elapsed)? Could you not drive them all off a single digital timer? \$\endgroup\$
    – pjc50
    Commented Dec 30, 2015 at 11:25
  • 1
    \$\begingroup\$ @Fred Mah: you need to improve the question. It is very hard to understand what you are trying to do and what your question is. (There is no question mark [?] in all your 'question'.) There is a schematic editor button available when editing your question. Add one if it helps. What are the specifications for the NiMH cells? Are you planning on charging the batteries from the PIC pins directly? (Did you calculate the maximum current the PIC will supply?) What are the 1s timers for? What will 'flip at 2/2 Vdd'? (2/2 = 1.) Again, what is the actual question? \$\endgroup\$
    – Transistor
    Commented Dec 30, 2015 at 11:39
  • \$\begingroup\$ Since PIC is one of the few architectures I do not use, I cannot in good conscience write an answer, since I feel your question needs specific registers and details that take me too long to datasheet out. But you can set a timer, which is clocked from your chips main clock with a division factor. Usually the division factor is chosen out of a set of options. You can then set what is called an interrupt on overflow. If the timer is 8bit, the result will be {CPU Frequency} / (256 * {clock_division}) times per second. You can then count those in 4 variables and determine which reached 1 second. \$\endgroup\$
    – Asmyldof
    Commented Dec 30, 2015 at 12:33

4 Answers 4


Unless the particular part you have in mind has four suitable hardware timers you will not be able to do it 'in parallel', however all is almost surely not lost. Chance are that for a battery charger almost 1 second and almost in parallel is just fine.

For example, set up a 5ms periodic interrupt and decrement four 8-bit registers inside the ISR (interrupt service routine) until they become zero.

Now you can write a routine that sets each of the registers to 200 and waits until they test as zero for the 1 second +/- 5ms delays.

In you are using C be sure to use the volatile keyword for things that are changed in the ISR and used elsewhere .. and more complexity rears its head when the registers are wider than the 8-bit PIC word (say you wanted 1ms resolution), but it's all very do-able.


The PIC18F4550 has 4 independent hardware timers, but depending on system clock frequency they may not all be able to time a full second. Anyway you actually only need 1 timer. The problem then becomes 'how can I share 1 timer between 4 tasks?'.

The next question is 'how can the PIC perform several tasks in parallel?'. The CPU can only execute 1 instruction at a time, so to multitask it has to do the same thing a human would - a bit of one task, a bit of the next etc. in a loop so that they all get an equal share of its time.

The timer can also be shared between tasks the same way you would if you only had a single clock to read. There are several ways to do this:-

  1. Set up the timer to overflow at a high rate, eg. 50 times per second (20ms period). Now give each task its own counter which is incremented on every timer overflow 'tick'. When a cell is inserted, turn on the power and reset its counter to zero. When the counter reaches 50, turn off the power etc.

The timer overflow could be captured in an interrupt which then increments each task counter (and reloads the timer if necessary), or you could simply poll the timer and wait for it to overflow at the start of the multitasking loop. Using interrupts may be more accurate if you need to reload the timer to get the required 'tick' time, but you must remember that as far as each task is concerned its counter could change at any time.

  1. Set up the timer so it overflows much slower than 1 second. When a cell is inserted the task reads the current time and stores it. On subsequent reads it subtracts the stored time from the current time, and when the difference reaches 1 second it turns the power off etc.

But what about if the timer overflows between reads? Then the task does what you would do with a 12 hour wall clock - if the result of the subtraction is negative then add the time it takes for the timer to overflow (same as you would add 12 hours to the wall clock time).

  1. Set the timer to overflow once per second, and execute each task at the same rate. When a task detects that a cell has been inserted it turns the power on - unless it is already on, in which case it turns the power off etc.

Of course a cell might be inserted at any time within the 1 second period, so the initial charging time will be a random period of less than 1 second. However each subsequent charge period will be locked into the 1 second cycle. Since NiMH cells take a long time to charge this shouldn't be a problem (I have a commercial 4 cell charger that uses this technique - it works perfectly!).

  • \$\begingroup\$ Dear Bruce Abbot, I have apologised for not formulating the question correctly. What I wish to do when a cellis inserted is to see if it is \$\endgroup\$
    – Fred Mah
    Commented Jan 2, 2016 at 8:37

First many thanks to the good people who took time reading my question.

I agree completely that explaining the analogue solutions really clouds the basic question.... Q1. I need to charge independently 4 NiMH 2AH cells, terminating using the voltage rise at the end of charge . I wish to sample every APPROXIMATE second, for this Cell charging application.

Sorry for a too wooly question ;-)



I have apologized for not formulating the question correctly. What I wish to do when a cell is inserted is to see if it is:- a) dead and will not charge; b) Been discharged to zero but will respond to recovery charging; c)it has discharged to the maker's recommended minimum, approx. 0.9V.... then monitor each cell for the end of charge rise and disconnect. At their recommended C/5 if charged correctly at about 6 hours to fully charged... So really the Bat Charger is all about long period of timing. many thanks Fred


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.