I want a efficient C code for my microcontroller to get the elapsed time since reset? just like aurdino has built in function millisec(). Assume the microcontroller be 8051, AVR and ARM7.
Thanx in advance..
I want a efficient C code for my microcontroller to get the elapsed time since reset? just like aurdino has built in function millisec(). Assume the microcontroller be 8051, AVR and ARM7.
Thanx in advance..
You need to set up a timer and an interrupt and count the timer interrupts since reset.
The code will be different not just for each processor core type but for each different implementation of that core.
Is this homework or do you have a serious requirement - if so it would help if you described the problem in a lot more detail, like processor type, precision required etc etc.
MK
Mike,
there are many cases I see that the simple call of the time function always return the number of microseconds (in a long integer format, usually) from the last micro controller power-on. Or I am wrong ?
Enrico
Since the OP said C and didn't specify an OS I'm assuming there isn't any code running that the OP didn't write or explicitly link.
8051 s and AVRs don't have hardware on-timers - some ARM7 s might but CortexM cores don't (they do have Systick timer which you can code to do this.)
MK
Thanks Mike,
Instead I have always got this fact as a sort of assumption. So there are some micro that doesn't "export" their ticks count based on the clock? My assumption was based on that time.h and the related C standard library is always available (maybe not?) on every compiler. http://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html
Then sure not (very few as a matter of fact) processors / boards has a more complex time function than the bare number of clocks from the power on.
Enrico
Here's some code for an STM32F405
uint32_t g_tick_count = 0;
RCC_ClocksTypeDef RCC_Clocks; | // global structure allows access to clock frequencies | |||||
void init(void)
{
RCC_GetClocksFreq(&RCC_Clocks); // get the clock frequencies, config done in startup - useful for debugging so main clocks are in global which is easily monitored
SysTick_Config(RCC_Clocks.HCLK_Frequency / SYS_TICKS_PER_SECOND); // 1000 ticks per second independent of actual clock frequency
// other stuff
}
void SysTick_Handler(void) // gets called once every ms
{
g_tick_count++; // system timer incremented here
if (flg_adc_running == true) // other code to do with this projects specifics
{
do_data_processing();
}
if (fsm_timer != 0)
{
fsm_timer--;
}
}
Of course this system doesn't support the time.h library .
MK
Here's some code for an STM32F405
uint32_t g_tick_count = 0;
RCC_ClocksTypeDef RCC_Clocks; | // global structure allows access to clock frequencies | |||||
void init(void)
{
RCC_GetClocksFreq(&RCC_Clocks); // get the clock frequencies, config done in startup - useful for debugging so main clocks are in global which is easily monitored
SysTick_Config(RCC_Clocks.HCLK_Frequency / SYS_TICKS_PER_SECOND); // 1000 ticks per second independent of actual clock frequency
// other stuff
}
void SysTick_Handler(void) // gets called once every ms
{
g_tick_count++; // system timer incremented here
if (flg_adc_running == true) // other code to do with this projects specifics
{
do_data_processing();
}
if (fsm_timer != 0)
{
fsm_timer--;
}
}
Of course this system doesn't support the time.h library .
MK