Showing posts with label MCP4728. Show all posts
Showing posts with label MCP4728. Show all posts

Thursday, August 4, 2016

ESP Basic -3 - PCF8574 I2C I/O 8 x independent switches example



8 x Independent switches driver Example :






  
   After doing last time the Mutually exclusive 8x Switch driver example, now is time for a 8 x independent switches implementation .


What we will need: 


      This is how is looking the setup on a breadboard, for a better visibility:





Software implementation:

Something to remember: 

  • PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot output high, if you want). Look at the above example how is connected the LED for SINKING current.
  • Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
  • Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
  • Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)

For more details please take a look at the PCF8574 Datasheet


ESP8266 Basic code: 
let address = 32 'PCF8574 I2C Address

i2c.begin(address)
ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
button "1", [1]
button "2", [2]
button "3", [3]
button "4", [4]
button "5", [5]
button "6", [6]
button "7", [7]
button "8", [8]
button "OFF", [9]
wait

[9]
i2c.begin(address)
ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
wait

[1]
i2c.begin(address)
ss = ss xor 1   'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
wait

[2]
i2c.begin(address)
ss = ss xor 2
i2c.write(ss)
i2c.end()
wait

[3]
i2c.begin(address)
ss = ss xor 4
i2c.write(ss)
i2c.end()
wait

[4]
i2c.begin(address)
ss = ss xor 8
i2c.write(ss)
i2c.end()
wait

[5]
i2c.begin(address)
ss = ss xor 16
i2c.write(ss)
i2c.end()
wait

[6]
i2c.begin(address)
ss = ss xor 32
i2c.write(ss)
i2c.end()
wait

[7]
i2c.begin(address)
ss = ss xor 64
i2c.write(ss)
i2c.end()
wait

[8]
i2c.begin(address)
ss = ss xor 128
i2c.write(ss)
i2c.end()
wait

This is how is looking the interface in the Web Browser after running the above program:



PS: credit for the XOR Bit masking goes this time to Forlotto from the esp8266.com forum who posted quicker than me this elegant solution :)

ESP Basic -1 - PCF8574 I2C I/O Expander Driver example



Mutually-exclusive switch driver Example :







     Now that we have the I2C scanner program  that can help us to detect our I2C devices is time to move to some driver examples

    I think everybody know already the famous PCF8574. I have also cover it extendely in the previous Articles about, here for I/O input and here for output I/O.

    Now is time to put it at work also in ESP8266 Basic and see how is working.

   The example for today is a implementation of a mutually-exclusive switch that can be handy for many, many things as for example for doing the selection of a source in a Audio mixer or a any other analog path. Or driving Power relays.

   Mutually exclusive switch means  that when the corresponding switch button is pressed ON must also automatically turn the other sources OFF.



What we will need: 


      This is how is looking the setup on a breadboard, for a better visibility:





Software implementation:

Something to remember: 

  • PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot output high, if you want). Look at the above example how is connected the LED for SINKING current.
  • Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
  • Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
  • Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)

For more details please take a look at the PCF8574 Datasheet


ESP8266 Basic code: 
let address=32     'PCF8574 I2C Address

button "1", [1]     'Define push buttons for each I/O pin
button "2", [2]
button "3", [3]
button "4", [4]
button "5", [5]
button "6", [6]
button "7", [7]
button "8", [8]
button "OFF", [9]  'Button for turning OFF all
wait

[9]
i2c.begin(address)    'Start I2C communication with device at 'address'
i2c.write(255)           'write the corresponding I/O pin value to register
i2c.end()                   'end I2C communication
wait

[1]
i2c.begin(address)
i2c.write(254)
i2c.end()
wait

[2]
i2c.begin(address)
i2c.write(253)
i2c.end()
wait

[3]
i2c.begin(address)
i2c.write(251)
i2c.end()
wait

[4]
i2c.begin(address)
i2c.write(247)
i2c.end()
wait

[5]
i2c.begin(address)
i2c.write(239)
i2c.end()
wait

[6]
i2c.begin(address)
i2c.write(223)
i2c.end()
wait

