Skip to content

Commit

Permalink
allow to change pwm frequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
LynnL4 committed Nov 28, 2019
1 parent 53092da commit b6d15a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions 1.0.0/cores/arduino/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion 1.0.0/cores/arduino/wiring_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b6d15a9

Please sign in to comment.