Thursday, June 30, 2016

P1 - PCF8575 - Remote 16-bit I/O expander for ESP8266 I2C-bus



AVAILABLE also on TINDIE Store : https://www.tindie.com/stores/nEXT_EVO1/


Today we will continue with the most requested typer os posts the "Driver Implementation Series": Part 1 of the PCF8575 16-bit I/O Port Expander - Output.






    In the situation when you need more I/O pins that available on your MCU the simplest solution is to use a "port expander". In the previous articles, nEXT EVO Extension Board AN1 (for I/O output) and 4x4 Matrix Keyboard input example for ESP8266 (for I/O input), we spoken already about a 8 bit I/O port expander based on the PCF8574 IC from NXP.

    But what to do if you need for your project even more I/O lines?

    You have 2 solution:

    1. Use more that one PCF8574, configure each with it's own unique I2C address.

    2. For higher density number of I/O pins os just a compact 16 I/O lines interface, use the bigger brother of the PCF8574, the PCF8575.

    As the option (1) I think is obvious for everybody and means just replication of the PCF8574 as many times you need it (and have free available I2C addresses for each), today we will explore a bit the 16bit version, the PCF8575.


  PCF8575 General Description

    The PCF8575 is a silicon CMOS circuit, and provides general purpose remote I/O expansion for most microcontroller families via the two-line bidirectional bus (I²C-bus).

    The device consists of a 16-bit quasi-bidirectional port and an I²C-bus interface (PCF8574 has 8-bit one).
     The PCF8575 has a low current consumption and includes latched outputs with high current drive capability for directly driving LEDs.
      It also possesses an interrupt line (INT) which can be connected to the interrupt logic of the microcontroller. By sending an interrupt signal on this line, the remote I/O can inform the microcontroller if there is incoming data on its ports without having to communicate via the I²C-bus.
And here you can see the advantage over using 2xPCF8574. For each of the 16 I/O lines you can use just one INT line to your MCU. In case of the PCF8574 usage you need 2 INT lines. So, less overhead on number of the I/O lines.

     Every data transmission from the PCF8575 must consist of an even number of bytes, the first byte will be referred to as P07 to P00 and the second byte as P17 to P10. The third will be referred to as P07 to P00 and so on.


PCF8575 - Block Diagram


    Features

  • Operating supply voltage 2.5 to 5.5 V
  • Low standby current consumption of 10 µA maximum
  • I²C-bus to parallel port expander
  • 400 kbits/s FAST I²C-bus
  • Open-drain interrupt output
  • 16-bit remote I/O port for the I²C-bus
  • Compatible with most microcontrollers
  • Latched outputs with high current drive capability for directly driving LEDs
  • Address by 3 hardware address pins for use of up to 8 devices
  • SSOP24 package.

     As been very similar with his small brother, the PCF8575, described already, I will not insist too much on the IC itself, for a deeper look please take a look also at the Datasheet.


 What we will need:


  • ESP8266 nEXT EVO Board
  • PCA9685 Extension Board as below or similar
  • For programming and uploading the driver and the software we will continue to use the LuaUploader as before.  
    Connection with the ESP8266 nEXT EVO Board is very easy, as PCF8575 Extension Board connector is fully compatible with the nEXT connector. Depending on how to you choose you socket type, you can install it on TOP or Bottom of the ESP8266 nEXT EVO Board :

PCF8575 - nEXT EVO 16bit Expander Board - TOP

 On the bottom side you can see also available the jumpers for I2C address selection in case that you need more than one I/O Expander board connected in the same time:

PCF8575 - nEXT EVO 16bit Expander Board - Bottom



Driver implementation
 
 
As PCF8575 is a I2C device, building a driver for it will follow the same path as for any other  I2C compatible device:
   
 
1 . I2C Bus initialisation function
function  init_i2c(sda, scl)
          i2c.setup(id, sda, scl, i2c.SLOW)
     end

2 . PCF8575 Write register function (for data Output)

     To write, the master (microcontroller) first addresses the slave device. By setting the last bit of the byte containing the slave address to logic 0 the write mode is entered.

     The PCF8575 acknowledges and the master sends the first data byte for P07 to P00. After the first data byte is acknowledged by the PCF8575, the second data byte P17 to P10 is sent by the master. Once again the PCF8575 acknowledges the receipt of the data after which this 16-bit data is presented on the port lines.


    The number of data bytes that can be sent successively is not limited. After every two bytes the previous data is overwritten.
    The first data byte in every pair refers to Port 0 (P07 to P00), whereas the second data byte in every pair refers to Port 1 (P17 to P10):




function setPort( port, PL, PH) 
    i2c.start(id)
    i2c.address(id, io_addr,i2c.TRANSMITTER)
    i2c.write(id,PL)
    i2c.write(id,PH)
    i2c.stop(id)
end


3 . PCF8575 Set Port (for data Output)

function setPortdata(pl, ph)
    ppl = 255-pl
    pph = 255-ph
    setPort(0x20,ppl, pph)
