w mx
/
sensors3
sr501/BH1750/mq-2/dht11
Fork of sensors_2 by
Revision 4:fe1e9f9c7b33, committed 2018-10-18
- Comitter:
- xmwmx
- Date:
- Thu Oct 18 16:23:13 2018 +0000
- Parent:
- 3:31aec950f7dc
- Commit message:
- 9 kinds of sensors
Changed in this revision
--- a/example.cpp Fri Sep 14 10:52:15 2018 +0000 +++ b/example.cpp Thu Oct 18 16:23:13 2018 +0000 @@ -13,10 +13,14 @@ BH1750 y(PB_7,PB_6); mq z(PA_7,PA_6); dht11 w(PA_5); + DS18B20 v(PC_14); + YL u(PA_1,PA_0); + BMP180 t(PB_9,PB_8); + GP2Y1010 s(PA_8,PB_0); while(1) { - /* + /*sr501 if(x.operator ==(true)) { usb2pc.printf("get\r\n"); @@ -30,11 +34,11 @@ } wait(0.1); */ - /* + /*BH1750 float light=y.getlightdata(); usb2pc.printf("Light intensity: %.4f Lux\r\n",light); */ - /* + /*mq if(z.operator==(true)) { usb2pc.printf("mq on\r\n"); @@ -50,13 +54,45 @@ wait(1.0); // 1 sec } */ - ///* + /*dht11 if(w.getdata()) { usb2pc.printf("Humidity (%):%.3f\r\n",w.gethumidity()); usb2pc.printf("Temperature (oC): %.3f\r\n",w.gettemperature()); } wait(2); - //*/ + */ + /*DS18B20 + if(v.getdata()) + { + usb2pc.printf("Temperature (oC): %.3f\r\n",v.gettemperature()); + } + wait(2); + */ + /*YL-38 + if(u.operator==(true)) + { + usb2pc.printf("YL on\r\n"); + while(!u.read()){usb2pc.printf("%.3f\r\n",u.getairdata());wait(0.1);myled=1;} + myled = 1; // LED is ON + wait(0.2); // 200 ms + u.reset(); + } + else + { + usb2pc.printf("YL off\r\n"); + myled = 0; // LED is OFF + wait(1.0); // 1 sec + } + */ + /*bmp180 + usb2pc.printf("Temperature is %.1f C\r\n", (float)t.BMP180GetTemperature()); + usb2pc.printf("Pressure is %.3f kPa\r\n", (float)t.BMP180GetPressure()/1000.0); + wait(1.0); + */ + /*GP2Y1010 + usb2pc.printf("Dust Density: %.3f \r\n",s.getairdata()) ; + wait(1.0); + */ } } \ No newline at end of file
--- a/sensors.cpp Fri Sep 14 10:52:15 2018 +0000 +++ b/sensors.cpp Thu Oct 18 16:23:13 2018 +0000 @@ -3,7 +3,7 @@ extern Serial usb2pc; extern DigitalOut myled; -//============================================================ +//============================================================SR501 void sr501::triggered() //触发中断!! { usb2pc.printf("sr501 Triggered!\r\n"); @@ -38,7 +38,7 @@ { return signal.read(); } -//========================================== +//========================================== //BH1750 BH1750::BH1750(PinName sda,PinName scl) //启动光强!!!!!(默认设置) :link(sda,scl) { @@ -80,7 +80,7 @@ return -1; } } -//============================================== +//==============================================MQ-2 mq::mq(PinName dio) : status(false), signal(dio),signallevel(PC_13) { @@ -126,7 +126,7 @@ { return signal.read(); } -//=========================================== +//===========================================DHT11 dht11::dht11(PinName pin) :datapin(pin) { @@ -216,4 +216,322 @@ float dht11::gettemperature() { return T; -} \ No newline at end of file +} +//============================================DS18B20 +DS18B20::DS18B20(PinName pin) + :datapin(pin) +{ + if(start()){usb2pc.printf("DS18B20 started\r\n");} +} +//----------- +int DS18B20::start() +{ + //usb2pc.printf("starting\r\n"); + datapin.output(); + datapin=0; + wait_us(600); + datapin.input(); + datapin.mode(PullUp); + wait_us(70); + while(datapin.read()){return 0;} + //usb2pc.printf("started\r\n"); + wait_us(250); + return 1; +} +//---------- +void DS18B20::writebyte(uint8_t send) +{ + datapin.output(); + datapin=1; + wait_us(20); + for(int i=0;i<8;i++) + { + datapin.output(); + datapin=0; //产生读写时序的起始信号 + wait_us(2); //要求至少1us的延时 + datapin=send & 0x01; //对总线赋值,从最低位开始写起 + wait_us(70);//延时70us,写0在60~120us之间释放,写1的话大于60us均可释放 + datapin=1; //释放总线,为下一次mcu送数据做准备, + send>>=1; //有效数据移动到最低位,2次写数据间隙至少需1us + } + datapin.output(); + datapin=1; + wait_us(70); +} +//----------- +uint8_t DS18B20::readByte() //mcu读一个字节 +{ + uint8_t i,value=0; + for(i=0;i<8;i++) + { + datapin.output(); + datapin=0; //起始信号 + wait_us(2); + datapin.input(); //mcu释放总线 + datapin.mode(PullUp); + if(datapin) + { + value=value|0x80;//保存高电平数据,低电平的话不用保存,移位后默认是0 + } + value>>=1; + wait_us(30); //延时40us + } + return value; +} +//------------ +float DS18B20::transfer(uint8_t h,uint8_t l) +{ + //h=0x01; + //l=0x01; + int flag=(h&0x01)>>7; + if(!flag) + { + float i=(h*256+l)*0.25; + //usb2pc.printf("flag:%d\th:%d\tl:%d\r\n",flag,h,l); + return i; + } + else + { + float i=((~h)*256+(~l))*0.25*-1; + //usb2pc.printf("flag:%d\th:%d\tl:%d\r\n",flag,h,l); + return i; + } +} +//---------- +int DS18B20::getdata() +{ + T=5000; + start(); + writebyte(0xcc); + writebyte(0x44); + wait(2); + + start(); + writebyte(0xcc); + writebyte(0xbe); + uint8_t a=readByte();//l + uint8_t b=readByte();//h + T=transfer(b,a); + + if(T==5000){return 0;} + else{return 1;} +} +//---------- +float DS18B20::gettemperature() +{ + if(getdata()){return T;} + else{usb2pc.printf("get temperature fail!\r\n");return 0;} +} +//==============================================YL-38 +YL::YL(PinName dio) + : status(false), signal(dio),signallevel(PC_13) +{ + signal.fall(this, &YL::triggered); + usb2pc.printf("YL start!\r\n"); +} +//-------- +YL::YL(PinName dio,PinName aio) + : status(false), signal(dio),signallevel(aio) +{ + signal.fall(this, &YL::triggered); + usb2pc.printf("YL start!\r\n"); +} +//-------- +void YL::triggered() //触发中断!! +{ + usb2pc.printf("YL Triggered!\r\n"); + status = true; +} +//-------- +bool YL::operator==(const bool &target) +{ + if(status == target) + { + return true; + } + else + { + return false; + } +} +//-------- +float YL::getairdata() +{ + return signallevel.read(); +} +//----------- +void YL::reset() +{ + status = false; +} +int YL::read() +{ + return signal.read(); +} +//============================================= +BMP180::BMP180(PinName sda,PinName scl) + :i2c(sda,scl) +{ + OSS=OSS_3; + uint8_t c = readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); + if(c == 0x55) { + + usb2pc.printf("BMP-180 is 0x%x\r\n", c); + usb2pc.printf("BMP-180 should be 0x55\r\n"); + usb2pc.printf("BMP-180 online...\r\n"); + + BMP180Calibration(); + usb2pc.printf("BMP-180 calibration complete...\r\n"); + } + else + { + usb2pc.printf("BMP-180 is 0x%x\r\n", c); + usb2pc.printf("BMP-180 should be 0x55\r\n"); + while(1); // idle here forever + } +} +void BMP180::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) +{ + char data_write[2]; + data_write[0] = subAddress; + data_write[1] = data; + i2c.write(address, data_write, 2, 0); +} + +char BMP180::readByte(uint8_t address, uint8_t subAddress) +{ + char data[1]; // `data` will store the register data + char data_write[1]; + data_write[0] = subAddress; + i2c.write(address, data_write, 1, 1); // no stop + i2c.read(address, data, 1, 0); + return data[0]; +} + +void BMP180::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) +{ + char data[14]; + char data_write[1]; + data_write[0] = subAddress; + i2c.write(address, data_write, 1, 1); // no stop + i2c.read(address, data, count, 0); + for(int ii = 0; ii < count; ii++) { + dest[ii] = data[ii]; + } +} + + +// Stores all of the BMP180's calibration values into global variables +// Calibration values are required to calculate temp and pressure +// This function should be called at the beginning of the program +// These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics +void BMP180::BMP180Calibration() +{ + ac1 = readByte(BMP180_ADDRESS, 0xAA) << 8 | readByte(BMP180_ADDRESS, 0xAB); + ac2 = readByte(BMP180_ADDRESS, 0xAC) << 8 | readByte(BMP180_ADDRESS, 0xAD); + ac3 = readByte(BMP180_ADDRESS, 0xAE) << 8 | readByte(BMP180_ADDRESS, 0xAF); + ac4 = readByte(BMP180_ADDRESS, 0xB0) << 8 | readByte(BMP180_ADDRESS, 0xB1); + ac5 = readByte(BMP180_ADDRESS, 0xB2) << 8 | readByte(BMP180_ADDRESS, 0xB3); + ac6 = readByte(BMP180_ADDRESS, 0xB4) << 8 | readByte(BMP180_ADDRESS, 0xB5); + b1 = readByte(BMP180_ADDRESS, 0xB6) << 8 | readByte(BMP180_ADDRESS, 0xB7); + b2 = readByte(BMP180_ADDRESS, 0xB8) << 8 | readByte(BMP180_ADDRESS, 0xB9); + mb = readByte(BMP180_ADDRESS, 0xBA) << 8 | readByte(BMP180_ADDRESS, 0xBB); + mc = readByte(BMP180_ADDRESS, 0xBC) << 8 | readByte(BMP180_ADDRESS, 0xBD); + md = readByte(BMP180_ADDRESS, 0xBE) << 8 | readByte(BMP180_ADDRESS, 0xBF); +} + +// Temperature returned will be in units of 0.1 deg C +long BMP180::BMP180GetTemperature() +{ + long ut = 0; + uint8_t rawData[2] = {0, 0}; + + writeByte(BMP180_ADDRESS, 0xF4, 0x2E); // start temperature measurement + wait_ms(5); + readBytes(BMP180_ADDRESS, 0xF6, 2, &rawData[0]); // read raw temperature measurement + ut = rawData[0]*256+ rawData[1]; + + long x1, x2; + + x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15; + x2 = ((long)mc << 11)/(x1 + md); + b5 = x1 + x2; + + return ((b5 + 8)>>4)/10.0; +} + +// Calculate pressure read calibration values +// b5 is also required so BMP180GetTemperature() must be called first. +// Value returned will be pressure in units of Pa. +long BMP180::BMP180GetPressure() +{ + long up = 0; + writeByte(BMP180_ADDRESS, 0xF4, 0x34 | OSS << 6); // Configure pressure measurement for highest resolution + wait_ms(5+8*OSS); // delay 5 ms at lowest resolution, 29 ms at highest + uint8_t rawData[3] = {0, 0, 0}; + readBytes(BMP180_ADDRESS, 0xF6, 3, &rawData[0]); // read raw pressure measurement of 19 bits + up = (((long) rawData[0] << 16) | ((long)rawData[1] << 8) | rawData[2]) >> (8 - OSS); + + long x1, x2, x3, b3, b6, p; + unsigned long b4, b7; + + b6 = b5 - 4000; + // Calculate B3 + x1 = (b2 * (b6 * b6)>>12)>>11; + x2 = (ac2 * b6)>>11; + x3 = x1 + x2; + b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2; + + // Calculate B4 + x1 = (ac3 * b6)>>13; + x2 = (b1 * ((b6 * b6)>>12))>>16; + x3 = ((x1 + x2) + 2)>>2; + b4 = (ac4 * (unsigned long)(x3 + 32768))>>15; + + b7 = ((unsigned long)(up - b3) * (50000>>OSS)); + if (b7 < 0x80000000) + p = (b7<<1)/b4; + else + p = (b7/b4)<<1; + + x1 = (p>>8) * (p>>8); + x1 = (x1 * 3038)>>16; + x2 = (-7357 * p)>>16; + p += (x1 + x2 + 3791)>>4; + + return p; +} +//=============================== +GP2Y1010::GP2Y1010(PinName led,PinName measure) + :measurePin(measure),ledPower(led) +{ + samplingTime = 280; + deltaTime = 40; + sleepTime = 9680; + + voMeasured = 0; + calcVoltage = 0; + dustDensity = 0; +} + +float GP2Y1010::getairdata() +{ + ledPower=1; + + wait_us(samplingTime); + + voMeasured = measurePin.read(); + + wait_us(deltaTime); + ledPower=0; + wait_us(sleepTime); + + calcVoltage = voMeasured*5.0; + dustDensity = 0.17*calcVoltage-0.1; + + if ( dustDensity < 0) + { + dustDensity = 0.00; + } + return dustDensity ; +}
--- a/sensors.h Fri Sep 14 10:52:15 2018 +0000 +++ b/sensors.h Thu Oct 18 16:23:13 2018 +0000 @@ -3,6 +3,27 @@ #include "mbed.h" + + +//========================================== +class sr501 +{ +/***** + * 红外热释电对象 + * 感应输出高电平 +*****/ + private: +// DigitalIn signal; + bool status; + InterruptIn signal; + void triggered(); + public: + sr501(PinName pSignal); + bool operator==(const bool &target); + void reset(); + int read(); +}; +//========================================= //+++++++++++++++++++++BH1750+++++++++++++++++++++++++++++ #define BH1750_I2CADDR 0x46 #define BH1750_POWER_DOWN 0x00 // No active state @@ -23,27 +44,7 @@ // 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 -{ -/***** - * 红外热释电对象 - * 感应输出高电平 -*****/ - private: -// DigitalIn signal; - bool status; - InterruptIn signal; - void triggered(); - public: - sr501(PinName pSignal); - bool operator==(const bool &target); - void reset(); - int read(); -}; -//========================================= +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class BH1750 { @@ -99,5 +100,120 @@ float gettemperature(); }; -//======================= +//==========================================DS18B20 +class DS18B20 +{ +/***** +* +*****/ + private: + DigitalInOut datapin; + uint8_t dat; + float T; + + int start(); + void writebyte(uint8_t send); + uint8_t readByte(); + float transfer(uint8_t h,uint8_t l); + public: + DS18B20(PinName pin); + int getdata(); + float gettemperature(); + +}; +//==========================================YL-38通用传感 +class YL +{ +/***** +* +*****/ + private: + bool status; + InterruptIn signal; + AnalogIn signallevel; + + void triggered(); + public: + YL(PinName dio); + YL(PinName dio,PinName aio); + bool operator==(const bool &target); + float getairdata(); + void reset(); + int read(); +}; +//======================================== +#define BMP180_ADDRESS 0x77<<1 // I2C address of BMP180, eight bit address on mbed +#define BMP180_WHO_AM_I 0xD0 // WHO_AM_I id of BMP180, should return 0x55 +#define BMP180_RESET 0xE0 +#define BMP180_CONTROL 0xF4 +#define BMP180_OUT_MSB 0xF6 +#define BMP180_OUT_LSB 0xF7 +#define BMP180_OUT_XLSB 0xF8 + +// Set initial input parameters + +enum OSS { // BMP-085 sampling rate + OSS_0 = 0, // 4.5 ms conversion time + OSS_1=1, // 7.5 + OSS_2=2, // 13.5 + OSS_3=3 // 25.5 +}; + + + +class BMP180 { +/***** +* +*****/ +private: + uint8_t OSS; // maximum pressure resolution + + //Set up I2C, (SDA,SCL) + I2C i2c; + + // These are constants used to calculate the temperature and pressure from the BMP-180 sensor + int16_t ac1, ac2, ac3, b1, b2, mb, mc, md, b5; + uint16_t ac4, ac5, ac6; + + void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); + char readByte(uint8_t address, uint8_t subAddress); + void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); + + // Stores all of the BMP180's calibration values into global variables + // Calibration values are required to calculate temp and pressure + // This function should be called at the beginning of the program + // These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics + void BMP180Calibration(); +public: + BMP180(PinName sda,PinName scl) ; + // Temperature returned will be in units of 0.1 deg C + long BMP180GetTemperature(); + + // Calculate pressure read calibration values + // b5 is also required so BMP180GetTemperature() must be called first. + // Value returned will be pressure in units of Pa. + long BMP180GetPressure(); +}; +//================================================ +class GP2Y1010 +{ +/***** +* +*****/ + private: + AnalogIn measurePin ; + DigitalOut ledPower; + unsigned int samplingTime ; + unsigned int deltaTime ; + unsigned int sleepTime ; + + float voMeasured ; + float calcVoltage ; + float dustDensity ; + + public: + GP2Y1010(PinName ledPower,PinName measurePin); + float getairdata(); +}; +//============================================= #endif