[7]
i2c.begin(address)
i2c.write(191)
i2c.end()
wait

[8]
i2c.begin(address)
i2c.write(127)
i2c.end()
wait

This is how is looking the interface in the Web Browser after running the above program:



Monday, August 1, 2016

ESP Basic - I2C Bus scanner example

UPDATE !! UPDATE !!UPDATE !!UPDATE !!
From ESPBasic Version 3.0 Alpha 43 you have also the i2c.setup function available!!

i2c.setup():

Will change the default pins for the i2c interface.  
i2c.setup({SDA}, {SCL})

----------------------------------------------------------------------------------------------------

From today I will start a new Series related with ESP Basic (www.esp8266basic.com)


When working with a lot of I2C devices one of the first thing that you need is a simple I2C scanner that can help you to identify the IC's and do some very basic troubleshooting.








What we will need: 
  • ESP8266 nEXT EVO Board
  • ESP8266 nEXT EVO - Analog Extension Board - AN1 (I2C devices onboard to test our scanner )
  • For programming and uploading the driver and the software we will use ESP8266 Basic .  


    nEXT EVO + AN-1 extension board



    I2C Scanner Software:

    UPDATE !! UPDATE !!UPDATE !!UPDATE !!
    From ESPBasic Version 3.0 Alpha 43 you have also the i2c.setup function available!!

    i2c.setup():

    Will change the default pins for the i2c interface.  
    i2c.setup({SDA}, {SCL})



              i2c.setup(4,5)
    for address = 1 to 127
       i2c.begin(address)
       stat = i2c.end()

       if stat < 1 then
         ' print stat
          wprint "Found I2C device at address: 0x" & hex(address)
          wprint " - > " & address
          wprint " <br>"
     endif
    next
    wait
     And the scanning result in browser :



    As you can see, has properly detected the I2C devices that are on the AN-1 Extension board:

    - PCF8574      - 8Bit I/O EXT port      - found at address 0x20 -> 32
    -
    LM75           - Temperature sensor    - found at address 0x48 -> 72
    -
    MCP4728    - 4x12Bit DAC              - found at address 0x60 -> 96
    -
    MCP3421    - 18Bit ADC                  - found at address 0x68 -> 10

Please keep in mind that in ESP Basic you have hardcoded I2C pins yet, SDA -> GPIO0 and SCL -> GPIO2.

Don't forget to add if needed pullup resistors on both lines, 4K7 should be OK. In case of AN-1 they are NOT needed.



Wednesday, April 20, 2016

MPDMv4 - AC MAINS Dimmer - software example

UPDATE !! Fixed broken Tindie Link from below, now should be OK UPDATE !!


--------------------------------------------------- DISCLAIMER --------------------------------------------------
WARNING!! You will play with LIVE MAINS!! Deadly zone!! 
      If you don't have any experience and are not qualified for working with MAINS power I will not encourage you to play arround!. The author take no responsibility for any injury or death resulting, directly or indirectly, from your inability to appreciate the hazards of household mains voltages.
   The circuit diagrams are as accurately as possible, but are offered with no guarantees whatsoever. 
    There is no guarantee that this design meets any Rules which may be in force in your country so please check before your local rules/regulations.
----------------------------------------------------------------------------------------------------------------------------
 
                                                             Creative Commons License

MPDMv4 by ESP8266-Projects.com is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.  

 ---------------------------------------------------------------------------------------------------------------------------

    For any new orders/requests please feel free to use as usual: tech at esp8266-projects.com. 
   
    MPDMv4 Boards are also available on Tindie: AC MAINS Dimmer - MPDMv4









As been a Voltage controled AC MAINS Dimmer you can control it with:
  • PWM signal
  • DAC output Voltage
  • or if you don't want any kind of MCU involved, just user a 10k Potentiometer in a voltage divider as part of a simple VCNT input circuit!

In this example we will use a MCP4728 4 channels/12 Bit DAC as a VCNT (voltage control) command source for our MPDMv4 Dimmer Board.



What we will need:


Software implementation

 1. MCP4728 DAC Driver