end

4 . Main program
 id = 0                    --I2C bus ID
sda=2                    --GPIO4
scl=1                     --GPIO5
io_addr = 0x20     --PCF8575 I2C Address


--init I2C nEXT BUS
i2c_init()

 -- Direct port bit set examples
setPortdata(0,0)   -- All OFF  

setPortdata(1,0)     -- P0 - ON
setPortdata(2,0)     -- P1 - ON
setPortdata(4,0)     -- P2 - ON
setPortdata(8,0)     -- P3 - ON
setPortdata(16,0)    -- P4 - ON
setPortdata(32,0)    -- P5 - ON
setPortdata(64,0)    -- P6 - ON
setPortdata(128,0)   -- P7 - ON

setPortdata(0,1)     -- P8  - ON
setPortdata(0,2)     -- P9  - ON
setPortdata(0,4)     -- P10 - ON
setPortdata(0,8)     -- P11 - ON
setPortdata(0,16)    -- P12 - ON
setPortdata(0,32)    -- P13 - ON
setPortdata(0,64)    -- P14 - ON
setPortdata(0,128)   -- P15 - ON


Next time we will explore the PCF8575 16 bit I/O Port Expander Inpout capabilities.




Wednesday, June 29, 2016

PCA9685-16Channel 12-bit PWM I²C-bus LED controller Driver


    !! UPDATE !! UPDATE !!
   Available on Tindie Store here 




   PCA9685 General Description

   If you are looking for a very simple to use 16 Channel LED controller or just for a nice 16 Channel, high resolution PWM driver then this one is for you!

   The PCA9685 is an I²C-bus controlled 16-channel LED controller optimized for Red/Green/Blue/Amber (RGBA) color backlighting applications.

   Each LED output has its own 12-bit resolution (4096 steps) fixed frequency individual PWM controller that operates at a programmable frequency from a typical of 24 Hz to 1526 Hz with a duty cycle that is adjustable from 0 % to 100 % to allow the LED to be set to a specific brightness value. All outputs are set to the same PWM frequency.

  Each LED output can be off or on (no PWM control), or set at its individual PWM controller value.

  The LED output driver is programmed to be either open-drain with a 25 mA current sink capability at 5 V or totem pole with a 25 mA sink, 10 mA source capability at 5 V.

  The PCA9685 operates with a supply voltage range of 2.3 V to 5.5 V and the inputs and outputs are 5.5 V tolerant. LEDs can be directly connected to the LED output (up to 25 mA, 5.5 V) or controlled with external drivers and a minimum amount of discrete components for larger current or higher voltage LEDs.



PCA9685 - Block Diagram

   

Features

  • 16 LED drivers. Each output programmable at:
    • Off
    • On
    • Programmable LED brightness
    • Programmable LED turn-on time to help reduce EMI
     
  • 1 MHz Fast-mode Plus compatible I²C-bus interface with 30 mA high drive capability on SDA output for driving high capacitive buses 
  • 4096-step (12-bit) linear programmable brightness per LED output varying from fully off (default) to maximum brightness 
  • LED output frequency (all LEDs) typically varies from 24 Hz to 1526 Hz (Default of 1Eh in PRE_SCALE register results in a 200 Hz refresh rate with oscillator clock of 25 MHz)
  • Sixteen totem pole outputs (sink 25 mA and source 10 mA at 5 V) with software programmable open-drain LED outputs selection (default at totem pole). No input function.
  • Output state change programmable on the Acknowledge or the STOP Command to update outputs byte-by-byte or all at the same time (default to ‘Change on STOP’)
  • Active LOW Output Enable (OE) input pin. LEDn outputs programmable to logic 1, logic 0 (default at power-up) or ‘high-impedance’ when OE is HIGH.
  • 6 hardware address pins allow 62 PCA9685 devices to be connected to the same I²C-bus
  • Toggling OE allows for hardware LED blinking
  • 4 software programmable I²C-bus addresses (one LED All Call address and three LED Sub Call addresses) allow groups of devices to be addressed at the same time in any combination (for example, one register used for ‘All Call’ so that all the PCA9685s on the I²C-bus can be addressed at the same time and the second register used for three different addresses so that 1⁄3 of all devices on the bus can be addressed at the same time in a group). Software enable and disable for these I²C-bus address.
  • Software Reset feature (SWRST General Call) allows the device to be reset through the I²C-bus
  • 25 MHz typical internal oscillator requires no external components
  • External 50 MHz (max.) clock input
  • Internal power-on reset
  • Noise filter on SDA/SCL inputs
  • Edge rate control on outputs
  • No output glitches on power-up
  • Supports hot insertion
  • Low standby current
  • Operating power supply voltage range of 2.3 V to 5.5 V
  • 5.5 V tolerant inputs
  • -40 °C to +85 °C operation
  • ESD protection exceeds 2000 V HBM per JESD22-A114, 200 V MM per JESD22-A115 and 1000 V CDM per JESD22-C101
  • Latch-up testing is done to JEDEC Standard JESD78 which exceeds 100 mA
  • Packages offered: TSSOP28, HVQFN28


 For more details please take a look at the PCA9685 Datasheet



