Table of Contents

Class Pca9685

Namespace
Pamoja.Actuators
Assembly
Pamoja.Actuators.dll

An NXP PCA9685 16-channel PWM controller, for servos, LEDs, and valves, and a driver for one on an I2C bus.

public sealed class Pca9685 : IDisposable
Inheritance
Pca9685
Implements
Inherited Members

Remarks

The static members are the part's datasheet: its registers, the prescale arithmetic, and Pca9685.Sim, a part that is not there.

An instance drives one part over an I2cBus. It programs the prescale for its frequency with the oscillator asleep, because the part only takes a prescale then, wakes it, waits the 500 us it needs, and loads a channel's four registers in one transfer. Nothing is sent until Init() or the first channel is loaded.

Constructors

Pca9685(I2cBus, byte, uint, uint, Pca9685Outputs?)

Creates a driver for the part at address on bus.

public Pca9685(I2cBus bus, byte address = 64, uint frequencyHz = 200, uint oscillatorHz = 25000000, Pca9685Outputs? outputs = null)

Parameters

bus I2cBus

The bus the part is on.

address byte

The address its A5 to A0 pins select, DefaultAddress with all six low.

frequencyHz uint

The PWM frequency every channel shares; 50 for hobby servos.

oscillatorHz uint

The clock the prescaler divides, for a board that drives EXTCLK.

outputs Pca9685Outputs?

How the outputs are wired, or null for PowerOn.

Exceptions

PamojaException

The native driver could not be created.

Fields

Channels

How many channels it drives.

public const byte Channels = 16

Field Value

byte

Counts

How many counts each period is divided into.

public const ushort Counts = 4096

Field Value

ushort

DefaultAddress

The address it answers at with its six address pins low.

public const byte DefaultAddress = 64

Field Value

byte

InternalOscHz

The part's internal oscillator frequency, in hertz.

public const uint InternalOscHz = 25000000

Field Value

uint

Mode1Reset

MODE1 at power-up: asleep, answering the All Call address.

public const byte Mode1Reset = 17

Field Value

byte

Mode2Reset

MODE2 at power-up: totem-pole outputs.

public const byte Mode2Reset = 4

Field Value

byte

OscillatorStartupMicros

How long the oscillator takes to run once woken, in microseconds.

public const uint OscillatorStartupMicros = 500

Field Value

uint

PreScaleMin

The smallest value the part loads into PRE_SCALE.

public const byte PreScaleMin = 3

Field Value

byte

PreScaleReset

PRE_SCALE at power-up: 200 Hz on the internal oscillator.

public const byte PreScaleReset = 30

Field Value

byte

Properties

Frequency

The frequency the part runs at once the prescaler has rounded the one asked for, in hertz.

public float Frequency { get; }

Property Value

float

Prescale

The prescale value the driver writes for its frequency.

public byte Prescale { get; }

Property Value

byte

Methods

Channel(byte)

Reads one channel's four register bytes back from the part.

public byte[] Channel(byte channel)

Parameters

channel byte

The output, 0 to 15.

Returns

byte[]

The four register bytes the channel holds.

Remarks

The registers are read one at a time, so the read works whether or not MODE1's auto-increment bit is set, and it changes nothing on the part: a program that restarts can ask a running part what it holds. Counts(ReadOnlySpan<byte>) names the counts.

Exceptions

PamojaException

The channel is past 15, or the bus failed.

ChannelRegister(byte)

Returns the first of a channel's four consecutive registers.

public static byte ChannelRegister(byte channel)

Parameters

channel byte

The channel, 0 to 15.

Returns

byte

The register address.

Exceptions

PamojaException

The channel is beyond the part.

Dispose()

Releases the driver and its share of the bus.

public void Dispose()

FrequencyForPrescale(byte, uint)

Returns the update rate a prescale value produces.

public static float FrequencyForPrescale(byte prescale, uint oscHz = 25000000)

Parameters

prescale byte

The prescale register value.

oscHz uint

The oscillator frequency, usually InternalOscHz.

Returns

float

The frequency in hertz.

Init()

Programs the prescale and the output wiring, wakes the oscillator, and restarts the channels.

public void Init()

Exceptions

PamojaException

The bus failed.

PrescaleForFrequency(uint, uint)

Returns the prescale value that sets an update rate.

public static byte PrescaleForFrequency(uint updateRateHz, uint oscHz = 25000000)

Parameters

updateRateHz uint

The PWM frequency wanted.

oscHz uint

The oscillator frequency, usually InternalOscHz.

Returns

byte

The prescale register value.

SetAll(ReadOnlySpan<byte>)

Loads every channel with the same four bytes in one transfer.

public void SetAll(ReadOnlySpan<byte> pwm)

Parameters

pwm ReadOnlySpan<byte>

The four register bytes the Pwm builders make.

Exceptions

PamojaException

The setting is not four bytes, or the bus failed.

SetChannel(byte, ReadOnlySpan<byte>)

Loads one channel, initializing the part first if it has not been.

public void SetChannel(byte channel, ReadOnlySpan<byte> pwm)

Parameters

channel byte

The output, 0 to 15.

pwm ReadOnlySpan<byte>

The four register bytes the Pwm builders make.

Exceptions

PamojaException

The channel is past 15, the setting is not four bytes, or the bus failed.

Sleep()

Stops the oscillator; the channels keep their settings.

public void Sleep()

Exceptions

PamojaException

The bus failed.

SoftwareReset()

Sends the general-call software reset to every PCA9685 on the bus.

public void SoftwareReset()

Remarks

It goes to address 0x00, which nothing on a simulated bus answers.

Exceptions

PamojaException

The bus failed, or nothing acknowledged the general call.

Wake()

Wakes the oscillator, waits the 500 us it needs, and restarts the channels.

public void Wake()

Exceptions

PamojaException

The bus failed.