1.1 I2C Bus Initialisation
    init = function (self, sda, scl)
          self.id = 0
          i2c.setup(self.id, sda, scl, i2c.SLOW)
    end,

1.2 Set DAC Voltage output on the selected Channel

    dac = function(self, ch_reg,voltage)
          volt=(voltage*4096)/vcal
          msb = bit.rshift(volt, 8)  
          lsb = volt-bit.lshift(msb,8)   
          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
          i2c.write(id,msb)
          i2c.write(id,lsb)
          i2c.stop(id)
    end,




1.3 Set DAC Register 

   set_reg_dac = function(self, reg)
          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
          i2c.stop(id)
     end

2. MAIN Program
id=0
sda=2
scl=1
dac_addr=0x60
ch_reg=0x58      -- DAC CH A - Ext REF -
vcal=3.2325      -- external voltage reference = Vcc

require('mcp4728')             --call MCP4728 Driver module
mcp4728:init(sda, scl)       --Init I2C BUS
mcp4728:dac(ch_reg,2.8)  --Set VCMD Voltage (0-2.8V)

    2.1 Dimming stage example based on timer :


vcmd=0
tmr.alarm( 0, 1000, 1, function()
    print("Set VCMD value : "..vcmd)
    mcp4728:dac(ch_reg,vcmd)
    vcmd=vcmd+0.10
    if (vcmd>=2.81) then vcmd=0 end
end)

tmr.stop(0)   --stop the timer when you want to finnish cycling thru dimmer stages.

       











Wednesday, March 30, 2016

ESP8266 - ADC Input frontend



Autorange Analog frontend


For a deeper Hardware description please take a look at the ESP8266 Analog Extension Board





What we will need:
 


ADC Frontend description

 Selection of the input voltage divider is done using an analog switch driven by PCF8574 PORT P0 and P1 bits:

0 - 1:20 Divider
1 - 1:10 Divider
2 - 1:5 Divider
3 - Full Voltage Range


As the ADC Input is programmed to be used in the 0-2V range that will give us the followings available ranges:

0 -> 0 - 40V
1 -> 0 - 20V
2 -> 0 - 10V
3 -> 0 - 2V

In the case of using the MCP3421 ADC at 12 Bit resolution, we will have the following corresponding LSB values:

- 1:20 Divider  -  0.02V
- 1:10 Divider  -  0.01V
- 1:5 Divider    -  0.005V
- Full Range    -  0.001V




Software implementation

1. Init I2C bus/interface

 Standard I2C Bus Initialisation function:

function init_I2C()
    i2c.setup(bus, sda, scl, i2c.SLOW)
end

 2. Set PCF8574 PORT Register Function
function setPort( port, stat) 
    i2c.start(id)
    i2c.address(id, dev_addr ,i2c.TRANSMITTER)
    i2c.write(id,stat)
    i2c.stop(id)
end

 3. Set Port function 

 Just a nicer way to write data to PCF8574 Register. Remember that we need to write a "ZERO" to the corresponding bit. We are sinking not sourcing !!

function setPortdata(p)
    pp = 255-p
    setPort(0x20,pp)
end


4. Set Voltage Divider function

  Select the desired Voltage divider ratio based on the choosen ratio value and calibrated LSB data.

--calibration data
x20 = 0.020056
x10 = 0.01014
x5  = 0.005014
xfl = 0.0010075
xrt = x20 --by default start with highest VDIV!
function SetVDivider(rtio)
    if (rtio==0) then xrt=x20 end
    if (rtio==1) then xrt=x10 end
    if (rtio==2) then xrt=x5 end
    if (rtio==3) then xrt=xfl end
    print("XRT = "..xrt)
    setPortdata(rtio)    -- select desired voltage divider
    return xrt
end



       -- Set/Change Voltage Divider ratio:
SetVDivider(0)  -- 1:20 Divider
SetVDivider(1)  -- 1:10 Divider
SetVDivider(2)  -- 1:5  Divider
SetVDivider(3)  -- 1:1  Full Voltage in!!


