если в ШИМ нужен максимальный уровень, используется digitalWrite(), чтобы не было просадок еле заметных

This commit is contained in:
KLASSENTS 2025-08-01 13:24:49 +07:00
parent 1ab5125fc8
commit cd3e2268c6

View File

@ -104,9 +104,17 @@ It is recommended to enable the timer in the following order:
*/
void analogWrite(uint32_t PinNumber, uint32_t writeVal)
{
if (writeVal > WriteValMax) writeVal = WriteValMax;
if (writeVal >= WriteValMax)
{
// if we need max value, use digitalWrite to supply constant level
digitalWrite(PinNumber, HIGH);
}
else
{
// if we need less then max, use pwm
int8_t pwmState = digitalPinPwmIsOn(PinNumber);
if (digitalPinPwmIsOn(PinNumber) > 0) // pin has pwm and pwm is already on
if (pwmState > 0) // pin has pwm and pwm is already on
{
// we can only change writeVal if it is differ from current value
TIMER32_TypeDef* timer = pwmPinToTimer(PinNumber);
@ -117,10 +125,10 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR = newOCR;
}
}
else if (digitalPinPwmIsOn(PinNumber) == 0) // pin has pwm and pwm is off
else if (pwmState == 0) // pin has pwm and pwm is off
{
// init pin as pwm
uint32_t OCRval = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax);
uint32_t OCRval = (pwmTopVal * writeVal) / WriteValMax;
// initialization of the required timer
htimer32.Instance = pwmPinToTimer(PinNumber);
@ -153,6 +161,7 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
}
else // pin doesn't have pwm
ErrorMsgHandler("analogWrite(): invalid pwm pin number");
}
}
// Set the resolution of analogWrite parameters