2009-02-23 07:15 PM
Toggle a pin with 3.57MHz frequency
2011-05-17 04:04 AM
I tried to make this using TIM2, but the max freq was 666.7 KHz only.
Notes: TIM2CLK is 36 MHz. TIM2 counter clock = TIM2CLK / (Prescaler +1) Period = TIM2 counter clock/MyFreq MyFreq = 3.57 MHz Prescaler = 9 TIM2 counter clock = 3.6 MHz Period = 1 Where are i am wrong? Code with TIM2 init: /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure ); TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE ); TIM_Cmd( TIM2, ENABLE ); Interrupt handler: void TIM2_IRQHandler(void) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); GPIOB->ODR ^= (1 << 12); //PB.12 GPIOA->ODR ^= (1 << 8); //PA.8 } [ This message was edited by: dimasusl on 23-02-2009 18:44 ]2011-05-17 04:04 AM
It appears you're trying to fire an interrupt at your target output clock rate (3.57 MHz). I'm sorry to say that I don't think the STM32 can handle interrupts that fast. In fact, I think you maxed out your ISR rate at 666 KHz.
2011-05-17 04:04 AM
So, it's work, but...
I setup TIM2 with CC2 in toggle mode (it's using pin PA.1) /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1; //tick with 7.2MHz TIM_TimeBaseStructure.TIM_Prescaler = 4; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure ); /* Output Compare Toggle Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; // CCR2_Val = 0 TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable); /* TIM IT enable */ TIM_ITConfig( TIM2, TIM_IT_CC2, ENABLE ); /* TIM2 enable counter */ TIM_Cmd( TIM2, ENABLE ); GPIO setup: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; <-- ! GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); And, interrupt handler: void TIM2_IRQHandler(void) { /* TIM2_CH2 toggling with frequency = 3.6 MHz */ if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); capture = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture + CCR2_Val); } } [ This message was edited by: dimasusl on 23-02-2009 21:42 ]2011-05-17 04:04 AM
Quote:
On 23-02-2009 at 20:34, Anonymous wrote: It appears you're trying to fire an interrupt at your target output clock rate (3.57 MHz). I'm sorry to say that I don't think the STM32 can handle interrupts that fast. In fact, I think you maxed out your ISR rate at 666 KHz. Maybe you are right, but how can i generate CLK = 3.57(3.6)MHz for my SAM module, in another way? P.S. My oscilloscope shows that this is possible :) [ This message was edited by: dimasusl on 23-02-2009 21:52 ]2011-05-17 04:04 AM
It's a programmer exercise. Take a look at any PWM example on a timer output pin. You'll never achive the rate you're looking for with an interrupt handler. You'll need the help of the timer peripheral. The PWM example will hsow you how. You'll need to experiment. You might not be able to achieve the exact frequency you want because the clock divisors will be small.
2011-05-17 04:04 AM
Quote:
On 24-02-2009 at 00:58, Anonymous wrote: It's a programmer exercise. Take a look at any PWM example on a timer output pin. You'll never achive the rate you're looking for with an interrupt handler. You'll need the help of the timer peripheral. The PWM example will hsow you how. You'll need to experiment. You might not be able to achieve the exact frequency you want because the clock divisors will be small. Thank you very much. Indeed, using the example of PWM, I have achieved the desired result without interruption. This is very good, since it works the rest of the hardware (eg, USB). P.S. But now I am well-mastered timers:)