@@ -29,6 +29,8 @@ extern "C" {
2929uint32_t g_anOutputPinConfigured [MAX_NB_PORT ] = {0 };
3030#endif
3131
32+ #define MAX_PWM_RESOLUTION 16
33+
3234static int _readResolution = 10 ;
3335int _writeResolution = PWM_RESOLUTION ;
3436static uint32_t _writeFreq = PWM_FREQUENCY ;
@@ -40,7 +42,11 @@ void analogReadResolution(int res)
4042
4143void analogWriteResolution (int res )
4244{
43- _writeResolution = res ;
45+ if ((res > 0 ) && (res <= 32 )) {
46+ _writeResolution = res ;
47+ } else {
48+ Error_Handler ();
49+ }
4450}
4551
4652void analogWriteFrequency (uint32_t freq )
@@ -112,10 +118,15 @@ void analogWrite(uint32_t ulPin, uint32_t ulValue)
112118#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
113119#if defined(HAL_TIM_MODULE_ENABLED ) && !defined(HAL_TIM_MODULE_ONLY )
114120 if (pin_in_pinmap (p , PinMap_PWM )) {
121+ int internalResolution = _writeResolution ;
115122 if (is_pin_configured (p , g_anOutputPinConfigured ) == false) {
116123 set_pin_configured (p , g_anOutputPinConfigured );
117124 }
118- pwm_start (p , _writeFreq , ulValue , _writeResolution );
125+ if (_writeResolution > MAX_PWM_RESOLUTION ) {
126+ internalResolution = MAX_PWM_RESOLUTION ;
127+ ulValue = mapResolution (ulValue , _writeResolution , internalResolution );
128+ }
129+ pwm_start (p , _writeFreq , ulValue , internalResolution );
119130 } else
120131#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */
121132 {
0 commit comments