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

hitechnic-superpro.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup HTSPB SuperPro Prototype Board
00004  * HiTechnic SuperPro Prototype Board
00005  * @{
00006  */
00007 /*
00008  * $Id: hitechnic-superpro.h 133 2013-03-10 15:15:38Z xander $
00009  */
00010 
00011 #ifndef __HTSPB_H__
00012 #define __HTSPB_H__
00013 /** \file hitechnic-superpro.h
00014  * \brief HiTechnic SuperPro Prototype Board driver
00015  *
00016  * hitechnic-superpro.h provides an API for the HiTechnic SuperPro Proto Board.
00017  *
00018  * Changelog:
00019  * - 0.1: Initial release
00020  *
00021  * License: You may use this code as you wish, provided you give credit where its due.
00022  *
00023  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00024 
00025  * \author Gustav Jansson (gus_at_hitechnic.com)
00026  * \date 10 October 2011
00027  * \version 0.1
00028  * \example hitechnic-superpro-exp1.c
00029  * \example hitechnic-superpro-exp2.c
00030  * \example hitechnic-superpro-exp3.c
00031  * \example hitechnic-superpro-exp4.c
00032  * \example hitechnic-superpro-exp5.c
00033  * \example hitechnic-superpro-exp6a.c
00034  * \example hitechnic-superpro-exp6b.c
00035  * \example hitechnic-superpro-exp7.c
00036  * \example hitechnic-superpro-exp8.c
00037  * \example hitechnic-superpro-exp9.c
00038  */
00039 
00040 #pragma systemFile
00041 
00042 #ifndef __COMMON_H__
00043 #include "common.h"
00044 #endif
00045 
00046 #define HTSPB_I2C_ADDR 0x10      /*!< Protoboard I2C device address */
00047 #define HTSPB_OFFSET   0x42
00048 #define HTSPB_A0_U     0x00      /*!< Address of upper bits of first ADC, bits 9-2 */
00049 #define HTSPB_A0_L     0x01      /*!< Address of lower bits of first ADC, bits 1-0 */
00050 #define HTSPB_DIGIN    0x0A      /*!< Address of digital inputs */
00051 #define HTSPB_DIGOUT   0x0B      /*!< Address of digital outputs */
00052 #define HTSPB_DIGCTRL  0x0C      /*!< Controls direction of digital ports */
00053 #define HTSPB_STROBE   0x0E      /*!< Address of strobe outputs, bits 6-0 */
00054 #define HTSPB_LED      0x0F      /*!< Address of on-board led outputs, bits 1-0 */
00055 #define HTSPB_O0MODE   0x10      /*!< Address of analog output 0 mode*/
00056 #define HTSPB_O0FREQ   0x11      /*!< Address of analog output 0 frequency*/
00057 #define HTSPB_O0VOLT   0x13      /*!< Address of analog output 0 voltage*/
00058 #define HTSPB_O1MODE   0x15      /*!< Address of analog output 1 mode*/
00059 #define HTSPB_O1FREQ   0x16      /*!< Address of analog output 1 frequency*/
00060 #define HTSPB_O1VOLT   0x18      /*!< Address of analog output 1 voltage*/
00061 
00062 #define HTSPB_DACO0    0x10      /*!< Address of analog parameters output O0 */
00063 #define HTSPB_DACO1    0x15      /*!< Address of analog parameters output O1 */
00064 
00065 
00066 // SuperPro Analog output modes
00067 #define DAC_MODE_DCOUT        0 /*!< Steady (DC) voltage output. */
00068 #define DAC_MODE_SINEWAVE     1 /*!< Sine wave output. */
00069 #define DAC_MODE_SQUAREWAVE   2 /*!< Square wave output. */
00070 #define DAC_MODE_SAWPOSWAVE   3 /*!< Positive going sawtooth output. */
00071 #define DAC_MODE_SAWNEGWAVE   4 /*!< Negative going sawtooth output. */
00072 #define DAC_MODE_TRIANGLEWAVE 5 /*!< Triangle wave output. */
00073 #define DAC_MODE_PWMVOLTAGE   6 /*!< PWM square wave output. */
00074 
00075 
00076 
00077 tByteArray HTSPB_I2CRequest;    /*!< Array to hold I2C command data */
00078 tByteArray HTSPB_I2CReply;      /*!< Array to hold I2C reply data */
00079 
00080 ubyte HTSPBreadIO(tSensors link, ubyte mask);
00081 bool HTSPBwriteIO(tSensors link, ubyte mask);
00082 bool HTSPBsetupIO(tSensors link, ubyte mask);
00083 int HTSPBreadADC(tSensors link, byte channel, byte width);
00084 bool HTSPBreadAllADC(tSensors link, int &adch0, int &adch1, int &adch2, int &adch3, int &adch4, byte width);
00085 bool HTSPBsetSamplingTime(tSensors link, byte interval);
00086 
00087 /**
00088  * Read the values of the digital inputs as specified by the mask.
00089  * @param link the HTSPB port number
00090  * @param mask the specified digital ports
00091  * @return 8 bits representing the state of the specified IOs
00092  */
00093 ubyte HTSPBreadIO(tSensors link, ubyte mask) {
00094   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00095 
00096   HTSPB_I2CRequest[0] = 2;                         // Message size
00097   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;             // I2C Address
00098   HTSPB_I2CRequest[2] = HTSPB_OFFSET + HTSPB_DIGIN;  // Start digital output read address
00099 
00100   if (!writeI2C(link, HTSPB_I2CRequest, HTSPB_I2CReply, 1))
00101     return 0;
00102 
00103   return HTSPB_I2CReply[0] & mask;
00104 }
00105 
00106 
00107 /**
00108  * Write the values the digital outpus as specified by the mask.
00109  * @param link the HTSPB port number
00110  * @param mask the specified digital ports
00111  * @return true if no error occured, false if it did
00112  */
00113 bool HTSPBwriteIO(tSensors link, ubyte mask) {
00114   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00115 
00116   HTSPB_I2CRequest[0] = 3;                         // Message size
00117   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;             // I2C Address
00118   HTSPB_I2CRequest[2] = HTSPB_OFFSET + HTSPB_DIGOUT; // Start digital output read address
00119   HTSPB_I2CRequest[3] = mask;                      // The specified digital ports
00120 
00121 
00122   return writeI2C(link, HTSPB_I2CRequest);
00123 }
00124 
00125 
00126 /**
00127  * Configure the ports for input or output according to the mask.
00128  * @param link the HTSPB port number
00129  * @param mask the specified digital ports, 0 = input, 1 = output
00130  * @return true if no error occured, false if it did
00131  */
00132 bool HTSPBsetupIO(tSensors link, ubyte mask) {
00133   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00134 
00135   HTSPB_I2CRequest[0] = 3;                           // Message size
00136   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;               // I2C Address
00137   HTSPB_I2CRequest[2] = HTSPB_OFFSET + HTSPB_DIGCTRL;  // Start digital input/output control address
00138   HTSPB_I2CRequest[3] = mask;                        // The specified digital ports
00139 
00140   return writeI2C(link, HTSPB_I2CRequest);
00141 }
00142 
00143 
00144 /**
00145  * Read the value of the specified analogue channel.
00146  * @param link the HTSPB port number
00147  * @param channel the specified ADC channel
00148  * @param width the bit width of the result, can be either 8 or 10
00149  * @return the value of the ADC channel, or -1 if an error occurred
00150  */
00151 int HTSPBreadADC(tSensors link, byte channel, byte width) {
00152   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00153 
00154   int _adcVal = 0;
00155   HTSPB_I2CRequest[0] = 2;                                       // Message size
00156   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;                           // I2C Address
00157   HTSPB_I2CRequest[2] = HTSPB_OFFSET + HTSPB_A0_U + (channel * 2); // Start digital output read address
00158                                                                     // with channel offset
00159   if (!writeI2C(link, HTSPB_I2CRequest, HTSPB_I2CReply, 2))
00160     return -1;
00161 
00162   // Convert the bytes into and int
00163   // 1st byte contains bits 9-2 of the channel's value
00164   // 2nd byte contains bits 1-0 of the channel's value
00165   // We'll need to shift the 1st byte left by 2 and or 2nd byte onto it.
00166   // If 8 bits is all we want, we just return the first byte and be done with it.
00167   if (width == 8)
00168     _adcVal = HTSPB_I2CReply[0];
00169   else
00170     _adcVal = (HTSPB_I2CReply[0] * 4) + HTSPB_I2CReply[1];
00171 
00172   return _adcVal;
00173 }
00174 
00175 
00176 
00177 /**
00178  * This function read the value of all of the analogue channels.
00179  * @param link the HTSPB port number
00180  * @param adch0 parameter to hold value for ad channel 0
00181  * @param adch1 parameter to hold value for ad channel 1
00182  * @param adch2 parameter to hold value for ad channel 2
00183  * @param adch3 parameter to hold value for ad channel 3
00184  * @param width the bit width of the result, can be either 8 or 10
00185  * @return true if no error occured, false if it did
00186  */
00187 bool HTSPBreadAllADC(tSensors link, int &adch0, int &adch1, int &adch2, int &adch3, byte width) {
00188   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00189 
00190   HTSPB_I2CRequest[0] = 2;                       // Message size
00191   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;           // I2C Address
00192   HTSPB_I2CRequest[2] = HTSPB_OFFSET + HTSPB_A0_U; // Start digital output read address
00193 
00194   if (!writeI2C(link, HTSPB_I2CRequest, HTSPB_I2CReply, 10))
00195     return false;
00196 
00197   // Convert the bytes into and int
00198   // 1st byte contains bits 9-2 of the channel's value
00199   // 2nd byte contains bits 1-0 of the channel's value
00200   // We'll need to shift the 1st byte left by 2 and or 2nd byte onto it.
00201   // If 8 bits is all we want, we just return the first byte and be done with it.
00202   if (width == 8) {
00203     adch0 = (int)HTSPB_I2CReply[0];
00204     adch1 = (int)HTSPB_I2CReply[2];
00205     adch2 = (int)HTSPB_I2CReply[4];
00206     adch3 = (int)HTSPB_I2CReply[6];
00207   } else {
00208     adch0 = ((int)HTSPB_I2CReply[0] << 2) + (int)HTSPB_I2CReply[1];
00209     adch1 = ((int)HTSPB_I2CReply[2] << 2) + (int)HTSPB_I2CReply[3];
00210     adch2 = ((int)HTSPB_I2CReply[4] << 2) + (int)HTSPB_I2CReply[5];
00211     adch3 = ((int)HTSPB_I2CReply[6] << 2) + (int)HTSPB_I2CReply[7];
00212   }
00213   return true;
00214 }
00215 
00216 
00217 
00218 /**
00219  * Write to the analog output.
00220  * @param link the HTSPB port number
00221  * @param dac the specified analog port, use HTSPB_DACO0 or HTSPB_DACO1
00222  * @param mode the analog mode
00223  * @param freq the analog frequency from 1 to 8193
00224  * @param volt the analog voltage from 0 to 1023 (for 0 to 3.3v)
00225  * @return true if no error occured, false if it did
00226  */
00227 bool HTSPBwriteAnalog(tSensors link, byte dac, byte mode, int freq, int volt) {
00228   memset(HTSPB_I2CRequest, 0, sizeof(tByteArray));
00229 
00230   HTSPB_I2CRequest[0] = 7;                          // Message size
00231   HTSPB_I2CRequest[1] = HTSPB_I2C_ADDR;             // I2C Address
00232   HTSPB_I2CRequest[2] = HTSPB_OFFSET + dac;         // Start analog output write address
00233   HTSPB_I2CRequest[3] = mode;                       // The specified analog mode
00234   HTSPB_I2CRequest[4] = freq/256;                   // High byte of frequency
00235   HTSPB_I2CRequest[5] = freq&255;                   // Low byte of frequency
00236   HTSPB_I2CRequest[6] = volt/4;                     // High 8 bits of voltage
00237   HTSPB_I2CRequest[7] = volt&3;                     // Low 2 bits of voltage
00238 
00239   return writeI2C(link, HTSPB_I2CRequest);
00240 }
00241 
00242 
00243 
00244 #endif // __HTSPB_H__
00245 
00246 /*
00247  * $Id: hitechnic-superpro.h 133 2013-03-10 15:15:38Z xander $
00248  */
00249 /* @} */
00250 /* @} */