PWM Switch

PWM charger MOSFET switch control functions.

Only used for PWM solar charge controllers.

Functions

float pwm_signal_get_duty_cycle()

Read the currently set duty cycle

Returns:

Duty cycle between 0.0 and 1.0

void pwm_signal_set_duty_cycle(float duty)

Set the duty cycle of the PWM signal

Parameters:

duty – Duty cycle between 0.0 and 1.0

void pwm_signal_duty_cycle_step(int delta)

Change raw timer capture/compare register by defined step

Parameters:

delta – Steps to be added/substracted from current CCR value

void pwm_signal_init_registers(int freq_Hz)

Initiatializes the registers to generate the PWM signal and sets duty cycle limits

Parameters:

freq_Hz – Switching frequency in Hz

void pwm_signal_start(float pwm_duty)

Start the PWM generation

Parameters:

duty – Duty cycle between 0.0 and 1.0

void pwm_signal_stop()

Stop the PWM generation

bool pwm_signal_high()

Check if the PWM pin voltage level is high (on-state)

Returns:

true if pin is high, false if pin is low

bool pwm_active()

Check if the PWM generation is active

Returns:

true if PWM signal generation is active

class PwmSwitch : public PowerPort
#include <pwm_switch.h>

PWM charger type

Contains all data belonging to the PWM switching sub-component.

Public Functions

PwmSwitch(DcBus *dc_bus)
void control()

Main control function for the PWM switching algorithm

void test()

Test mode for PWM switch

Sets duty cycle to 90% and listens to enable/disable signal

void stop()

Fast stop function (bypassing control loop)

May be called from an ISR which detected overvoltage / overcurrent conditions.

PWM port will be restarted automatically from control function if condtions are valid.

bool active()

Read the general on/off status of PWM switching

Returns:

true if on

bool signal_high()

Read the current high or low state of the PWM signal

Returns:

true if high, false if low

float get_duty_cycle()

Read the currently set duty cycle

Returns:

Duty cycle between 0.0 and 1.0

Public Members

float ext_voltage

Voltage measurement at terminal (external, usually solar panel voltage)

bool enable = true

Enable switch, true by default. Can be used to completely disable the PWM power stage.

float offset_voltage_start = 2.0F

Offset voltage of solar panel vs. battery to start charging (V)

uint32_t restart_interval = 60

Interval to wait before retrying charging after low solar power cut-off or overvoltage event (s)

time_t off_timestamp = -10000

Time when charger was switched off last time

Initialized with large negative value to start immediately after reset.

time_t power_good_timestamp

Last time the current through the switch was above minimum