# implementing an hours counting algorithm

• 02-03-2009
droseman
implementing an hours counting algorithm
Hi

I am trying to implement an algorithm which will store the number of hours run of a product using a timer, I have this so far

set timer to time 1000 x 1ms
|
|
count
|
when count == 0
|
Interrupt

The ISR would do:

increment var_1s
|
if var_1s == 3600
|
increment var_1h

This would need to continue up to var_10000h, so how do I keep track of all the variables and know when I need to roll each one over and ensure that the var_1s keeps getting incremented whatever?

Any suggestions gratefully received

--dave
• 02-03-2009
matsp
I don't see why you need more than ONE variable (assuming it is 32-bit, if you have only 16-bit integers, then you need two) to hold values up to 99999 hours.

Since ISR's should be kept as short as possible, I would also try to move any logic that you can out of the ISR and into general code somewhere else. So for example the "if (var_1s == 3600)" should probably not be part of the ISR [1].

[1] However, this WILL add to the problems - as you need to update the var_1s each time it reaches 3600. If you do that outside the ISR, there is (small) chance that the ISR hits again and updates the var_1s at the same time as you are updating it.

--
Mats
• 02-03-2009
droseman
yes is a 32 bit processor - when I worked it out, I can count to 490,000 years (!) that should be enough...

The reason why I was going to use several variables is because eventually this data is going to be output in the format <number_of_units><number_of_tens> etc, and I thought that this would be easier as I then only have to fill up a struct with each variable, serialise it and send it.

I suppose that using one variable, i could then do mathematical operations on that number to generate my struct members that way

good idea?

--dave
• 02-03-2009
matsp
Yes, separate the presentation (how you display/send the numbers) and the implementation of the counting. Not only is it fairly easy to split a number into bits, but also, you can then CHANGE the presentation and not having to change the counting.

--
Mats
• 02-03-2009
droseman
thanks again mats, that was very useful

--dave
• 02-03-2009
matsp
Actually, you could simply count up the seconds, and get hours by dividing by 3600. An unsigned 32-bit integer gives you a a good 1 million hours. Half a million if you make it signed. So a 5-digit number of hours is viable and you have an order of magnitude or so to "overflow" into.

The advantage is, again, that you move the "interpretation" or "presentation" of the count into a higher level of the interface, and removing bindings between the low-level code and the presentation.

Oh, and by the standards of this site, we are almost neighbours! ;)

--
Mats
• 02-04-2009
droseman
That should work well - as the product is designed to work for 20 years, worst case is 175,200 hours. I shouldn't think that it would get anywhere near that though..

Quote:

The advantage is, again, that you move the "interpretation" or "presentation" of the count into a higher level of the interface, and removing bindings between the low-level code and the presentation.
I remember that from one of my previous questions here - I must be learning something !

Quote:

Oh, and by the standards of this site, we are almost neighbours!
yes, I was surprised to post a question on the internet and get an answer from just down the road..

You had much snow down there ?

--dave