cancel
Showing results for 
Search instead for 
Did you mean: 

B-L072Z-LRWAN1 : PWM generation for LED dimming

cpc
Associate II

Hi,

I wanted to generate PWM using timer on B-L072Z-LRWAN1 board for LED Dimming.

I searched on the forum and web for this but I am unable to get any example for PWM on B-L072Z-LRWAN1 .

Which LED I can use for this purpose?

Can any one help me with this?

2 REPLIES 2
Piranha
Chief II

Brain is for thinking!

  • The board has MCU on it.
  • MCU has timer peripherals.
  • Timer peripherals can generate PWM on channel output.
  • LED has to be connected to timer channel output.
  • Board user manual has schematic where LED connections are shown.

AN4013 and AN4776 are good helpers for this task.

cpc
Associate II
void pwm_setvalue(uint16_t value)
{
	TIM_OC_InitTypeDef sConfigOC;
	sConfigOC.OCMode = TIM_OCMODE_PWM2;
	sConfigOC.Pulse = value;
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
}
 
void timer21_init(void)
{
   TimHandle.Instance = TIM21;
   TimHandle.Init.Prescaler = 0;
   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
   TimHandle.Init.Period = 3000;
   TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   HAL_TIM_PWM_Init(&TimHandle);
}
 
 
void gpio_tim_init(void)
{
	  /* Enable the GPIO_LED Clock */
	  __HAL_RCC_GPIOA_CLK_ENABLE();
 
	  /* Configure the GPIO_LED pin */
	  GPIO_InitStruct.Pin = GPIO_PIN_13;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  GPIO_InitStruct.Alternate = GPIO_AF6_TIM21;
 
	  //HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
	  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}
 
 
int main( void )
{
    HAL_Init( );
 
    SystemClock_Config( );
 
    HW_Init( );  
 
    gpio_tim_init();
 
    timer21_init();
 
 
 
    while(1)
    {
    		HAL_Delay(100);
    	    if(pwm_value == 0)
    		{
    			step = 100;
    		}
 
 
    	    if(pwm_value == 3000)
    		{
    			step = -100;
    		}
 
    	    pwm_value += step;
    	    pwm_setvalue(799);
 
}
    

Could u tell me what's wrong in this code because I am not getting PWM on pin PB13.