cancel
Showing results for 
Search instead for 
Did you mean: 

using PWM function in STM32F302R8 nucleo board. [ SOLVED] I can't start PWM function . most of my code has been made by using Cube. I see nothing in ouput PC2 and PF0 for TIM1 chanel 3 and 3N.

Joel14
Associate II

I am French.

There is now more than one week that I try, see youtube and some comments on forum. I need your experience for solving the initialisation of a "simple" PWM.

Thanks in advance.

Joël

here under my code. in the main I have launch : PWM1_CH3N_setup();

void PWM1_CH3N_setup(void)

{

// STM32F302R8

// Clock Frequency = SystemCoreClock = 64 000 000 Hz

  // Period = 38000 - 1 = 37999

  // Prescaler = (SystemCoreClock/38000) - 1

// Pulse = 190000;

  // ClockDivision = 0

  // Counter direction = Up

// for try I use 1hz

/* Set TIM1 clock source as internal clock */

LL_RCC_SetTIMClockSource(LL_RCC_TIM1_CLKSOURCE_PLL);

/* TIM1 clock enable */

LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);

/* GPIO config */

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);

GPIO_InitStruct.Pin = PF0_PWM38khz_Pin ;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; // proposed by CUBE

HAL_GPIO_Init(PF0_PWM38khz_GPIO_Port, &GPIO_InitStruct);

GPIO_InitStruct.Pin = PC2_PWM38khz_Pin ;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF2_TIM1; // proposed by CUBE

HAL_GPIO_Init(PC2_PWM38khz_GPIO_Port, &GPIO_InitStruct);

/* TIM1 init function */

  

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 64000;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 1000;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)

  {

/* Initialization Error */

error_data = 100 ;

Error_Handler();

  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig) != HAL_OK)

  {

/* Initialization Error */

error_data = 101 ;

Error_Handler();

  }

  if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)

  {

/* Initialization Error */

error_data = 102 ;

Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 500;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

  if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

  {

/* Initialization Error */

error_data = 103 ;

Error_Handler();

  }

  if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)

  {

/* Initialization Error */

error_data = 104 ;

Error_Handler();

  }

}

6 REPLIES 6

In debugger, read out and check/post the relevant GPIO and TIM registers content.

JW

Joel14
Associate II

here under the debug result register.

GPIO PC2 

MODER1[bits 3-2] 0x3

AFRL2 [bits 11-8] 0x2

AFRH10 [bits 11-8] 0x6

AFRH11 [bits 19-16] 0x6

IDR2 [bits 2] 0x1

OSSPEEDR1 [bits 3-2] 0x00

-

-

GPIO PF0 

MODER0[bits 1-0] 0x2

AFRL0 [bits 3-0] 0x6

OSSPEEDR1 [bits 3-2] 0x00

-

-

TIM 1

CR1 all bits = 0x00

CR2 all bits = 0x00

SMCR all bits = 0x00

DIER all bits = 0x00

SR  UIF [bit 0] = 0x1 and all others bits 0x00

CCMR1_output all bits = 0x00

CCMR1_input  all bits = 0x00

CCMR2_output all bits = 0x00

CCMR2_input  all bits = 0x00

CCER all bits = 0x00

CNT all bits = 0x00

PSC [bits 15-0] = 0xFA00

ARR [bits 15-0] = 0x03E8

RCR [bits 15-0] = 0x0000

CCR1[bits 15-0] = 0x0000

CCR2[bits 15-0] = 0x0000

CCR3[bits 15-0] = 0x0000

CCR4[bits 15-0] = 0x0000

BDTR all bits = 0x00

DCR all bits = 0x00

DMAR all bits = 0x00

CCMR3 all bits = 0x00

CCR5 all bits = 0x00

CCR6 all bits = 0x00

OR all bits = 0x00

> GPIOC

> MODER1[bits 3-2] 0x3

We are talking about PC2, so would need to have a look at MODER2 (bits 5-4 of GPIOC_MODER).

Does this mean they are 0x0 ? Then they are set as In, not as AF - AF is 0x02. As I've said I don't use Cube so don't quite understand why, but maybe you want to check the values used for setting up PC2. Make sure, you fill in all fields of the GPIO_InitStruct, and/or zero it explicitly.

GPIOC_AFRL appears to be set properly, though. I don't know why did you post GPIOC_AFRH's bits - those two appear to map PC10+PC11 to SPI3SCK+SPI3MISO, isn't that what you intended to do?

GPIOF appears to be OK.

TIM has set ARR and PSC, but that's all - you need to have set CCRx for channel X to a value between 0 and ARR, CCMRx set for output compare/on of the PWM modes, channel output(s) enabled in CCER, and timer/counter enabled in CR1.CEN. Again, I don't use Cube so I can't decipher your code, but I'd perhaps check if TimHandle is set properly, as that appears to be a common denominator of all called functions except the function which sets the "timebase" i.e. ARR and PSC.

JW

Joel14
Associate II

JW,

I have check, MODER2 [bits 5-4] = 0x2. i think now PC2 is ok no?

yes, my SPI3 is set

I understand that the structure "sConfigOC" is wrong because the "sConfigOC.pulse = 500" not 0x0 .

Thanks, Joël

> I have check, MODER2 [bits 5-4] = 0x2. i think now PC2 is ok no?

Yes that's what's expected.

JW

Joel14
Associate II

I JW;

I have found with your questions , "&Timhandle" is wrong , I need to use "&htm1"

I have tested and now I have output on PF0 . This is that I need . I don't need PC2 . I have used PF0 and PC2 for tring only.

here under the final code for 38khz PWM that works.

void PWM1_CH3N_setup(void)

{

// STM32F302R8

// Clock Frequency = SystemCoreClock = 64 000 000 Hz

// PWN needed = 38000 hz

/* Set TIM1 clock source as internal clock */

LL_RCC_SetTIMClockSource(LL_RCC_TIM1_CLKSOURCE_PLL);

/* TIM1 clock enable */

LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);

/* GPIO config */

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);

GPIO_InitStruct.Pin = PF0_PWM38khz_Pin ;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;

HAL_GPIO_Init(PF0_PWM38khz_GPIO_Port, &GPIO_InitStruct);

/* TIM1 init function */

  

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 20;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 160;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)

  {

/* Initialization Error */

error_data = 100 ;

Error_Handler();

  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)

  {

/* Initialization Error */

error_data = 101 ;

Error_Handler();

  }

  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)

  {

/* Initialization Error */

error_data = 102 ;

Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 80;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

  {

/* Initialization Error */

error_data = 103 ;

Error_Handler();

  }

  if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3) != HAL_OK)

  {

/* Initialization Error */

error_data = 104 ;

Error_Handler();

  }

}

Thanks you very much JW.

bye bye.

Joël