What we will need:

  • ESP8266 nEXT EVO Board
  • PCA9685 Extension Board as below or similar
  • For programming and uploading the driver and the software we will continue to use the LuaUploader as before.  
    Connection with the ESP8266 nEXT EVO Board is very easy, as PCA9685 Extension Board connector is fully compatible with the nEXT connector. Depending on how to you choose you socket type, you can install it on TOP or Bottom of the ESP8266 nEXT EVO Board :

PCA9685 16 Channel PWM Extension Board


Driver implementation
 
 
As
PCA9685 has a I2C compatible compatible interface, building a driver for it it's a pretty straigh forward process following the standard path for a I2C compatible device:
 
 
1 . I2C Bus initialisation function
function  init_i2c(sda, scl)
          i2c.setup(id, sda, scl, i2c.SLOW)
     end


2 . PCA9685 Read register function
function read_reg(reg)
          i2c.start(id)
          i2c.address(id, dev_addr ,i2c.TRANSMITTER)
          i2c.write(id,reg)
          i2c.stop(id)
          i2c.start(0x0)
          i2c.address(0x0, dev_addr,i2c.RECEIVER)
          c = i2c.read(0x0,1)
          i2c.stop(0x0)
          rval = string.byte(c, 1)
          --print(rval)
          return rval
end

3 . PCA9685 Write register function
function write_reg(reg, data)
          i2c.start(id)
          i2c.address(id, dev_addr ,i2c.TRANSMITTER)
          i2c.write(id,reg)
          i2c.write(id,data)
          i2c.stop(id)
end


4 . Write 12bit values function
function write_12_bit(a)
         ah=bit.rshift(a,8)
         al=bit.band(a,0xff)
         --print(a,ah,al)
         return ah, al
end  

5 . PCA9685 initialisation function

-- LED_ON and LED_OFF are 12bit values (0-4095)
-- ledN is 0-15

function init_pca()
    write_reg(mode1, rst)           --reset device

    if (read_reg(mode1)==0x01) then --check status
        status = true
        print("PCA9685 Init OK")
    else
        status = false
        print("PCA9685 Init Failure!")
    end
    --print(status)

    write_reg(mode1, 0xA0) --10100000 - set for auto-increment


   -- choose here the desired output mode!

    --Direct LED connection
    write_reg(mode2, 0x10) --set to output mode INVRT = 1 OUTDRV = 0

    --External N-type driver
    --write_reg(mode2, 0x04)   -- set to output mode INVRT = 0 OUTDRV = 1

    --External P-type driver
    --write_reg(mode2, 0x14) --set to output mode INVRT = 1 OUTDRV = 1
   
    return status
end


6 . Write LEDn function
 function write_LED(ledN, LED_ON, LED_OFF)
          i2c.start(id)
          i2c.address(id, dev_addr ,i2c.TRANSMITTER)
          i2c.write(id,led0+4*ledN)
          write_12_bit(LED_ON)
          i2c.write(id,al)
          i2c.write(id,ah)
          write_12_bit(LED_OFF)
          i2c.write(id,al)
          i2c.write(id,ah)
          i2c.stop(id)
end


7 . Set LEDn ON function  

function setLED_ON(ledN)
        write_LED(ledN,0x1000,0)
end


8 . Set LEDn OFF function

function setLED_OFF(ledN)
        write_LED(ledN,0,0x1000)
end


9 . Set LEDn Dimmer function

function LED_Dimmer(ledN, dimm)
   if (dimm==0) then setLED_OFF(ledN)
   else if (dimm==100) then setLED_ON(ledN)
        else write_LED(ledN, dim, 0)
   end
   end
end


 10 . Main Program
dev_addr=0x40  --I2C Address for the PCA9685

mode1=0x00     --location for Mode1 register address
mode2=0x01     --location for Mode2 reigster address
led0=0x06      --location for start of LED0 registers
rst=0x01       --reset device

id = 0
sda=2 --GPIO4
scl=1 --GPIO5

--init I2C Bus
init_i2c(sda, scl)


--init PCA9685
init_pca()
 
--running tests
--set LED_0 ON/OFF
setLED_ON(0)
setLED_OFF(0)

--Dim LED_0 at different levels
write_LED(0,0,4095)
write_LED(0,0,2048)
write_LED(0,0,1024)
write_LED(0,0,512)
write_LED(0,0,256)
write_LED(0,0,128)
write_LED(0,0,64)
write_LED(0,0,32)
write_LED(0,0,16)
write_LED(0,0,8)
write_LED(0,0,4)

 --test Dimmer
dim=2
tmr.alarm( 1, 200, 1, function()
      if (dim>=4096) then dim=2 end
      write_LED(0,0,dim)
      dim=dim+200
      print(dim)
end)


tmr.stop(1)