5. MAIN Program


-- Main Program
id = 0
sda=2 --GPIO4
scl=1 --GPIO5
dev_addr = 0x20
--calibration data
x20 = 0.020056
x10 = 0.01014
x5  = 0.005014
xfl = 0.0010075
xrt = x20 --by default start with highest VDIV!
--init I2C Bus
init_I2C()

--Init Volatage divider
setPortdata(0) -- --by default start with highest VDIV!
SetVDivider(0)  -- 1:20 Divider

----MCP3421 ADC
require('mcp3421')
sda=2 --GPIO4
scl=1 --GPIO5
mcp3421:init(sda, scl)
mcp3421:write_ADC_config(0x68, 0x10)

tmr.alarm( 0, 1000, 1, function()
    adc_val = mcp3421:read_ADC_data(0x68)
    print("\nADC Value : "..adc_val.." \n Voltage  : " ..adc_val*xrt)
    return adc_val
end)





Tuesday, February 23, 2016

MCP4728 - 12 Bit I2C DAC Driver - AN1



 MCP4726 - 12 Bit I2C DAC Driver for ESP8266 nEXT EVO AN-1


Youtube Video here


    From today we will move on the Analog interface part of the nEXT EVO Board AN-1 and we will start talking about the Digital to Analog conversion, Analog Autoscaling input and Analog to Digital conversion .

 For the Digital to Analog conversion part the choosen one is the Microchip MCP4728 I2C DAC IC.

 The MCP4728 device is a quad, 12-bit voltage output Digital-to-Analog Convertor (DAC) with non-volatile memory (EEPROM).

As it has a on-board precision output amplifier with rail-to-rail analog output swing capabilities that means first of all that we don't need any other Output Buffer as mandatory needed for non-buffered DAC's.

 The MCP4728 device has also a high precision internal voltage reference (VREF = 2.048V). The user can select the internal reference or external reference (VDD) for each channel individually.


Features

• 12-Bit Voltage Output DAC with 4 Buffered Voltage Outputs

        - Each output is driven by its own output buffer with a gain of 1 or 2 depending on the gain and
       VREF selection bit settings.
        - In normal mode, the DC impedance of the output pin is about 1Ω. In Power-Down mode,
       the output pin is internally connected to 1 kΩ, 100 kΩ, or 500 kΩ, depending on the
       Power-Down selection bit settings.
        - The VOUT pin can drive up to 1000 pF of capacitive load. It is recommended to use a load
       with RL greater than 5 kΩ.

• On-Board Non-Volatile Memory (EEPROM) for DAC Codes and I2CTM Address Bits
• Internal or External Voltage Reference Selection
• Output Voltage Range:
     - Using Internal VREF (2.048V):
            0.000V to 2.048V with Gain Setting = 1
           0.000V to 4.096V with Gain Setting = 2
     - Using External VREF (VDD): 0.000V to VDD
• ±0.2 LSB DNL (typical)
• Fast Settling Time: 6 μs (typical)
• Normal or Power-Down Mode
• Low Power Consumption
• Single-Supply Operation: 2.7V to 5.5V
• I2C Interface:
      - Address bits: User Programmable to EEPROM
      - Standard (100 kbps), Fast (400 kbps) and High Speed (3.4 Mbps) Modes
• 10-Lead MSOP Package
• Extended Temperature Range: -40°C to +125°C

If you want you can consider it the 4 channels big brother of the MCP4726 1 Channel 12 Bit DAC  

For more details, please see MCP4728 Datasheet



What we will need:
 
ESP8266 nEXT Evo + AN1 Board



Driver implementation
 
 
As MCP4728 has a I2C compatible compatible interface, building a driver for it it's a pretty straigh forward process:

 
1 . I2C Bus initialisation function
function  init_i2c(sda, scl)
          i2c.setup(id, sda, scl, i2c.SLOW)
     end

2. DAC Register Data load

 Each channel has its own volatile DAC input register and EEPROM. The details of the input registers and EEPROM are shown in the tables below:



  

