Thermo Sensor(TMP102 & LM61BIZ) test code ported code from Arduino. you can see the difference between mbed and Arduino in the code.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 //
00003 // Thermo Sensor(TMP102 & LM61BIZ) test code
00004 //
00005 // ported code from Arduino
00006 // you can see the difference between mbed and Arduino in the code.
00007 
00008 // 2010/10/30
00009 // written by:xshige
00010 
00011 /*
00012 
00013 mbed Case:
00014  Wiring Info for TMP102
00015  module side | mbed side
00016  GND --------- GND
00017  SCL --------- p10
00018  SDA --------- p9
00019  V+  --------- +3.3V
00020  ALT --------- NC
00021  ADD0 -------- GND
00022 
00023  Wring Info ofr LM61BIZ
00024  sensor side | mbed side
00025  +Vs --------- +5V or +3.3V
00026  Vout--------- p20 (analog input)
00027  GND --------- GND
00028 
00029 //--------------------------------
00030 
00031 Arduino Case:
00032  Wiring Info for TMP102
00033  module side | Arduino side
00034  GND --------- GND
00035  SCL --------- A5
00036  SDA --------- A4
00037  V+  --------- +3.3V
00038  ALT --------- NC
00039  ADD0 -------- GND
00040 
00041  Wring Info ofr LM61BIZ
00042  sensor side | Arduino side
00043  +Vs --------- +5V or +3.3V
00044  Vout--------- A3 (analog input)
00045  GND --------- GND
00046 
00047 Datasheet:
00048 http://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.pdf
00049 http://www.national.com/ds/LM/LM61.pdf
00050 
00051 */
00052 
00053 #define MBED
00054 
00055 #ifdef MBED
00056 #include "mbed.h"
00057 #else
00058 #include <Wire.h>
00059 #endif
00060 
00061 #ifdef MBED
00062 I2C i2c(p9, p10);        // sda, scl
00063 Serial pc(USBTX, USBRX); // tx, rx
00064 char cmd[2]; // command for I2C
00065 #endif
00066 
00067 // setup for TMP102
00068 #ifdef MBED
00069 #define TEMP_REG 0x0
00070 int tmp102 = 0x90; // with ADD0 tied to ground
00071 // important note: (Arduino_Address<<4)==(mbed_Address)
00072 #else
00073 #define TEMP_REG 0b00000000
00074 int tmp102 = 0b1001000;  // with ADD0 tied to ground
00075 #endif
00076 
00077 // setup for LM61BIZ
00078 #ifdef MBED
00079 AnalogIn LM61pin(p20); // select analog input
00080 #else
00081 int LM61Pin = 3;    // select analog input
00082 #endif
00083 
00084 void setPR(int reg) {
00085 #ifndef MBED // not MBED
00086   Wire.beginTransmission(tmp102);
00087   Wire.send(reg);
00088   Wire.endTransmission();
00089 #endif
00090 }
00091 
00092 int getReg() {
00093   unsigned char lo, hi;
00094 #ifdef MBED
00095     cmd[0] = TEMP_REG; i2c.write(tmp102, cmd, 1); // set temp?reg
00096     i2c.read(tmp102, cmd, 2); // read the two-byte echo result
00097     hi = cmd[0]; lo = cmd[1];
00098 #else
00099   Wire.requestFrom(tmp102, 2);
00100   hi = Wire.receive();
00101   lo = Wire.receive();
00102 #endif
00103   return (hi << 8) + lo;
00104 }
00105 
00106 void setup()   {
00107 #ifndef MBED // not MBED
00108   Serial.begin(9600);    
00109   Wire.begin();
00110   setPR(TEMP_REG);
00111 #endif
00112 }
00113 
00114 void show_TMP102_temp() {
00115   int temp_reg = getReg();
00116   temp_reg >>= 4;
00117 
00118   float temp_C = temp_reg * 0.0625;
00119   float temp_F = (temp_C * 9 / 5) + 32;
00120 
00121 #ifdef MBED
00122   printf("TMP102: %2.1f degree C\r\n",temp_C);
00123   printf("TMP102: %2.1f degree F\r\n",temp_F);
00124 #else
00125   Serial.print("TMP102: ");
00126   Serial.print(temp_C);
00127   Serial.println(" degree C  ");
00128   Serial.print(temp_F);
00129   Serial.println("\xdf""F  ");
00130 #endif
00131 }
00132 
00133 void show_LM61_temp() {
00134    // read the value from the sensor:
00135 #ifdef MBED
00136   float value = 3.3*LM61pin.read_u16()/0xFFFF;  
00137   // in mbed, input range is 0 thru 3.3V
00138 #else   
00139   float value = 5.0*analogRead(LM61Pin)/1023.0; 
00140   // in Arduino, input range is 0 thru 5V in many case 
00141 #endif
00142 
00143   // get degree C form LM61BIZ value
00144   float LM61degree=(value-0.6)/0.01;
00145 
00146 #ifdef MBED
00147   printf("LM61:   %2.1f degree C\r\n",LM61degree);
00148 #else
00149   Serial.print("LM61:   ");
00150   Serial.print(LM61degree);
00151   Serial.println(" degree C");
00152 #endif
00153 }
00154 
00155 void loop() {
00156   show_TMP102_temp();
00157   show_LM61_temp();
00158 #ifdef MBED
00159   printf("\r\n"); 
00160 #endif
00161 }
00162 
00163 #ifdef MBED
00164 void init()
00165 {
00166   // make debug port Fast
00167 //    pc.baud(9600);
00168     pc.baud(115200);
00169 //  pc.baud(230400);
00170 }
00171 
00172 int main(void)
00173 {
00174     init();
00175  
00176     setup();
00177  
00178     for (;;)
00179         loop();
00180  
00181     return 0;
00182 }
00183 #endif