cancel
Showing results for 
Search instead for 
Did you mean: 

timer in str911fm44

mariusz2
Associate II
Posted on January 23, 2009 at 18:35

timer in str911fm44

8 REPLIES 8
mariusz2
Associate II
Posted on May 17, 2011 at 09:42

Can my help. How configure any timer, to time mensure from internal clock SCU. I need interrupt from timer, constant period without PWM, PULSE, and other hardware events. please example code

markmcwired9
Associate II
Posted on May 17, 2011 at 09:42

Hi MariuszS,

I use this code to generate a 10mSec interrupt:

>>>>>> This code goes in the setup part:

// TIM0 Structure Initialization ---------------------------------------------

/* Timer calculation:

Period = (t * fPCLK / tPRESC) - 5

Where:

t = Desired output compare period (seconds)

fPCLK = Internal clock frequency

tPRESC = Timer clock prescaler

Ex: Period = (0.01 * 48Mhz / 256) - 5 = 0x74E

*/

TIM_StructInit(&TIM_InitStructure);

TIM_InitStructure.TIM_Mode = TIM_PWM;

TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB;

TIM_InitStructure.TIM_Prescaler = 0xFF;

TIM_InitStructure.TIM_Pulse_Level_1 = TIM_HIGH;

TIM_InitStructure.TIM_Period_Level = TIM_LOW;

TIM_InitStructure.TIM_Pulse_Length_1 = 0xFF;

TIM_InitStructure.TIM_Full_Period = 0x74E; //0xFFF max value for 22mSec

TIM_DeInit(TIM0);

TIM_Init (TIM0, &TIM_InitStructure);

TIM_ITConfig(TIM0, TIM_IT_TO, ENABLE); // Enable the Timer Overflow interrupt

TIM_CounterCmd(TIM0, TIM_START); // Start the Timer0 counter

// VIC configuration ---------------------------------------------------------

VIC_DeInit();

VIC_Config(TIM0_ITLine, VIC_IRQ, 4);

VIC_ITCmd(TIM0_ITLine, ENABLE);

>>>>>> This code goes in the ''91x_it.c'' file:

void TIM0_IRQHandler(void)

{

TIM0_IRQHandlerInvoked();

}

>>>>>> This code goes in the subroutine called by the interrupt:

/*******************************************************************************

* Function Name : TIM0_IRQHandlerInvoked

* Description : this routine is invoked when the Timer0 has generated an

* interrupt

*******************************************************************************/

void TIM0_IRQHandlerInvoked(void)

{

TIM_ClearFlag(TIM0, TIM_FLAG_TO); //Clear the TIM_TO flag for the next interrupt

if(pulse)

{

GPIO_WriteBit(GPIO6, GPIO_Pin_1, Bit_SET);

GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);

pulse = 0;

}

else

{

GPIO_WriteBit(GPIO6, GPIO_Pin_1, Bit_RESET);

GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);

pulse = 1;

}

}

I just toggle Leds when the interrupt happens. I hope this will help you.

Mark

mariusz2
Associate II
Posted on May 17, 2011 at 09:42

Very thanks. My problem was in library functions 91x_Lib.c. Copmiler not handled:

#ifdef _TIM0

TIM0 = (TIM_TypeDef *)TIM0_BASE;

#endif /* _TIM0 */

all function ''timers'' need this registration in debug mode

Is possible use function in no debug mode? How?

gkruger
Associate II
Posted on May 17, 2011 at 09:42

Hello,

I seem to get exactly double the period that I should. I used Mike's code as a test. And I get 20ms not 10ms.

My first thought was that my clock speed is wrong, I am running at 48MHz (SCU_PLLFactorsConfig(192, 25, 3);)

If I call SCU_GetMCLKFreqValue() I get 48000 back.

Is there anything else that I am missing?

Thanks,

Gert

amira1
Associate II
Posted on May 17, 2011 at 09:42

Hello GKruger,

Please find attached a Timer example working with fPLL=48MHz. To calculate the Timer frequency :

fTIM=fPCLK/(tPRESC*(OC2R-5))

So you will get from the example fTIM=45.8Hz.

I hope this will help,

With best regards,

mirou

jtremblay
Associate
Posted on May 17, 2011 at 09:43

I just want to keep you informed that the formula to compute fTIM has an error. You should read fTIM = fPCLK / (tPRESC * (OCR2 + 5) ). I have tested it with my 100uS tick timer and it is very accurate.

Jim

lazycat1049
Associate II
Posted on May 17, 2011 at 09:43

Hi,

Sorry can anybody tell mi why pulse length is 0xFF in the above code?

Thanx in advance!!

Lazycat

boris1
Associate
Posted on May 17, 2011 at 09:43

2 lazycat1049:

pulse length is 0xFF because we need divide prescale value = 256. Look at Reference manual at TIM_CR2 register

(CC[7:0]: Clock Control.

These bit are written by software to select the frequency of the timer clock applied

when internal clock is selected (ECKEN=0):

00h: fPCLK / 1

01h: fPCLK / 2

...

FFh: fPCLK / 256

)null