cancel
Showing results for 
Search instead for 
Did you mean: 

Hello, I am new to STM32 and I am trying to generate PWM signal of 10 KHZ and 50% duty cycle and i am not able to achive it and I am using STM32L476VG DISCO board and for reference I have attached the code. Thank you

PKuma.23
Associate II

/*CODE to generate PWM signal of 50%*/

#include "stm32l476xx.h"

#include "pwm.h"

void pwm_init(void){

/*RCC configuration*/

RCC->AHB2ENR|=RCC_AHB2ENR_GPIOEEN;//ENABLE GPIOE

RCC->APB2ENR|=RCC_APB2ENR_TIM1EN;// ENABLE CLOCK ACCESS TO TIM1

//RCC->APB1ENR1|=RCC_APB1ENR1_PWREN;// ENABLE POWER INTERFACE CLOCK

/*GPIO configuration*/

GPIOE->MODER &= (~(1U<<16)); // GPIOE selected as alternate function mode

GPIOE->MODER |= (1U<<17);

//GPIOE->MODER|=GPIO_MODER_MODE8; // GPIOE selected as analog function

//GPIOE->ODR|=(1U<<8);

//GPIOE->IDR|=(1U<<8);

GPIOE->AFR[1]|=(1U<<0);

/*Timer configuration*/

TIM1->PSC=0; //set prescalar to 0

TIM1->ARR=8000-1; // auto reload register

  TIM1->SR |= (1U<<0)|(1U<<1)|(1U<<2)|(1U<<3)|(1U<<4);

  TIM1->BDTR|=(1U<<13);

  TIM1->BDTR|=(1U<<15);

  TIM1->DMAR|= (0X01U);

TIM1->CCMR1|=(1U<<5);/* Output compare 1 mode as PWM MODE 1*/

TIM1->CCMR1|=(1U<<6);/* Output compare 1 mode as PWM MODE 1*/

  // TIM1->CCMR1|=(1U<<4);/* Output compare 1 mode as PWM MODE 1*/

//TIM1->CCMR1|=(1U<<5);/* Output compare 1 mode as PWM MODE 1*/

//TIM1->CCMR1|=(1U<<1);/*output compare preload enable*/

TIM1->CCR1|=(0X0U);//Capture compare register*/

TIM1->CCER|=(1U<<2); // Enable compare

//TIM1->CCER|=(1U<<0);//Enable CH1 compare mode*/

TIM1->CNT=0; // count register

TIM1->CR1|=(1U<<0); // enable timer1

}

Kindly help regarding this....

5 REPLIES 5
MM..1
Chief III

USE MX generated HAL code and learn or examples from folder..., after 10 years you can try start prog with registers...

In your code is big amount of errors

PKuma.23
Associate II

Hi,

Thanks for your feed back....

But it was needed to write in register level,so I requested

> TIM1->CCR1|=(0X0U);//Capture compare register*/

You need to set CCR1 to ARR/2 to have 50% PWM.

If it still does not work, read out and check/post content of TIM and relevant GPIO registers.

JW

PS. To set individual bits or even bitfields, it's better to use symbols from the CMSIS-mandated device header, e.g.

TIM1->CR1 |= TIM_CR1_CEN; // enable timer 1

PS2.  

TIM1->BDTR|=(1U<<13);

this sets TIMx_BDTR.BKP, break polarity, but this is ignored as you don't set break enable, BKE. But setting it opens other ways why the TIM output might not work, so just leave it for now as it is.

PKuma.23
Associate II

Waclawek.jan Thank you and thanks for your time and patience.....

Note that this is not really PWM - it is just a fixed duty-cycle square wave.

PWM = Pulse Width Modulation, which means that the pulse-width is being modulated - ie, changed.

So a fixed duty-cycle square wave isn't really PWM