cancel
Showing results for 
Search instead for 
Did you mean: 

TIM8 PWM Issue

officialkesh
Associate III
Posted on March 31, 2016 at 15:49

Hello ,

I have config to TIM8 to generate 12 MHz clock signal  on PC8 using Timer8 Channel . For this I have configured as per below . When I checked on DSO , I am not getting any signal . What could be the reason for this ?

int main(void)

{

             /* TIM Configuration */

            TIM_Config();

  /* TIM clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 6 ;       //for 2.048 MHz ---> use 40;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC3Init(TIM8, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM8, ENABLE);

  TIM_CtrlPWMOutputs(TIM8, ENABLE);

  /* TIM8 enable counter */

  TIM_Cmd(TIM8, ENABLE);

  while (1){}

}

void TIM_Config(void)

{

             GPIO_InitTypeDef GPIO_InitStructure;

  /* GPIOC and GPIOB clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);

 

  /* GPIOC Configuration */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;

  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_UP ;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Connect TIM8 pins to AF2 */  

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);

}

#output #timer #pwm #stm32f4 #discovery
9 REPLIES 9
Posted on March 31, 2016 at 17:09

int main(void)
{
/* TIM Configuration */
TIM_Config();
/* TIM clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 12000000) - 1; // 12 MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disble;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
/* TIM8 enable counter */
TIM_Cmd(TIM8, ENABLE);
while (1){}
}
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC and GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);
/* GPIOC Configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
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_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM8 pins to AF2 */ 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8); // PC8 TIM8_CH3
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
officialkesh
Associate III
Posted on April 01, 2016 at 07:46

Clive1 ,

Thanks for reply but I am not getting output signal on PC8 .

(1) I have verified  - PC8 is only configured TIM8 , Channel 3 in our code .

(2) What else could be reason for not getting output signal ?

Posted on April 01, 2016 at 19:14

Seemed to be viable here, I cleaned it up a little more and just bench tested this to confirm I can see 6 MHz (12 MHz toggle rate). If you don't check you are look at the right pin, or that there isn't something else going.

/**************************************************************************************/
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);
/* GPIOC Configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; // 2,25,50, or 100
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM8 pins to AF2 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8); // PC8 TIM8_CH3
}
/**************************************************************************************/
int main(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM Configuration */
TIM_Config();
/* TIM clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 12000000) - 1; // 12 MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; // 6 MHz output
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
/* TIM8 enable counter */
TIM_Cmd(TIM8, ENABLE);
while (1){}
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
officialkesh
Associate III
Posted on April 04, 2016 at 07:57

clive1 ,

Scope is connected at proper MCU pin only .Need help further from your side . I have attached system_stm32f4xx.c and startup_stm32f4xx.s file .

Please give a one look and let me know what could be wrong with this set up .

________________

Attachments :

startup_stm32f4xx.s : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzo3&d=%2Fa%2F0X0000000bbl%2FWWcIo.34SJ.IvYoFotDjaTwV83X_lxkrMHCtP40_04E&asPdf=false

system_stm32f4xx.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0dZ&d=%2Fa%2F0X0000000bbj%2FeNB2mWzRvKcYxxLpNeeO00zNveOnGvJ9t7n6qjYImNc&asPdf=false
Posted on April 04, 2016 at 12:30

Confirm that the code is running in a debugger. I tested functionality on STM32F4-DISCO

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
officialkesh
Associate III
Posted on April 06, 2016 at 07:38

clive1 ,

In TIM_OC3Init() ,

/* Set the Capture Compare Register value */

 TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;  line was creating issue .

So that CCR3 has initialized to some other value . After this if we assign value less than 3000 ,timer clock was not generating .

Fills each TIM_OCInitStruct member with its default value.

TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct) needs to used before TIM_OC3Init() API .

The code that you have given was working with Keil IDE while it was not working with gcc tool-chain.

Any idea about this ?

- Pinkesh

Walid FTITI_O
Senior II
Posted on April 06, 2016 at 15:37

Hi pinkesh,

Please, give us more details about the device/hardware your using , the system clock configuration ...

-Hannibal-
Walid FTITI_O
Senior II
Posted on April 06, 2016 at 16:02

Hi pinkesh,

Try to copy and paste code in the main.c file of a template project in the STM32 standard library and it will work fine ( I have tested it with ST' gcc compiler system workbench SW4STM32). Maybe you have some extra code lines that makes problem with gcc. 

-Hannibal-
Posted on April 06, 2016 at 17:06

I don't know, and really can't throw resources at it, it works here, the structures are fully initialized. Perhaps you can use PWM mode, and program with a 50/50 duty?

What version of GNU/GCC, some have given me headaches in the past (4.9) where viable code has been broken by the code generator/optimizer.

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