@@ -29,8 +29,18 @@ 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 ;
36+ static int _internalWriteResolution =
37+ #if PWM_RESOLUTION > MAX_PWM_RESOLUTION
38+ MAX_PWM_RESOLUTION
39+ #else
40+ PWM_RESOLUTION
41+ #endif
42+ ;
43+
3444static uint32_t _writeFreq = PWM_FREQUENCY ;
3545
3646void analogReadResolution (int res )
@@ -40,7 +50,16 @@ void analogReadResolution(int res)
4050
4151void analogWriteResolution (int res )
4252{
43- _writeResolution = res ;
53+ if ((res > 0 ) && (res <= 32 )) {
54+ _writeResolution = res ;
55+ if (_writeResolution > MAX_ADC_RESOLUTION ) {
56+ _internalWriteResolution = MAX_ADC_RESOLUTION ;
57+ } else {
58+ _internalWriteResolution = _writeResolution ;
59+ }
60+ } else {
61+ Error_Handler ();
62+ }
4463}
4564
4665void analogWriteFrequency (uint32_t freq )
@@ -115,7 +134,8 @@ void analogWrite(uint32_t ulPin, uint32_t ulValue)
115134 if (is_pin_configured (p , g_anOutputPinConfigured ) == false) {
116135 set_pin_configured (p , g_anOutputPinConfigured );
117136 }
118- pwm_start (p , _writeFreq , ulValue , _writeResolution );
137+ ulValue = mapResolution (ulValue , _writeResolution , _internalWriteResolution );
138+ pwm_start (p , _writeFreq , ulValue , _internalWriteResolution );
119139 } else
120140#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */
121141 {
0 commit comments