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

philips-pcf8574.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: philips-pcf8574.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __PCF8574_H__
00013 #define __PCF8574_H__
00014 /** \file philips-pcf8574.h
00015  * \brief Philips PCF8574 IO MUX driver
00016  *
00017  * philips-pcf8574.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 3.59 AND HIGHER. 
00028 
00029  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 30 March 2010
00031  * \version 0.1
00032  * \example philips-pcf8574-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define PCF8574_I2C_ADDR         0x70  /*!< HDMMUX I2C device address */
00042 
00043 
00044 tByteArray PCF8574_I2CRequest;    /*!< Array to hold I2C command data */
00045 tByteArray PCF8574_I2CReply;      /*!< Array to hold I2C reply data */
00046 
00047 // Function prototypes
00048 bool PCF8574sendBytes(tSensors link, ubyte _byte);
00049 bool PCF8574readBytes(tSensors link, ubyte &_byte);
00050 
00051 /**
00052  * Send a byte to the PCF8574 to set the IO ports
00053  *
00054  * @param link the PCF8574 port number
00055  * @param _byte the byte to be sent
00056  * @return true if no error occured, false if it did
00057  */
00058 bool PCF8574sendBytes(tSensors link, ubyte _byte) {
00059   memset(PCF8574_I2CRequest, 0, sizeof(tByteArray));
00060 
00061   PCF8574_I2CRequest[0] = 2;               // Message size
00062   PCF8574_I2CRequest[1] = PCF8574_I2C_ADDR; // I2C Address
00063   PCF8574_I2CRequest[2] = _byte;
00064 
00065   return writeI2C(link, PCF8574_I2CRequest);
00066 }
00067 
00068 
00069 /**
00070  * Read the current state of the ports on the PCF8574
00071  *
00072  * @param link the PCF8574 port number
00073  * @param _byte the byte thats been read
00074  * @return true if no error occured, false if it did
00075  */
00076 bool PCF8574readBytes(tSensors link, ubyte &_byte) {
00077   memset(PCF8574_I2CRequest, 0, sizeof(tByteArray));
00078 
00079   PCF8574_I2CRequest[0] = 1;               // Message size
00080   PCF8574_I2CRequest[1] = PCF8574_I2C_ADDR; // I2C Address
00081   PCF8574_I2CRequest[2] = _byte;
00082 
00083   if (!writeI2C(link, PCF8574_I2CRequest, PCF8574_I2CReply, 1))
00084     return false;
00085 
00086   _byte = PCF8574_I2CReply[0];
00087 
00088   return true;
00089 
00090 }
00091 
00092 
00093 #endif //  __PCF8574_H__
00094 
00095 /*
00096  * $Id: philips-pcf8574.h 133 2013-03-10 15:15:38Z xander $
00097  */
00098 /* @} */
00099 /* @} */