2025-01-21 09:33 AM - last edited on 2025-01-21 10:00 AM by Andrew Neil
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
2025-01-21 09:48 AM
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.
2025-01-21 11:22 AM
@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