Skip to content

PwmService

Daniel Johnson edited this page Jul 9, 2021 · 7 revisions
  1. Introduction
  2. Function Descriptions
  3. Examples
      Change LED brightness with Input PulseWidth

1. Introduction

The PwmService is a basic hardware abstraction to Read and Write to PWM pins. All functions are passed down to the underlying abstraction.

2. Function Descriptions

void InitPin(uint16_t pin, PinDirection direction, uint16_t minFreqeuncy)

Initializes a pin in the selected direction, either In or Out. Configuring the pin as In will set the pin as a high impedance input. minFrequency is the minimum frequency expected to either be output, or read in. That is used to setup the timer appropriately.

PwmValue ReadPin(uint16_t pin)

Returns the period and the pulse width of the pin.

void WritePin(uint16_t pin, PwmValue value)

Writes the period and pulse width to be output on the pin.

3. Examples

Change LED brightness with Input PulseWidth

Declaration

IPwmService *_pwmService;

Setup

 // Initialize LED pin
_pwmService->InitPin(45, PinDirection::Out, 100); //PC13 as Output 100 hz PWM pin
 // Initialize input pin
_pwmService->InitPin(0, PinDirection::In, 100); //PA0 as Input 10 hz PWM pin

Loop

 // Read pwm pin
PwmValue inputPwm = _pwmService->ReadPin(0);
 // Set PWM duty cycle to the input pulsewidth ranging between 0.01 and 0.09
_pwmService->WritePin(45, { 0.01, 0.1 * inputPwm.PulseWidth });