2.1 Simple version using the Power supply Voltage as External 
      Voltage reference, Vref=Vcc

        -- single Write command version
 function dac(ch_reg,voltage)
          volt=(voltage*4096)/vcal -- calibrate!
          print("Voltage Steps:" .. string.format("%d",volt))

          msb = bit.rshift(volt, 8)
          print("MSB:" .. string.format("%d",msb))      
          lsb = volt-bit.lshift(msb,8)
          print("LSB:" .. string.format("%d",lsb))      

          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
    
          i2c.write(id,msb)
          i2c.write(id,lsb)
          i2c.stop(id)
end


2.2 Using the internal Voltage Reference, Vref=2.048V
          - Gain settings also enabled, 1x,  2x
 function dac_vref(ch_reg,vref,g,voltage)
          volt=(voltage*4096)/vcal -- calibrate!
          print("Voltage Steps:" .. string.format("%d",volt))

          msb = bit.rshift(volt, 8)
          print("MSB:" .. string.format("%d",msb))      
          lsb = volt-bit.lshift(msb,8)
          print("LSB:" .. string.format("%d",lsb))      
          if (vref==1) then
                msb = msb + 128
                if (g==2) then msb = msb + 16 end
          end
          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
    
          i2c.write(id,msb)
          i2c.write(id,lsb)
          i2c.stop(id)
end


 3. TEST program

3.1 Vref = External, Vref=Vcc


init_i2c(sda,scl)
--SINGLE WRITE COMMAND: WRITE A SINGLE DAC INPUT REGISTER AND EEPROM

ch_reg=0x58         -- CH A - VRef Vcc
dac(ch_reg,0.5)
ch_reg=0x5A        -- CH B - VRef Vcc
dac(ch_reg,1)
ch_reg=0x5C        -- CH C - VRef Vcc
dac(ch_reg,2)
ch_reg=0x5E        -- CH D - VRef Vcc
dac(ch_reg,3)

3.2 Vref = Internal, Vref=2.048V

 -- SET PG to x1 or x2 - valid only when Vref = Vref Internal !!
ch_reg = 0x58
vref= 1    --set internal Vref = 2.048 !!
vcal=2.048

gain=2
voltage = 1
voltage= voltage/2
dac_vref(ch_reg,vref,gain,voltage)
voltage=0.5
dac_vref(ch_reg,vref,gain,voltage)


 3.3 Vref = Vcc

-- EXTERNAL Vref = Vcc
voltage = 2
gain=1
ch_reg = 0x58
vref= 0 -- set EXTERNAL Vref = Vcc = 3.265 - MEASURE & Calibrate!!
vcal=3.265
dac_vref(ch_reg,vref,gain,voltage)

--autoupdate DAC every sec
tmr.alarm( 0, 1000, 1, function()
  print("\nUpdate DAC data")
  dac_vref(ch_reg,vref,gain,voltage)
end)


Sunday, January 31, 2016

ESP8266 nEXT EVO - Analog extension Board - P3



This is Part 3 of the ESP8266 nEXT EVO Analog Extension Board (AN1)


In this part we will talk about the ESP8266 nEXT Evo 8 Bit I/O Expansion  Port based on the PCF8574 chip from NXP, testing procedures and software programming for the AN1 Board






Previous related Articles:



--------------------------------------------------------------------------------------------------------------------------
For any new CBDB orders/requests please feel free to use as usual:
     tech at esp8266-projects.com.


ESP8266 nEXT Evo bare PCB has also been made available directly at Dirty PCBs, our preferred PCB House for experimenting (**):
 http://dirtypcbs.com/view.php?share=9699&accesskey=91d782fd4a10943fd36ecc371c7ff2cd


(**) - Actually you have there 2 Boards for the price of one, a ESP8266 nEXT Evo together with a AN1 nEXT Analog Extension Board that brings you a 18Bit ADC (autoscale 0-40V input!), 4x12Bit DAC, Precison Temperature measurement, 8bit I/O port, etc.  
-------------------------------------------------------------------------------------------------------------------------



