cancel
Showing results for 
Search instead for 
Did you mean: 

Timer program for 1us and 1 ms

ping2murali
Associate
Posted on November 26, 2014 at 15:54

Hi All,

I am having stm32f4discovery board with stm32f407vg processor and i am new to stm32f4  & also embedded programming. i tried delay using normal delay loop function calling method but i need to check timer interrupt for every 1us and also time delay for 1ms using timer. Can anyone help me to provide sample code for 1us delay using timer.

#stm32f4-timer-interrupt #timer
4 REPLIES 4
t2399
Associate II
Posted on November 26, 2014 at 16:25

 And? What the problem?  Just declare global variable and increment it every 1 us in interrupt. So there is System timer.

 Or I did'nt understand you?

ping2murali
Associate
Posted on November 26, 2014 at 16:41

Thanks brother, i really don't know how to configure timer as a 1us or 1ms delay.

I am using coocox , there are few sample programs and that programs are PWM configuration. Those programs are confusing me.  i need sample program for timer based interrupt program.

t2399
Associate II
Posted on November 26, 2014 at 17:00

For C laguage something like:

uint32_t timeCounter = 0;
int
main (
void
)
{
uint32_t previousTime;
// ...
if
( (timeCounter - previousTime) < x) {
// do what you want
}
previousTime = timeCount;
/// 
}
void
isrTimer( 
void
)
{
//... your code if need
counter++;
}

Or

http://pastebin.com/gqvJSk72

colored
Posted on November 26, 2014 at 17:27

Interrupting at 1 us (1MHz) is a fools errand.

For 1 ms consider looking at the SysTick examples in the firmware library.

If you want a timer to tick at 1MHz, you'd set the prescaler to (SystemCoreClock / 1000000)-1, for APB2 based timers. With a period of 1000 ticks at 1 MHz, you'd have an update interrupt at 1 ms

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Delay%20with%20timers%20on%20stm32F107&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=71]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FDelay%20with%20timers%20on%20stm32F107&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=71

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20Delay&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=46]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSTM32F4%20Delay&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=46

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20Free%20running%2032-bit%20timercounter&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&curren...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..