Skip to main content
Senior
January 21, 2025
Question

Changing Brightness

  • January 21, 2025
  • 2 replies
  • 458 views

Hi I am having some trouble adjusting the brightness for the STM32F7.

I used this in main.c, and externed it 

I tried using this in model.cpp and in main.c, neither seems to adjust the brightness of the display.

 

void setDutyCycle(float duty_cycle) {
 if (duty_cycle > 100) duty_cycle = 100;
 if (duty_cycle < 0 ) duty_cycle = 0;

 float pw_resolution = (((float)99 + 1.0f) / 100.0f);

 uint16_t pw_desired = pw_resolution * duty_cycle;
 __HAL_TIM_SET_COMPARE( &htim2, TIM_CHANNEL_3, pw_desired );
}

 


Code formatting applied - please see How to insert source code for future reference

 

2 replies

Tesla DeLorean
Guru
January 21, 2025

Attach a scope, check the output.

Drive the TIM2->CCR3 manually, confirm it does what you expect.

Confirm you're writing in the values you think you are.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Peter BENSCH
Technical Moderator
January 21, 2025

@Priyank Your problem starts with the fact that you absolutely want to pass the duty cycle as a float. Wouldn't it be easier to treat it as a percentage and e.g. uint8_t?

But even if you use float, you must also use everything that has to do with it as float. For example, you query:

if(duty_cycle > 100)...

which should be expressed as:

if(duty_cycle > 100.0)...

Anyway, the best thing would also be to work with direct register programming first, as @Tesla DeLorean has recommended.

Regards
/Peter