implementing an hours counting algorithm

This is a discussion on implementing an hours counting algorithm within the C Programming forums, part of the General Programming Boards category; Hi I am trying to implement an algorithm which will store the number of hours run of a product using ...

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    Question 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
    |
    load timer
    |
    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    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
    Last edited by droseman; 02-03-2009 at 06:52 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    thanks again mats, that was very useful

    --dave

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    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..

    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 !

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. 40 hours and counting..
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 03-19-2003, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21