cancel
Showing results for 
Search instead for 
Did you mean: 

Generating a simple square signal

mathias23
Associate
Posted on August 31, 2012 at 13:33

Hi at all,

I am using the STM32F4 i am trying to generate a simple square signal without using an interrupt (about 2MHz). Therfore i tried using Timer 3 in OC-Toggle mode. See below my initialisation for Timer 3:

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Enable Pin
// TIM3 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// GPIOB Configuration: TIM3 CH2 (PB5)
GPIO_InitStructure.GPIO_Pin = TLC_GSCLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(TLC_PORT, &GPIO_InitStructure);
// Connect TIM3 pins to AF2
GPIO_PinAFConfig(TLC_PORT, TLC_GSCLK_PIN, GPIO_AF_TIM3);
// PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 28000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0; //No prescaler
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
TIM3->CCR2 = 1;

When I debug my code I can see the counter is actually running. But on TLC_Port (which is Port B) TLC_GSCLK_PIN (which is Pin 5) nothing happens. Did I forget something? Did I do something wrong with the alternate function mapping? It would be great if you could help me with that issue... Thanks in advance. Mathias #stm32f4-square-signal-toggle
2 REPLIES 2
Posted on August 31, 2012 at 13:43

I'd use PWM, but one problem here is that

GPIO_PinAFConfig(

) takes an index (GPIO_PinSource) not a bit vector (GPIO_Pin) parameter. Going to clock significantly faster than 2 MHz
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mathias23
Associate
Posted on August 31, 2012 at 14:32

Yes thanks,

that was the problem!