From b6d15a9841e5312006bf7044192b08ab299b30cf Mon Sep 17 00:00:00 2001 From: LynnL4 Date: Thu, 28 Nov 2019 14:53:31 +0800 Subject: [PATCH] allow to change pwm frequence. --- 1.0.0/cores/arduino/wiring_analog.c | 14 ++++++++++++-- 1.0.0/cores/arduino/wiring_analog.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/1.0.0/cores/arduino/wiring_analog.c b/1.0.0/cores/arduino/wiring_analog.c index 0f3275b..6eafc12 100644 --- a/1.0.0/cores/arduino/wiring_analog.c +++ b/1.0.0/cores/arduino/wiring_analog.c @@ -25,7 +25,7 @@ extern "C" { static int _readResolution = 10; static int _writeResolution = 8; - +static int _pwm__frequence = PWM_FREQUENCY; void analogReadResolution(int res) { _readResolution = res; @@ -128,7 +128,7 @@ void analogWrite(uint32_t ulPin, uint32_t ulValue) ulValue = mapResolution(ulValue, _writeResolution, PWM_RESOLUTION); uint16_t TimerPeriod = (uint16_t)(SystemCoreClock / 1000000) - 1; - uint16_t TimerARP = (uint16_t)(1000000 / PWM_FREQUENCY) - 1; + uint16_t TimerARP = (uint16_t)(1000000 / _pwm__frequence) - 1; uint16_t Duty_Cycle = (uint16_t)((ulValue * 100) / 255); uint16_t ChannelPulse = (uint16_t)((Duty_Cycle * (TimerARP + 1)) / 100); @@ -198,8 +198,18 @@ void analogWrite(uint32_t ulPin, uint32_t ulValue) // TIMx Main Output Enable TIM_CtrlPWMOutputs(TIMx, ENABLE); + + +} + +// allow to change pwm frequence +void setPWMfrequence(uint32_t _frequence) +{ + _pwm__frequence = _frequence; + } + #ifdef __cplusplus } #endif diff --git a/1.0.0/cores/arduino/wiring_analog.h b/1.0.0/cores/arduino/wiring_analog.h index 351cc4c..254cbb6 100644 --- a/1.0.0/cores/arduino/wiring_analog.h +++ b/1.0.0/cores/arduino/wiring_analog.h @@ -29,13 +29,13 @@ typedef enum _eAnalogReference AR_DEFAULT, } eAnalogReference ; - void analogReference(eAnalogReference ulMode) ; void analogWrite(uint32_t ulPin, uint32_t ulValue) ; uint32_t analogRead(uint32_t ulPin) ; void analogReadResolution(int res); void analogWriteResolution(int res); void analogOutputInit(void) ; +void setPWMfrequence(uint32_t _frequence); #ifdef __cplusplus }