PCF8574 General Description

  The PCF8574/74A provides general-purpose remote I/O expansion via the two-wire
bidirectional I2C-bus (serial clock (SCL), serial data (SDA)).
  

   The devices consist of eight quasi-bidirectional ports, 100 kHz I2C-bus interface, three
hardware address inputs and interrupt output operating between 2.5 V and 6 V. The
quasi-bidirectional port can be independently assigned as an input to monitor interrupt
status or keypads, or as an output to activate indicator devices such as LEDs. System
master can read from the input port or write to the output port through a single register.


  The active LOW open-drain interrupt output (INT) can be connected to the interrupt logic
of the microcontroller and is activated when any input state differs from its corresponding
input port register state. It is used to indicate to the microcontroller that an input state has
changed and the device needs to be interrogated without the microcontroller continuously
polling the input register via the I2C-bus.


 
Features : 
  • I2C-bus to parallel port expander 
  • 100 kHz I2C-bus interface (Standard-mode I2C-bus)
  • Operating supply voltage 2.5 V to 6 V with non-overvoltage tolerant I/O held to VDD with 100uA current source
  • 8-bit remote I/O pins that default to inputs at power-up
  • Latched outputs directly drive LEDs
  • Total package sink capability of 80 mA
  • Active LOW open-drain interrupt output
  • Eight programmable slave addresses using three address pins
  • Low standby current (2.5 uA typical)
  • -40C to +85C operation 
  • ESD protection exceeds 2000 V HBM per JESD22-A114 and 1000 V CDM per JESD22-C101
  • Latch-up testing is done to JEDEC standard JESD78 which exceeds 100 mA
  • Packages offered: DIP16, SO16, SSOP20

Bidirectional I/O Expander Example

Something to remember: 
  • PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot output high, if you want). Look at the above example how is connected the LED for SINKING current.
  • Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
  • Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
  • Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)

For more details please take a look at the PCF8574 Datasheet



For easy testing the PCF8574 8Bit Port Output Pins we will use a very simple "Ghetto-Tester" based on 8 LED's and corresponding current limiting resitors:




What we will need:



    Connection with the ESP8266 nEXT EVO Board is very easy, as Analog Extension Board - AN1 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 :






    By default, on the AN-1 Board you should have the following available devices:
  • PCF8574 8Bit I/O Extension port  - at address 0x20
  • LM75 Temperature sensor             - at address 0x48
  • MCP4728 - 4x12Bit DAC             - at address 0x60
  • MCP3421 - 18Bit ADC                 - at address 0x68
   When also connected, the SSD1306 OLED Display will be available at 0x3C


   A first step in testing the AN-1 available functions and devices will be to scan the nEXT I²C Bus and see if all the existing ones are alive and responding to the I²C Master requests. Also will list any new added devices, if alive.

Using the SCANBUS program described in the Part 2 of the AN-1 Series, you can find very easy if your PCF8574 8Bit I/O Extension port  is available on the nEXT Bus :








 Software implementation


PCF8574 8Bit I/O port is a quasi-bidirectional I/O Port, same as Port 1,2,3 on the 8051 MCU, if you like.

    A quasi-bidirectional I/O is an input or output port without using a direction control register.
Whenever the master reads the register, the value returned to master depends on the
actual voltage or status of the pin. At power on, all the ports are HIGH with a weak 100 uA
internal pull-up to VDD, but can be driven LOW by an internal transistor, or an external
signal. The I/O ports are entirely independent of each other, but each I/O octal is controlled by the same read or write data byte.

Advantages of the quasi-bidirectional I/O over totem pole I/O include:

  • Better for driving LEDs since the p-channel (transistor to VDD) is small, which saves die size and therefore cost. LED drive only requires an internal transistor to ground, while the LED is connected to VDD through a current-limiting resistor. Totem pole I/O have both n-channel and p-channel transistors, which allow solid HIGH and LOW output levels without a pull-up resistor — good for logic levels.
  • Simpler architecture — only a single register and the I/O can be both input and output at the same time. Totem pole I/O have a direction register that specifies the port pin direction and it is always in that configuration unless the direction is explicitlychanged.

  • Does not require a command byte. The simplicity of one register (no need for the pointer register or, technically, the command byte) is an advantage in some embedded systems where every byte counts because of memory or bandwidth limitations.


