|
00001 /*!@addtogroup other 00002 * @{ 00003 * @defgroup max127 MAXIM MAX127 ADC 00004 * MAXIM MAX127 ADC 00005 * @{ 00006 */ 00007 00008 /* 00009 * $Id: MAX127-driver.h 48 2011-02-13 20:35:38Z xander $ 00010 */ 00011 00012 #ifndef __MAX127_H__ 00013 #define __MAX127_H__ 00014 /** \file MAX127-driver.h 00015 * \brief MAXIM MAX127 ADC driver 00016 * 00017 * HTIRS-driver.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 2.00 AND HIGHER. 00026 * \author Xander Soldaat (mightor_at_gmail.com) 00027 * \date 08 March 2009 00028 * \version 0.5 00029 * \example MAX127-test1.c 00030 */ 00031 00032 #pragma systemFile 00033 00034 #ifndef __COMMON_H__ 00035 #include "common.h" 00036 #endif //__COMMON_H__ 00037 00038 #define MAX127_I2C_ADDR 0x50 /*!< MAX127 default I2C device address */ 00039 00040 tByteArray MAX127_I2CRequest; /*!< Array to hold I2C command data */ 00041 tByteArray MAX127_I2CReply; /*!< Array to hold I2C reply data */ 00042 00043 int MAX127readChan(tSensors link, byte i2caddress, byte adcchannel); 00044 00045 /** 00046 * Returns the current analogue value as measured on the specified channel. 00047 * @param link the MAX127 port number 00048 * @param i2caddress the I2C address the MAX127 is configured for. Use MAX127_I2C_ADDR for the default. 00049 * @param adcchannel the ADC channel number (0-7) 00050 * @return value of the ADC channel or -1 if an error occurred. 00051 */ 00052 int MAX127readChan(tSensors link, byte i2caddress, byte adcchannel) { 00053 int _chVal = 0; 00054 00055 memset(MAX127_I2CRequest, 0, sizeof(tByteArray)); 00056 00057 MAX127_I2CRequest[0] = 2; // Message size 00058 MAX127_I2CRequest[1] = i2caddress; // I2C Address 00059 MAX127_I2CRequest[2] = (1 << 7) + (adcchannel << 4); // Control Byte 00060 // Control byte is calculated as follows: 00061 // start bit (7) + channel number bit-shifted into place 00062 00063 if (!writeI2C(link, MAX127_I2CRequest, 2)) 00064 return -1; 00065 00066 if (!readI2C(link, MAX127_I2CReply, 2)) 00067 return -1; 00068 00069 // Convert the bytes into ints 00070 // 1st byte contains bits 11-4 of the channel's value 00071 // 2nd byte contains bits 3-0 of the channel's value, followed by 4 0's 00072 // We'll need to shift the 1st byte left by 4 and the 2nd byte to the right by 4 00073 _chVal = ((int)(MAX127_I2CReply[0]) << 4) + ((int)MAX127_I2CReply[1] >> 4); 00074 00075 return _chVal; 00076 } 00077 #endif // __MAX127_H__ 00078 00079 /* 00080 * $Id: MAX127-driver.h 48 2011-02-13 20:35:38Z xander $ 00081 */ 00082 /* @} */ 00083 /* @} */