Hello,
Please tell me, How to call Time() in milliseconds in Nordic nrf51822 ??
Hello,
Please tell me, How to call Time() in milliseconds in Nordic nrf51822 ??
Hi Rakesh
As mentioned by Endnode it is quite simple to have one of the TIMER modules return the time elapsed in milliseconds.
I made a quick example for this based on the template example in the SDK:
#define MY_TIMER NRF_TIMER1
#define MY_TIMER_IRQn TIMER1_IRQn
#define MY_TIMER_IRQHandler TIMER1_IRQHandler
static uint32_t my_timer_seconds;
static void my_timer_start(void)
{
// Reset the second variable
my_timer_seconds = 0;
// Ensure the timer uses 24-bit bitmode or higher
MY_TIMER->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
// Set the prescaler to 4, for a timer interval of 1 us (16M / 2^4)
MY_TIMER->PRESCALER = 4;
// Set the CC[0] register to hit after 1 second
MY_TIMER->CC[0] = 1000000;
// Make sure the timer clears after reaching CC[0]
MY_TIMER->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
// Trigger the interrupt when reaching CC[0]
MY_TIMER->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
// Set a low IRQ priority and enable interrupts for the timer module
NVIC_SetPriority(MY_TIMER_IRQn, 7);
NVIC_EnableIRQ(MY_TIMER_IRQn);
// Clear and start the timer
MY_TIMER->TASKS_CLEAR = 1;
MY_TIMER->TASKS_START = 1;
}
static uint32_t my_timer_get_ms(void)
{
// Store the current value of the timer in the CC[1] register, by triggering the capture task
MY_TIMER->TASKS_CAPTURE[1] = 1;
// Combine the state of the second variable with the current timer state, and return the result
return (my_timer_seconds * 1000) + (MY_TIMER->CC[1] / 1000);
}
static uint64_t my_timer_get_us(void)
{
// Store the current value of the timer in the CC[1] register, by triggering the capture task
MY_TIMER->TASKS_CAPTURE[1] = 1;
// Combine the state of the second variable with the current timer state, and return the result
return (uint64_t)my_timer_seconds * 1000000 + MY_TIMER->CC[1];
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
// Enable LED_1
nrf_gpio_cfg_output(LED_1);
// Enable and start the timer
my_timer_start();
while (true)
{
// Toggle LED_1 every second based on the time value returned by my_timer_get_ms()
nrf_gpio_pin_write(LED_1, (my_timer_get_ms() / 1000) % 2);
}
}
/** @} */
// Timer interrupt handler
void MY_TIMER_IRQHandler(void)
{
if(MY_TIMER->EVENTS_COMPARE[0])
{
MY_TIMER->EVENTS_COMPARE[0] = 0;
// Increment the second variable
my_timer_seconds++;
}
}
Just copy/paste the above code into the template project if you want to try it out.
Best regards
Torbjørn
Can this code work for nrf52832 as well?
Hi
Yes, this code should run nicely on the nRF52832.
Looking at the code I think I wrote it for the nRF52832 in the first place, since I set the interrupt priority like this:
NVIC_SetPriority(MY_TIMER_IRQn, 7);
On the nRF51822 the lowest IRQ priority (highest number) is 3.
The code should work fine on both the nRF51 and the nRF52 series, since the IRQ priority is masked internally before it is set (so 7 will become 3 on the nRF51).
Best regards
Torbjørn
Could you also help me in understanding how do I print the time(with millisecond accuracy) on uart terminal.
Are you using the nrf_log module?
I am using the nrf_log group in the project if that is what you meant. Check the image below if that is of any help.
I am using the nrf_log group in the project if that is what you meant. Check the image below if that is of any help.
If you are using the log module you can print integer numbers in a similar way to the printf function, so you could do something like this:
NRF_LOG_INFO("Time in milliseconds: %i", (int)my_timer_get_ms()
);
Hi! Sorry for replying to you late but I am unable to verify the results of the code in the logger or probably I'm not observing the results in the right place. Could you help me out since I'm new to this module.
Hi
Are you running the J-Link RTT Viewer software on your PC?
Then you should see all the output from the devkit, as long as the code is running and the kit is connected to the PC over USB.
Best regards
Torbjørn
Hi
I tried your solution but I'm unable to get past the following screen whose screenshot is attached along with this. Thank you
Hi
If you click on the "All Terminals" tab, do you see some output then?
The "Log" tab will only show RTT log information, not the actual output from the Nordic kit.
Best regards
Torbjørn