Cheat Sheet - EA Baseboard PCA9352

Introduction

This cheat sheets aims to get you up and running with the PCA9532 - a 16 bit I2C IO expander designed for controlling LEDs, which is included on the baseboard from  Embedded Artists.

To do this, we'll be looking in more detail at:

  • PCA9532 - What it is and how it works
  • The EA baseboard hardware - How the PCA9532 is set up on the EA baseboard
  • Example Program - Fade the LEDs up one at a time.

PCA9532 IO Expander

The PCA9532 is an IC that is designed for controlling 16 LEDs over and I2C bus, and includes the logic to act as an I2C slave device as well as the drive capability for directly driving LEDs.

As well as being able to switch each of the LEDs on and off independently, the PCA9532 also has two fully programmable PWM controllers that can be used to control one or more of teh LEDs. Each PWM channel has a programmable period ranging from 0.6Hz to 152Hz, and a programmable duty cycle from 0-100%. This means the LEDs can be set to blink steadily and visiblibly, or dimmed.

The PCA9532 device is well documented on the the NXP website that gives links to where to buy it, data sheets as so on 

 

Embedded Artists Baseboard

 

The base board is available from Embedded Artists directly, or through regional distribution. Details of how to buy them, and links to more resources for this board can be found at :

 

The Baseboard Hardware

The PCA9532 is fitted to the Embedded artists board, and connected to a bank of 8 red LEDs on pins 0-7, and a bank of 8 green LEDs on pins 8-15. The pull resistors required by the I2C bus are already fitted, and the three additional address bits provided by the PCA9532 are all set to 0.

The important details :

SCL = mbed p27

SDA = mbed p28

Address 0xC0

The Software

For this we'll be using the library for the PCA9532. There are two published items for the PCA9532:

  • PCA9532 - The library ready to import into your own program. Does not include mbed library or main.cpp
  • PCA9532_HelloWorld - Full hello world example using the library above
Hello World!

Our first simple test is to simply toggle the LEDs on and off. The library defines the modes as :

  • MODE_OFF
  • MODE_ON
  • MODE_PWM0
  • MODE_PWM1

Our first experiment will use the SetMode function that sets all LEDs

#include "mbed.h" #include "PCA9532.h"

PCA9532 leds (p28, p27, 0xc0);

int main() {

    while (1) {         leds.SetLed(0xffff, MODE_ON);         wait (0.2);

        leds.SetLed(0xffff, MODE_OFF);         wait (0.2);     } }

This simple example will blink the LEDs on and off using the API for the PCA9532

The program can be imported here EA_PCA9532_HelloWorld 

 

The Example Program

#include "mbed.h"
#include "PCA9532.h"

PCA9532 leds (p28, p27, 0xc0);

int main() {

    // Set LED15 to blink using PWM channel 1
    leds.Period(1, 0.1);
    leds.Duty(1, 0.5);
    leds.SetLed(15, MODE_PWM1);

    // LED0-14 will fade up in turn, the reset

    while (1) {

        // 0x7FFF enables LED 0-14, 
        // which are being switched off
        leds.SetMode(0x7fff, MODE_OFF);

        // For each LED in turn
        for (int i = 0 ; i < 15 ; i++) {

            // Switch PWM to off, and connect LED(i)
            leds.Duty(0, 0.0);
            leds.SetLed(i, MODE_PWM0);

            // Fade LED(i) from 0 to 1.0
            for (float j = 0.0 ; j < 1.0 ; j+=0.01) {
                leds.Duty(0,j);
                wait(0.005);
            }

            // Set LED(i) to continuously ON
            // this stops it fading out with LED(i+1)
            leds.SetLed(i, MODE_ON);
            wait (0.01);
        }
    } // while(1)
} // main

 This example program can be imported from here EA_PCA9532_Example

Links and Resources

 


0 comments

You need to log in to post a comment