N/APosted on - 07/21/2011
I want to make a code for a clock in which I think the counter will be used in such a way that after every 12 counts, it starts its counting again from 1 and it should also maintain seconds and hours separately. Again, all should be linked with each other. I tried designing the code but I am unable to maintain the seconds' counter loop with the hours and the minute loop.
Can anybody will help me through this?
Designing code for a clock
Sorry, but what do you mean by maintaining the seconds and hours separately? Do you mean that there should be a timer for both seconds and hours and they check each other ever so often?
If not, this is how you may do it:
You can use the Sleep(n) function where n is the millisecond you want to delay. So, since your second counter will increase every second, you can use Sleep(1000). Simply keep looping Sleep(1000) and increment a counter. Every time the counter hits 60, increment a secondary counter and reset the primary timer. And every time the secondary counter hits 60, you increment a third counter and reset the secondary. And finally every time the third timer hits 12, it just resets. Remember to include the header "timer.h"
If yes, then maybe you might have to do multithreading to maintain two delays. However, this might be rather inaccurate depending on the implementation of multithreading you are using, the way your program is given processing time and various other factors.
Alternatively, have you considered using system clock instead? Simply poll the system clock to capture every second tick and code accordingly to it.