Labmbed device drivers

Dependencies:   TextLCD mbed

Revision:
2:caa0e6b2b436
Parent:
1:04e1ee8faa04
Child:
3:8eee79f59b30
--- a/main.cpp	Wed Jan 11 17:03:19 2017 +0000
+++ b/main.cpp	Thu Jan 12 09:16:08 2017 +0000
@@ -3,49 +3,48 @@
 #include "mbed.h"
 #include "TextLCD.h"
 
-TextLCD lcd(p15, p16, p17, p18, p19, p20); // LCD: RS, E, D4-D7
-SPI spi(p5, p6, p7);                       // SPI: MOSI, MISO, SCLK (MISO not used with LCD)
-DigitalOut lat(p8);
-I2C i2c(p9, p10);                          // I2C: SDA, SCL
+TextLCD lcd(p15, p16, p17, p18, p19, p20);          // LCD: RS, E, D4-D7
+SPI spi(p5, p6, p7);                                // SPI: MOSI, MISO, SCLK (MISO not used with LCD)
+DigitalOut lat(p8);                                 // data latch for LED driver TLC59281
+I2C i2c(p9, p10);                                   // I2C: SDA, SCL
 
-int led_bits = 0;                          // global LED status used for readback
-const int tmp102addr = 0x92;               // TMP102 temperature I2C address
+int led_bits = 0;                                   // global LED status used for readback
+const int tmp102addr = 0x92;                        // TMP102 temperature I2C address
 
 
 void initleds() {
-    lat = 0;                               // latch must start low
-    spi.format(16,0);                      // SPI 16 bit data, low state, high going clock
-    spi.frequency(1000000);                // 1MHz clock rate
+    lat = 0;                                        // latch must start low
+    spi.format(16,0);                               // SPI 16 bit data, low state, high going clock
+    spi.frequency(1000000);                         // 1MHz clock rate
 }
 
 void setleds(int ledall) {
-    led_bits = ledall;                     // update global LED status
+    led_bits = ledall;                              // update global LED status
     spi.write((led_bits & 0x03ff) | ((led_bits & 0xa800) >> 1) | ((led_bits & 0x5400) << 1));
-    lat = 1;                               // latch pulse start 
-    lat = 0;                               // latch pulse end
+    lat = 1;                                        // latch pulse start 
+    lat = 0;                                        // latch pulse end
 }
 
 void setled(int ledno, int ledstate) {
-    ledno = ((ledno - 1) & 0x0007) + 1;    // limit led number
-    ledno = (8 - ledno) * 2;               // offset of led state in 'led_bits'
-    ledstate = ledstate & 0x0003;          // limit led state
+    ledno = ((ledno - 1) & 0x0007) + 1;             // limit led number
+    ledno = (8 - ledno) * 2;                        // offset of led state in 'led_bits'
+    ledstate = ledstate & 0x0003;                   // limit led state
     ledstate = ledstate << ledno;
-    int statemask = 0x0003;                // mask used to clear led state
-    statemask = ((statemask << ledno) ^ 0xffff);    // shift and invert statemask
+    int statemask = ((0x0003 << ledno) ^ 0xffff);   // mask used to clear led state
     led_bits = ((led_bits & statemask) | ledstate); // clear and set led state
     setleds(led_bits);
 }
 
 int readled(int ledno) {
-    ledno = ((ledno - 1) & 0x0007) + 1;    // limit led number
-    ledno = (8 - ledno) * 2;               // offset of led state in 'led_bits'
+    ledno = ((ledno - 1) & 0x0007) + 1;             // limit led number
+    ledno = (8 - ledno) * 2;                        // offset of led state in 'led_bits'
     int ledstate = led_bits;
-    ledstate = ledstate >> ledno;
-    return (ledstate & 0x0003);    
+    ledstate = ledstate >> ledno;                   // shift selected led state into ls 2 bits
+    return (ledstate & 0x0003);                     // mask out and return led state
 }
 
 int readleds() {
-    return led_bits;
+    return led_bits;                                // return LED status
 }
 
 //int readswitch(int switchno) {
@@ -64,15 +63,16 @@
 //}
 
 float readtemp() {
-    char cmd[2];
-    cmd[0] = 0x01;
-    cmd[1] = 0x00;
-    i2c.write(tmp102addr, cmd, 2);
-    wait(0.5);
-    cmd[0] = 0x00;
-    i2c.write(tmp102addr, cmd, 1);
-    i2c.read(tmp102addr, cmd, 2);
-    return (float((cmd[0]<<8)|cmd[1]) / 256.0);    
+    char cmd[3];
+    cmd[0] = 0x01;                                  // pointer register value
+    cmd[1] = 0x60;                                  // byte 1 of the configuration register
+    cmd[2] = 0xa0;                                  // byte 2 of the configuration register
+    i2c.write(tmp102addr, cmd, 3);                  // select configuration register and write 0x60a0 to it
+    wait(0.5);                                      // wait for conversion
+    cmd[0] = 0x00;                                  // pointer register value
+    i2c.write(tmp102addr, cmd, 1);                  // select temperature register
+    i2c.read(tmp102addr, cmd, 2);                   // read 16-bit temperature register 
+    return (float((cmd[0]<<8)|cmd[1]) / 256.0);     // divide by 256 and return temperature
 }
 
 int main() {
@@ -82,10 +82,14 @@
     while(1) {
         int a,b;
         for (b = 0; b < 4; b++ ) {
-            for (a = 1; a < 17; a++ ) {
+            for (a = 1; a < 9; a++ ) {
                 setled (a,b);
                 wait(.5);
             }
         }
+    float temp = readtemp();
+    lcd.printf("                \n");   
+    lcd.printf("Temp = %f\n", temp);
+    wait(3);
     }
 }