Testing I/O Port Output

There is only one register to control four possibilities of the port pin: Input HIGH, input
LOW, output HIGH, or output LOW.

  • Input HIGH: The master needs to write 1 to the register to set the port as an input mode if the device is not in the default power-on condition. The master reads the register to check the input status. If the external source pulls the port pin up to VDD or drives logic 1, then the master will read the value of 1.

  • Input LOW: The master needs to write 1 to the register to set the port to input mode if the device is not in the default power-on condition. The master reads the register to check the input status. If the external source pulls the port pin down to VSS or drives logic 0, which sinks the weak 100uA current source, then the master will read the value of 0.

  • Output HIGH: The master writes 1 to the register. There is an additional ‘accelerator’ or strong pull-up current when the master sets the port HIGH. The additional strong pull-up is only active during the HIGH time of the acknowledge clock cycle. This accelerator current helps the port’s 100uA current source make a faster rising edge into a heavily loaded output, but only at the start of the acknowledge clock cycle to avoid bus contention if an external signal is pulling the port LOW to VSS/driving the port with logic 0 at the same time. After the half clock cycle there is only the 100uA current source to hold the port HIGH.
  • Output LOW: The master writes 0 to the register. There is a strong current sink transistor that holds the port pin LOW. A large current may flow into the port, which could potentially damage the part if the master writes a 0 to the register and an external source is pulling the port HIGH at the same time.

Simple quasi-bidirectional I/O example


    In our case, to light-up our LED's we will use the last option, Output LOW!


  1. Init I2C bus/interface

         -- init I2C nEXT BUS
       function i2c_init()
            i2c.setup(id, sda, scl, i2c.SLOW)
      end



2. Set PCF8574 PORT Register Function
function setPort( port, stat) 
    i2c.start(id)
    i2c.address(id, dev_addr ,i2c.TRANSMITTER)
    i2c.write(id,stat)
    i2c.stop(id)
end

3. Set Port function 

 Just a nicer way to write data to PCF8574 Register. Remember that we need to write a "ZERO" to the corresponding bit. We are sinking not sourcing !!
function setPortdata(p)
    pp = 255-p
    setPort(0x20,pp)
end

4. Main Program

i2c_init()

-- Direct port bit by bit set
setPortdata(0)   -- All OFF  
setPortdata(1)   -- P0 - ON     
setPortdata(2)   -- P1 - ON     
setPortdata(4)   -- P2 - ON
setPortdata(8)   -- P3 - ON
setPortdata(16)  -- P4 - ON  
setPortdata(32)  -- P5 - ON
setPortdata(64)  -- P6 - ON
setPortdata(128) -- P7 - ON 

-- mixed bit set - set 2 or more bits at the same time. Do not exceed max sink current!
setPortdata(3) -- P0 & P1 - ON 


-- test autoincrement bit
i=1
tmr.alarm( 0, 200, 1, function()
  print(i)
  setPortdata(i)
  i=i+i
  if i>128 then i=1
  end
end)

tmr.stop(0)


-- cycle visual step-by-step, from left-to-right
port={1,2,4,8,128,64,32,16}
i=1
tmr.alarm( 0, 100, 1, function()
  print(port[i])
  setPortdata(port[i])
  i=i+1
  if i>8 then i=1
  end
end)


-- Knight Rider style test - anybody remember about Knight Rider  KITT car?:)
port={1,2,4,8,128,64,32,16}
i=1
c=1
tmr.alarm( 0, 75, 1, function()
  --print(port[i])
  setPortdata(port[i])
  if c<8 then i=i+1
  else
      if c>14 then
                   c=1
                   i=2
       else
            i=i-1
       end
  end
  --print(port[i].."  i="..i.."  c="..c)
  c=c+1
end)


That's all for today, next time, PCF8574 Input, and a nice 4x4 Matrix Keyboard example :)