2009-01-23 09:35 AM
2011-05-17 12:42 AM
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
2011-05-17 12:42 AM
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. Mark2011-05-17 12:42 AM
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?2011-05-17 12:42 AM
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, Gert2011-05-17 12:42 AM
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, mirou2011-05-17 12:43 AM
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.
Jim2011-05-17 12:43 AM
Hi,
Sorry can anybody tell mi why pulse length is 0xFF in the above code? Thanx in advance!! Lazycat2011-05-17 12:43 AM
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