Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

PCF8574-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup other
00002  * @{
00003  * @defgroup pcf8574 Philips PCF8574
00004  * Philips PCF8574
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: PCF8574-driver.h 46 2011-01-16 16:56:49Z xander $
00010  */
00011 
00012 #ifndef __PCF8574_H__
00013 #define __PCF8574_H__
00014 /** \file PCF8574-driver.h
00015  * \brief Philips PCF8574 IO MUX driver
00016  *
00017  * PCF8574-driver.h provides an API for the Philips PCF8574 IO MUX.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00028  * \author Xander Soldaat (mightor_at_gmail.com)
00029  * \date 30 March 2010
00030  * \version 0.1
00031  * \example PCF8574-test1.c
00032  */
00033 
00034 #pragma systemFile
00035 
00036 #ifndef __COMMON_H__
00037 #include "common.h"
00038 #endif
00039 
00040 #define PCF8574_I2C_ADDR         0x70  /*!< HDMMUX I2C device address */
00041 
00042 
00043 tByteArray PCF8574_I2CRequest;    /*!< Array to hold I2C command data */
00044 tByteArray PCF8574_I2CReply;      /*!< Array to hold I2C reply data */
00045 
00046 // Function prototypes
00047 bool PCF8574sendBytes(tSensors link, ubyte _byte);
00048 bool PCF8574readBytes(tSensors link, ubyte &_byte);
00049 
00050 /**
00051  * Send a byte to the PCF8574 to set the IO ports
00052  *
00053  * @param link the PCF8574 port number
00054  * @param _byte the byte to be sent
00055  * @return true if no error occured, false if it did
00056  */
00057 bool PCF8574sendBytes(tSensors link, ubyte _byte) {
00058   memset(PCF8574_I2CRequest, 0, sizeof(tByteArray));
00059 
00060   PCF8574_I2CRequest[0] = 2;               // Message size
00061   PCF8574_I2CRequest[1] = PCF8574_I2C_ADDR; // I2C Address
00062   PCF8574_I2CRequest[2] = _byte;
00063 
00064   return writeI2C(link, PCF8574_I2CRequest, 0);
00065 }
00066 
00067 
00068 /**
00069  * Read the current state of the ports on the PCF8574
00070  *
00071  * @param link the PCF8574 port number
00072  * @param _byte the byte thats been read
00073  * @return true if no error occured, false if it did
00074  */
00075 bool PCF8574readBytes(tSensors link, ubyte &_byte) {
00076   memset(PCF8574_I2CRequest, 0, sizeof(tByteArray));
00077 
00078   PCF8574_I2CRequest[0] = 1;               // Message size
00079   PCF8574_I2CRequest[1] = PCF8574_I2C_ADDR; // I2C Address
00080   PCF8574_I2CRequest[2] = _byte;
00081 
00082   if (!writeI2C(link, PCF8574_I2CRequest, 1))
00083     return false;
00084 
00085   if (!readI2C(link, PCF8574_I2CReply, 1))
00086     return false;
00087 
00088   _byte = PCF8574_I2CReply[0];
00089 
00090   return true;
00091 
00092 }
00093 
00094 
00095 #endif //  __PCF8574_H__
00096 
00097 /*
00098  * $Id: PCF8574-driver.h 46 2011-01-16 16:56:49Z xander $
00099  */
00100 /* @} */
00101 /* @} */