How to deal with a wrapping counter in embedded C

后端 未结 11 883
时光取名叫无心
时光取名叫无心 2021-02-05 14:23

I need to deal with a counter that gives me ticks for my application. The counter is 32bits so what i need to know is how to deal with it when it wraps. for example:

I h

11条回答
  •  孤独总比滥情好
    2021-02-05 15:12

    The simplest way to do this is to make an "epoch counter", that explicitly counts rollovers. (Example: you have a hardware counter that counts seconds 0..59. Your epoch counter would count minutes, by incrementing each time it noticed that the seconds counter had rolled over.)

    Your future_scheduler function then reads the current epoch and time,and computes a new epoch and time for your event.

    Alternatively, you could just punt, and make your timing function count your event schedules down to zero on each timer tick.

提交回复
热议问题