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

maxim-max127.h

Go to the documentation of this file.
00001 /*!@addtogroup other
00002  * @{
00003  * @defgroup max127 MAXIM MAX127 ADC
00004  * MAXIM MAX127 ADC
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: maxim-max127.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MAX127_H__
00013 #define __MAX127_H__
00014 /** \file maxim-max127.h
00015  * \brief MAXIM MAX127 ADC driver
00016  *
00017  * hitechnic-irseeker-v1.h provides an API for the MAXIM MAX127 ADC.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.5: Major rewrite of code, uses common.h for most functions
00022  *
00023  * License: You may use this code as you wish, provided you give credit where its due.
00024  *
00025  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00026 
00027  * \author Xander Soldaat (xander_at_botbench.com)
00028  * \date 08 March 2009
00029  * \version 0.5
00030  * \example maxim-max127-test1.c
00031  */
00032 
00033 #pragma systemFile
00034 
00035 #ifndef __COMMON_H__
00036 #include "common.h"
00037 #endif //__COMMON_H__
00038 
00039 #define MAX127_I2C_ADDR 0x50     /*!< MAX127 default I2C device address */
00040 
00041 tByteArray MAX127_I2CRequest;    /*!< Array to hold I2C command data */
00042 tByteArray MAX127_I2CReply;      /*!< Array to hold I2C reply data */
00043 
00044 int MAX127readChan(tSensors link, byte i2caddress, byte adcchannel);
00045 
00046 /**
00047  * Returns the current analogue value as measured on the specified channel.
00048  * @param link the MAX127 port number
00049  * @param i2caddress the I2C address the MAX127 is configured for.  Use MAX127_I2C_ADDR for the default.
00050  * @param adcchannel the ADC channel number (0-7)
00051  * @return value of the ADC channel or -1 if an error occurred.
00052  */
00053 int MAX127readChan(tSensors link, byte i2caddress, byte adcchannel) {
00054   int _chVal = 0;
00055 
00056   memset(MAX127_I2CRequest, 0, sizeof(tByteArray));
00057 
00058   MAX127_I2CRequest[0] = 2;                            // Message size
00059   MAX127_I2CRequest[1] = i2caddress;                   // I2C Address
00060   MAX127_I2CRequest[2] = (1 << 7) + (adcchannel << 4); // Control Byte
00061   // Control byte is calculated as follows:
00062   // start bit (7) + channel number bit-shifted into place
00063 
00064   if (!writeI2C(link, MAX127_I2CRequest, MAX127_I2CReply, 2))
00065     return -1;
00066 
00067   // Convert the bytes into ints
00068   // 1st byte contains bits 11-4 of the channel's value
00069   // 2nd byte contains bits 3-0 of the channel's value, followed by 4 0's
00070   // We'll need to shift the 1st byte left by 4 and the 2nd byte to the right by 4
00071   _chVal = ((int)(MAX127_I2CReply[0]) << 4) + ((int)MAX127_I2CReply[1] >> 4);
00072 
00073   return _chVal;
00074 }
00075 #endif // __MAX127_H__
00076 
00077 /*
00078  * $Id: maxim-max127.h 133 2013-03-10 15:15:38Z xander $
00079  */
00080 /* @} */
00081 /* @} */