热释电+光强

Dependencies:   mbed

Fork of sensors by BieWang

Files at this revision

API Documentation at this revision

Comitter:
xmwmx
Date:
Wed Sep 12 15:51:26 2018 +0000
Parent:
0:6dca851b4779
Commit message:
???+??

Changed in this revision

example.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
sensors.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example.cpp	Wed Sep 12 15:51:26 2018 +0000
@@ -0,0 +1,35 @@
+#include "sensors.h"
+#include "mbed.h"
+
+Serial usb2pc(PA_2, PA_3, 9600);  
+DigitalOut myled(PC_13);
+
+int main()
+{
+
+    
+    usb2pc.printf("starting\r\n");
+    sr501 x(PB_1);
+    BH1750 y(PB_7,PB_6);
+    
+    while(1)
+    {
+        /*
+        if(x.operator ==(true))
+        {
+            usb2pc.printf("get\r\n");
+            while(x.read()){usb2pc.printf("aaaaaaaaaaaaa\r\n");wait(0.1);myled=1;}
+            x.reset();
+        }
+        
+        else
+        {
+            usb2pc.printf("no\r\n");
+        }
+        wait(0.1);
+        */
+        
+        float light=y.getlightdata();
+        usb2pc.printf("Light intensity: %.4f Lux\r\n",light);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 12 15:51:26 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a97add6d7e64
\ No newline at end of file
--- a/sensors.cpp	Sun Sep 09 13:44:05 2018 +0000
+++ b/sensors.cpp	Wed Sep 12 15:51:26 2018 +0000
@@ -1,29 +1,82 @@
 #include "sensors.h"
 #include "mbed.h"
 
-//extern Serial usb;
-
-void sr501::triggered()
+extern Serial usb2pc;  
+extern DigitalOut myled;
+//============================================================
+void sr501::triggered()                                     //触发中断!!
 {
-    //usb.printf("Triggered!\r\n");
+    usb2pc.printf("sr501 Triggered!\r\n");
     status = true;
 }
-
-sr501::sr501(PinName pSignal)
-    : status(false), signal(pSignal)
+//--------
+sr501::sr501(PinName pSignal)                               //启动热释电!!!
+    : status(false), signal1(pSignal),signal2(pSignal)
 {
-    signal.rise(this, &sr501::triggered);
+    signal1.rise(this, &sr501::triggered);
+    usb2pc.printf("sr501 start!\r\n");
 }
-
-bool sr501::operator ==(const bool &target)
+//-------
+bool sr501::operator==(const bool &target)
 {
     if(status == target)
+    {
         return true;
+    }
     else
+    {
         return false;
+    }
 }
-
+//-------
 void sr501::reset()
 {
     status = false;
 }
+//-------
+int sr501::read()
+{
+    return signal2.read();
+}
+//==========================================
+BH1750::BH1750(PinName sda,PinName scl)                     //启动光强!!!!!(默认设置)
+    :link(sda,scl)                   
+{
+    status = true;
+    char mode[1]={BH1750_CONTINUOUS_HIGH_RES_MODE};
+    usb2pc.printf("modify\r\n");
+    while(status)
+    {
+        status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false);
+        wait_ms(10);
+    }
+    usb2pc.printf("BH1750 start with default mode!\r\n");
+}
+//--------
+BH1750::BH1750(PinName sda,PinName scl,char mode[])           //启动光强!!!!!(自定义设置)
+    :link(sda,scl)         
+{
+    status = true;
+    while(status)
+    {
+        status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false);
+        wait_ms(10);
+    }
+    usb2pc.printf("BH1750 start with customize mode!\r\n");    
+}
+//--------
+float BH1750::getlightdata()                                //读取光强(lux)
+{
+    status = true;
+    status = link.read(BH1750_I2CADDR, rawdata, 2, false);
+    if(!status)
+    {
+        float result = ((rawdata[0]<<8)|rawdata[1])/1.2;
+        return result;
+    }
+    else
+    {
+        usb2pc.printf("BH1750 read fail!\r\n");
+        return -1;
+    }
+}
\ No newline at end of file
--- a/sensors.h	Sun Sep 09 13:44:05 2018 +0000
+++ b/sensors.h	Wed Sep 12 15:51:26 2018 +0000
@@ -2,7 +2,28 @@
 #define SENSORS_H
 
 #include "mbed.h"
-//#include <stdlib.h>
+//+++++++++++++++++++++BH1750+++++++++++++++++++++++++++++
+#define BH1750_I2CADDR 0x46
+#define BH1750_POWER_DOWN 0x00                  // No active state
+#define BH1750_POWER_ON 0x01                    // Wating for measurment command
+#define BH1750_RESET 0x07                       // Reset data register value - not accepted in POWER_DOWN mode
+#define BH1750_CONTINUOUS_HIGH_RES_MODE  0x10   // Start measurement at 1lx resolution. Measurement time is approx 120ms.
+#define BH1750_CONTINUOUS_HIGH_RES_MODE_2  0x11 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
+#define BH1750_CONTINUOUS_LOW_RES_MODE  0x13    // Start measurement at 4lx resolution. Measurement time is approx 16ms.
+
+// Start measurement at 1lx resolution. Measurement time is approx 120ms.
+// Device is automatically set to Power Down after measurement.
+#define BH1750_ONE_TIME_HIGH_RES_MODE  0x20
+
+// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
+// Device is automatically set to Power Down after measurement.
+#define BH1750_ONE_TIME_HIGH_RES_MODE_2  0x21
+
+// Start measurement at 1lx resolution. Measurement time is approx 120ms.
+// Device is automatically set to Power Down after measurement.
+#define BH1750_ONE_TIME_LOW_RES_MODE  0x23
+//++++++++++++++++++++++++++++++++++++++++
+
 
 class sr501
 {
@@ -13,12 +34,29 @@
   private:
 //    DigitalIn signal;
     bool status;
-    InterruptIn signal;
+    InterruptIn signal1;
+    DigitalIn signal2;
     void triggered();
   public:
     sr501(PinName pSignal);
-    bool operator ==(const bool &target);
+    bool operator==(const bool &target);
     void reset();
+    int read();
 };
+//=========================================
+class BH1750
+{
+    
+/*****
 
+*****/
+  private:
+    I2C link;
+    char rawdata[2];
+    bool status;
+  public:
+    BH1750(PinName sda,PinName scl);
+    BH1750(PinName sda,PinName scl,char mode[]);
+    float getlightdata();
+};
 #endif