printer

Dependencies:   HCSR04 USBDevice mbed-src mbed mbed

Fork of Serial_HelloWorld_Mbed by mbed official

Files at this revision

API Documentation at this revision

Comitter:
redplam
Date:
Mon Apr 14 14:38:46 2014 +0000
Parent:
0:879aa9d0247b
Commit message:
printer

Changed in this revision

DS18B20.cpp Show annotated file Show diff for this revision Revisions of this file
DS18B20.h Show annotated file Show diff for this revision Revisions of this file
DS1Wire.cpp Show annotated file Show diff for this revision Revisions of this file
DS1Wire.h Show annotated file Show diff for this revision Revisions of this file
HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS18B20.cpp	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,96 @@
+#include "DS18B20.h"
+#include "DS1Wire.h"
+#include "mbed.h"
+#include <stdint.h>
+
+// Device byte commands over 1-wire serial
+enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE,  SKIP_ROM = 0xCC };
+
+// device onboard register layout
+typedef struct {
+    uint8_t    LSB;
+    uint8_t    MSB;
+    uint8_t    Th;
+    uint8_t    Tl;
+    uint8_t    config;
+    uint8_t    reserved0xFF;
+    uint8_t    reserved0xCH;
+    uint8_t    reserved0x10;
+    uint8_t    CRC;
+} ScratchPad_t;
+
+
+DigitalOut conversionInProgress(LED4);  // conversion in progress
+DigitalOut resetFailure(LED1);          // for error reporting
+extern DigitalInOut sensor;     // sensor pin
+
+static void inError() {
+    while (1) {
+        resetFailure = !resetFailure;
+        wait(0.2);
+    }
+}
+
+void DoConversion() {
+    if (Reset(sensor) != 0) {
+        inError();
+    } else {
+        conversionInProgress = 1;       // led on
+        WriteByte(sensor, SKIP_ROM);            // Skip ROM
+        WriteByte(sensor, CONVERT);             // Convert
+        while (ReadBit(sensor) == 0) {
+            // wait for conversion to complete
+        }
+        conversionInProgress = 0;       // led off
+    }
+}
+
+uint32_t GetTemperature() {
+    uint32_t result = 0;
+    if (Reset(sensor) != 0) {
+        inError();
+    } else {
+        ScratchPad_t scratchpad;
+        WriteByte(sensor, SKIP_ROM);    // Skip ROM
+        WriteByte(sensor, READ_SCRATCHPAD);    // Read Scrachpad
+        scratchpad.LSB = ReadByte(sensor);
+        scratchpad.MSB = ReadByte(sensor);
+        Reset(sensor);    // terminate read as we only want temperature
+        result = ((scratchpad.MSB << 8) | scratchpad.LSB);
+    }
+    return result;
+}
+
+ROM_Code_t ReadROM() {
+    ROM_Code_t ROM_Code;
+    if (Reset(sensor) != 0) {
+        inError();
+    } else {
+        
+        WriteByte(sensor, READ_ROM);    // Read ROM
+        for (uint32_t i = 0; i < 8; ++i) {
+            ROM_Code.rom[i] = ReadByte(sensor);
+        }
+    }
+    return ROM_Code;
+}
+
+// temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
+void displayTemperature(Serial& s) {
+    DoConversion();
+    uint32_t temp = GetTemperature();
+    float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
+    f += (temp >> 4);    // add 7.0 part to it
+    s.printf("Temp is %2.1fC\n\r", f);    // display in 2.1 format
+}
+
+// temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
+void showTemperature(float *f) {
+    DoConversion();
+    uint32_t temp = GetTemperature();
+    *f = (temp & 0x0F) * 0.0625;    // calculate .4 part
+    *f += (temp >> 4);    // add 7.0 part to it
+    //return(f);
+    //s.printf("Temp is %2.1fC\n\r", f);    // display in 2.1 format
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS18B20.h	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,23 @@
+#ifndef _DS18B20_
+#define _DS18B20_
+
+#include <stdint.h>
+#include "mbed.h"
+
+// Device Faimly ID and Setial number information
+typedef union {
+    uint8_t rom[8];
+    struct {
+        uint8_t    familyCode;
+        uint8_t    serialNo[6];
+        uint8_t    CRC;
+    } BYTES;
+} ROM_Code_t;
+
+ROM_Code_t ReadROM() ;
+
+// temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
+void displayTemperature(Serial& s) ;
+void showTemperature(float *f);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1Wire.cpp	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,60 @@
+#include "DS1Wire.h"
+#include "mbed.h"
+#include <stdint.h>
+
+// Timing delay for 1-wire serial standard option
+enum DELAY { A = 6, B = 64, C = 60, D = 10, E = 9, F = 55, G = 0, H = 480, I = 70, J = 410 };
+
+   
+int Reset(DigitalInOut& pin) {
+    pin.output();
+    pin = 0;    // drive bus low
+    wait_us(H);
+    pin.input(); // release bus
+    wait_us(I);
+    uint32_t result = pin;  // read bus value
+    wait_us(J);
+    return result;
+}
+
+void WriteBit(DigitalInOut& pin, uint32_t bit) {
+    pin.output();
+    if (bit) {
+        pin = 0;        // drive bus low
+        wait_us(A);        // delay A
+        pin.input();      // release bus
+        wait_us(B);        // delay B
+    } else {
+        pin = 0;    // drive bus low
+        wait_us(C);    // delay C
+        pin.input();  // release bus
+        wait_us(D);    // delay D
+    }
+}
+
+uint32_t ReadBit(DigitalInOut& pin) {
+    uint32_t bit_value;
+    pin.output();
+    pin = 0;        // drive bus low
+    wait_us(A);        // delay A
+    pin.input();      // release bus
+    wait_us(E);        // delay E
+    bit_value  = pin;    // master sample bus
+    wait_us(F);
+    return bit_value;
+}
+
+void WriteByte(DigitalInOut& pin, uint32_t byte) {
+    for (uint32_t bit = 0; bit < 8; ++bit) {
+        WriteBit(pin, byte & 0x01); // lsb to msb
+        byte >>= 1;    // right shift by 1-bit
+    }
+}
+
+uint32_t ReadByte(DigitalInOut& pin) {
+    uint32_t byte = 0;
+    for (uint32_t bit = 0; bit < 8; ++bit) {
+        byte |= (ReadBit(pin) << bit);    // Reads lsb to msb
+    }
+    return byte;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1Wire.h	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,15 @@
+#ifndef __DS_1_WIRE__
+#define __DS_1_WIRE__
+#include <stdint.h>
+#include "mbed.h"
+
+int Reset(DigitalInOut& pin);
+
+void WriteBit(DigitalInOut& pin, uint32_t bit);
+uint32_t ReadBit(DigitalInOut& pin);
+
+void WriteByte(DigitalInOut& pin, uint32_t byte);
+uint32_t ReadByte(DigitalInOut& pin);
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/srsmitherman/code/HCSR04/#3ebde19131af
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#335f2506f422
--- a/main.cpp	Tue Feb 12 17:39:05 2013 +0000
+++ b/main.cpp	Mon Apr 14 14:38:46 2014 +0000
@@ -1,10 +1,102 @@
+//usb serial at 1200,8,n,1
 #include "mbed.h"
- 
-Serial pc(USBTX, USBRX); // tx, rx
+#include <stdint.h>
+#include "DS18B20.h"
+#include "HCSR04.h"
+
+#define     BAUDRATE0       1200
+#define     BAUDRATE1       2400
+#define     BAUDRATE3       2400
+
+HCSR04  rangeFinder(PTA1,PTA2);
+Serial pcomp(USBTX, USBRX); // tx, rx
+DigitalInOut sensor(PTE0);     // sensor connected to pin 5
+DigitalOut StopLedRed(LED_RED);     //for testing purposes STOP
+AnalogIn ain(PTB0);
+Serial pc(PTC4,PTC3);
+int cell;
+char aa;
+float temp=0;
+float range;
+
+void test (void)
+{
+    cell=123;
+  pcomp.printf("test\n");
+
+    pc.printf("TESTINRT0\n");
+
+  //  pc1.printf("TESTING0\n");
+
+}
+void analog(void)
+{
+    while (1) {
+                pc.printf("\n\r=====================================================\n\r");
+
+        pc.printf("analog in = %f\n", ain.read());
+        if(ain > 0.3) {
+            StopLedRed = 1;
+        } else {
+            StopLedRed = 0;
+        }
+    }
+}
+void dist(void){
+                rangeFinder.startMeas();
+                wait(0.1);
+                if ( rangeFinder.getMeas(range) == RANGE_MEAS_VALID )
+                {
+                    pcomp.printf("range = %f\n\r", range);
+                }
+    }
+    void temp1(void){
+        pc.printf("\n\r=====================================================\n\r");
+    pc.printf("DS18B20 Configuration\n\r");
+    sensor.mode(PullUp);
+    
+    ROM_Code_t ROM_Code = ReadROM();
+    pcomp.printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode);
+    pcomp.printf("Serial Number: ");
+    for (uint32_t i = 6; i != 0; --i) {
+        pcomp.printf("%02X%s", ROM_Code.BYTES.serialNo[i-1], (i != 1)?":":"\r\n");
+    }
+    pcomp.printf("CRC: 0x%X\r\n", ROM_Code.BYTES.CRC);
+    
+    pcomp.printf("\n\rRunning temperature conversion...\n\r");
+}
+int main()
+{   
+    pcomp.baud(BAUDRATE3);
+
  
-int main() {
-    pc.printf("Hello World!\n");
+    pc.baud(BAUDRATE0);
+
+   pc.format(8,Serial::None,1);
+
+    //pc.printf("dddHello World!\n");
+    //wait(2);
+      //  pc.printf("Family");
+      //  wait(2);
+      //  pc1.printf("1111Family");
+      //  wait(2);
+      //  pcomp.printf("333Family");
+      //  wait(2);
     while(1) {
-        pc.putc(pc.getc() + 1);
+             test();
+             // dist();
+              // analog();
+              // wait(2);
+     //          displayTemperature(pcomp);
+     //              showTemperature(&temp);
+     //              pcomp.printf("%.2f C",temp);
+      // analog();
+        ////pc.printf("serial 0 Hello from me\n");
+        ////pc1.printf("serial 2 how are you\n");
+       wait(5.0);
+        //test();
+        //  aa=pc.getc();
+        // pc.printf( "%c", aa,"\n");
+        // pc.putc(pc.getc() + 1);pc.printf("\n");
     }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#b5886236e6f0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Mon Apr 14 14:38:46 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/redplam/code/mbed/#0b0a5a73f998