cancel
Showing results for 
Search instead for 
Did you mean: 

PWM not working on TIM2 channel 1 on STM32H753XIH6

Npiet.1
Associate II

We are using PWM here to change the brightness of LCD display, but PWM is not working , as of now I don't have CRO's as I'm working remotely. Is there anything wrong in the below code??

#include "stm32h7xx.h"
 
void PwmGpioInitalize()
{
  /* Timer GPIO Pin Enabling  */
  RCC->AHB4ENR   |= RCC_AHB4ENR_GPIOAEN;
  GPIOA->MODER   |= (2<<(2*15));// PA15 - 10 Alternate Function mode;
  GPIOA->PUPDR   &= ~((1<<30)|(2<<30)); //PA15 - 00 NO PULL UP or PULL DOWN
  GPIOA->OTYPER  &= ~(1<<15); // 0 - PA15 Selected Push Pull type
  GPIOA->OSPEEDR &= ~((1<<30)|(2<<30));  // PA15 - 00 Low Speed Selected
  GPIOA->AFR[1]  |= (1<<(4*7)); // Timer2 PWM Channel 1 selected as Alternate function1
}
 
void BacklightPinGpioInitalize()
{
  /* Backlight GPIO Pin Enabling  */
  RCC->AHB4ENR   |= RCC_AHB4ENR_GPIOEEN;
  GPIOE->MODER   |= (1<<(2*2)); //PE2 - Output Mode
  GPIOE->PUPDR   &= ~((1<<4)|(2<<4));// NO PULL UP or PULL DOWN
  GPIOE->OTYPER  &= ~(1<<2);// PE2 Selected Push Pull type
  GPIOE->OSPEEDR |= (2<<(2*2)); // HighSpeed is selected
}
 
/** Timer_Clock = 2 x  APB2_clock = 200 MHz */
/** PWM_freq = Timer_Clock /(Period x (Prescaler + 1))*/
/** PWM_freq = (200*1000000)/(40000*1) = 5000 Hz*/
 
void PwmTimerInit(void)
{
  RCC->APB1LENR |=RCC_APB1LENR_TIM2EN; 					    // Timer2 Clock Enable
  PwmGpioInitalize(); 								    // Initialize the PWM Channel 1 GPIO - PA15
  TIM2->CCMR1  = 0X00000000;								// y De initialize
  TIM2->CR1    = 0x0000; 									// No clock division, Up counter mode,Edge Aligned mode
  TIM2->PSC    = 0;             		 					// Precale value TODO:Needs to fix the PWM Frequency
  TIM2->ARR    = 40000 ;                					// Set to maximum
  TIM2->CCR1   = 20000 ; 						    		// 50 % duty cycle set
  TIM2->CCMR1 |= (TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2);		// y 0110 - Pwm Mode 1 is selected
  TIM2->CCMR1 |= TIM_CCMR1_OC1PE; 						    // y Preload register enabled
  TIM2->CR1   |= TIM_CR1_ARPE;    						    // Auto preload register enabled
  TIM2->CR1   &= ~TIM_CR1_DIR;                              //ch Countermodedirup
  TIM2->CCER  &= ~TIM_CCER_CC1P; 						    // Output Polarity high
  TIM2->CCER  |= TIM_CCER_CC1E; 							// Signal output enable on the pin
  TIM2->CR1   |= TIM_CR1_CEN; 								// Timer Enable
}
 
void LcdSetBrightness( uint8_t BrightnessValue )
{
  uint32_t Pulse;
   /* Setting up pulse value for brightness */
  Pulse =  (uint32_t)((40000 * BrightnessValue) / 100);
 
  /* Set the Capture Compare Register value */
  TIM2->CCR1 = Pulse;
}
 
void LcdBacklightOn( void )
{
  GPIOE->ODR |= 1<<2;
}
 
void LcdBacklightOff( void )
{
  GPIOE->ODR &= ~(1<<2);
}
 
int main()
{
  BacklightPinGpioInitalize();
  PwmTimerInit();
  LcdBacklightOn;
  LcdBacklightOff;
  LcdSetBrightness(60);
}

0 REPLIES 0