Wednesday, March 20, 2013

PCF8574 GPIO expander library for AVR Atmega - v01

The PCF8574 is a silicon CMOS circuit. It provides general purpose remote I/O expansion for most microcontroller families via the two-line bidirectional bus (I2C-bus).


PCF8574 lib is an AVR Atmega library for the PCF8574 i2c IO port expander. This library can run up to 8 PCF8574, it can read and write port status, and allows you to expand your Atmega ports.

Setup parameters can be found in file pcf8574.h

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.


Code


Notes
  • read risk disclaimer
  • excuse my bad english

2 comments:

  1. Hello Davide,
    I recently bought an i2c expander uzing PCF8574A It works fine in Arduino environment. But, I would like to use it in a more complex project which I develop using Atmel Studio 7 and plain C. No success up to now.
    To test it I put an LED and a resistor on each of its outputs. I use PCF8574 GPIO expander library and following parameters:
    F_CPU=16000000
    SCL_CLOCK 100000
    PCF8574_ADDRBASE (0x3F)

    #include
    #include
    #include
    #include
    #include

    #include "pcf8574/pcf8574.h"
    #include "uart/uart.h"

    #define UART_BAUD_RATE 19200


    int main(void)
    {
    char buf[10];

    //init uart
    uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));

    //init interrupt
    sei();

    uart_puts("Init UART OK\r\n");

    //init pcf8574
    pcf8574_init();
    uart_puts("Init i2c OK, TWBR = ");
    itoa(TWBR, buf, 10);
    uart_puts(buf);
    uart_puts("\r\n");
    uart_puts("Init PCF8574 OK\r\n");
    uart_puts("PCF8574_ADDRBASE = ");
    itoa(PCF8574_ADDRBASE, buf, 10);
    uart_puts(buf);
    uart_puts("\r\n");

    //test output
    pcf8574_setoutput(0, 0);

    pcf8574_setoutputpinhigh(0, 0);
    _delay_ms(2000);
    pcf8574_setoutputpinhigh(0, 1);
    _delay_ms(2000);
    pcf8574_setoutputpinhigh(0, 2);
    _delay_ms(2000);
    }

    The program compiles fine and the output is:

    Init UART OK
    Init i2c OK, TWBR = 72
    Init PCF8574 OK
    PCF8574_ADDRBASE = 63 /0x3F in hex/

    No LED is on.
    Any help is appreciated.
    Pop

    ReplyDelete
    Replies
    1. Hello Pop, you should debug with a logic analyzer the PCF8574 bus. If you do not have one, debut it using simple serial uart output. This project it is build using avrgcc. Also please, if you have to post long lines of codes, use something like pastebin.

      Delete