Systick Delay functions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-07 2:31 AM
Hi folks,
I have had some code working for quite a while now using the sytick to create delay to the order of ms. This was made up of the following functions:static uint32_t TimingDelay;
void Delay( uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
With the systick config setup with the systemcoreclock/1000.
This all worked fine. What I am trying do now, is to introduce a us delay. I thought it should be as simple as changing the systick config to systemcoreclock/1000000, and creating a Delay_us() function, and then modifying my Delay() function to
TimingDelay = nTime*1000;
This seems to encounter some issues where it never seems to leave the Delay function. The Delay_us function seems to work fine. Has anyone else ever implemented a similar idea?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-07 4:10 AM
I thought it should be as simple as changing the systick config to systemcoreclock/1000000,
...
This seems to encounter some issues where it never seems to leave the Delay function.
An interrupt frequency of 1MHz (one per microsecond) is just cluttering your MCU. A M4 core might still have some cycles left, but M0 cores less so. If you need the usec delay rarely (e.g. less than ever millisecond), use a timer you configure to 1.0 us cycle time, and the delay time as counter value. And if you need precision in the low usec range, measure the setup runtime, and subtract it from the counter value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-07 4:31 AM
As indicated interrupting a 1 MHz is ridiculous. You're going to saturate the processor with useless work.
If you need tight timing, consider a spin loop looking at DWT_CYCCNT, or other rapidly clocked free running timer. These also have the benefit of being usable in interrupts, although you'd generally want to avoid blocking.Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-07 11:27 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-08 5:37 AM
HAL_Delay(msec);
Dude said microsecondsUp vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-09 1:31 AM
Ah of course! Feeling a bit stupid now. Have now setup TIM2 but it doesn't seem to be working (this is the first time I've played with timers). My code is as follows:
void Timer_Setup(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //ms
TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//TIM_Cmd(TIM2,ENABLE);
}
void Delay_us( int nTime)
{
u16 counter=nTime&0xffff;
TIM_Cmd(TIM2,ENABLE);
TIM_SetCounter(TIM2,counter);
while(counter>1)
{
counter=TIM_GetCounter(TIM2);
}
TIM_Cmd(TIM2,DISABLE);
}
Why wouldn't the Delay_us function be working. All it does is keep returning a counter value equal to nTime, as if the timer were not counting down properly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-12 7:44 AM
Anyone able to point out what I'm doing wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-01-12 8:06 AM
Nothing is immediately jumping out to me. Could it be the initialization code isn't run, or that it's not preloading?
TIM2 is 32-bit, you could just let it up count and do a B-A type comparison.Up vote any posts that you find helpful, it shows what's working..
