Takeuchi Kouichi
/
20160510_V22_1s_LedMatrix
LEDマトリックス2枚 BME280付き
Revision 0:cfba9bf82ab5, committed 2016-06-03
- Comitter:
- takeuchi
- Date:
- Fri Jun 03 01:43:36 2016 +0000
- Commit message:
- LED??????2??BME?????
Changed in this revision
diff -r 000000000000 -r cfba9bf82ab5 BME280.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BME280.cpp Fri Jun 03 01:43:36 2016 +0000 @@ -0,0 +1,192 @@ +/** + * BME280 Combined humidity and pressure sensor library + * + * @author Toyomasa Watarai + * @version 1.0 + * @date 06-April-2015 + * + * Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science + * https://www.switch-science.com/catalog/2236/ + * + * For more information about the BME280: + * http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf + */ + +#include "mbed.h" +#include "BME280.h" + +BME280::BME280(PinName sda, PinName scl, char slave_adr) + : + i2c_p(new I2C(sda, scl)), + i2c(*i2c_p), + address(slave_adr), + t_fine(0) +{ + initialize(); +} + +BME280::BME280(I2C &i2c_obj, char slave_adr) + : + i2c_p(NULL), + i2c(i2c_obj), + address(slave_adr), + t_fine(0) +{ + initialize(); +} + +BME280::~BME280() +{ + if (NULL != i2c_p) + delete i2c_p; +} + +void BME280::initialize() +{ + char cmd[18]; + + cmd[0] = 0xf2; // ctrl_hum + cmd[1] = 0x01; // Humidity oversampling x1 + i2c.write(address, cmd, 2); + + cmd[0] = 0xf4; // ctrl_meas + cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode + i2c.write(address, cmd, 2); + + cmd[0] = 0xf5; // config + cmd[1] = 0xa0; // Standby 1000ms, Filter off + i2c.write(address, cmd, 2); + + cmd[0] = 0x88; // read dig_T regs + i2c.write(address, cmd, 1); + i2c.read(address, cmd, 6); + + dig_T1 = (cmd[1] << 8) | cmd[0]; + dig_T2 = (cmd[3] << 8) | cmd[2]; + dig_T3 = (cmd[5] << 8) | cmd[4]; + + DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3); + + cmd[0] = 0x8E; // read dig_P regs + i2c.write(address, cmd, 1); + i2c.read(address, cmd, 18); + + dig_P1 = (cmd[ 1] << 8) | cmd[ 0]; + dig_P2 = (cmd[ 3] << 8) | cmd[ 2]; + dig_P3 = (cmd[ 5] << 8) | cmd[ 4]; + dig_P4 = (cmd[ 7] << 8) | cmd[ 6]; + dig_P5 = (cmd[ 9] << 8) | cmd[ 8]; + dig_P6 = (cmd[11] << 8) | cmd[10]; + dig_P7 = (cmd[13] << 8) | cmd[12]; + dig_P8 = (cmd[15] << 8) | cmd[14]; + dig_P9 = (cmd[17] << 8) | cmd[16]; + + DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9); + + cmd[0] = 0xA1; // read dig_H regs + i2c.write(address, cmd, 1); + i2c.read(address, cmd, 1); + cmd[1] = 0xE1; // read dig_H regs + i2c.write(address, &cmd[1], 1); + i2c.read(address, &cmd[1], 7); + + dig_H1 = cmd[0]; + dig_H2 = (cmd[2] << 8) | cmd[1]; + dig_H3 = cmd[3]; + dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f); + dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f); + dig_H6 = cmd[7]; + + DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6); +} + +float BME280::getTemperature() +{ + uint32_t temp_raw; + float tempf; + char cmd[4]; + + cmd[0] = 0xfa; // temp_msb + i2c.write(address, cmd, 1); + i2c.read(address, &cmd[1], 3); + + temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); + + int32_t temp; + + temp = + (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) + + ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14); + + t_fine = temp; + temp = (temp * 5 + 128) >> 8; + tempf = (float)temp; + + return (tempf/100.0f); +} + +float BME280::getPressure() +{ + uint32_t press_raw; + float pressf; + char cmd[4]; + + cmd[0] = 0xf7; // press_msb + i2c.write(address, cmd, 1); + i2c.read(address, &cmd[1], 3); + + press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); + + int32_t var1, var2; + uint32_t press; + + var1 = (t_fine >> 1) - 64000; + var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6; + var2 = var2 + ((var1 * dig_P5) << 1); + var2 = (var2 >> 2) + (dig_P4 << 16); + var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18; + var1 = ((32768 + var1) * dig_P1) >> 15; + if (var1 == 0) { + return 0; + } + press = (((1048576 - press_raw) - (var2 >> 12))) * 3125; + if(press < 0x80000000) { + press = (press << 1) / var1; + } else { + press = (press / var1) * 2; + } + var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12; + var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13; + press = (press + ((var1 + var2 + dig_P7) >> 4)); + + pressf = (float)press; + return (pressf/100.0f); +} + +float BME280::getHumidity() +{ + uint32_t hum_raw; + float humf; + char cmd[4]; + + cmd[0] = 0xfd; // hum_msb + i2c.write(address, cmd, 1); + i2c.read(address, &cmd[1], 2); + + hum_raw = (cmd[1] << 8) | cmd[2]; + + int32_t v_x1; + + v_x1 = t_fine - 76800; + v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) + + ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) * + (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) * + (int32_t)dig_H2 + 8192) >> 14)); + v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4)); + v_x1 = (v_x1 < 0 ? 0 : v_x1); + v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); + + humf = (float)(v_x1 >> 12); + + return (humf/1024.0f); +}
diff -r 000000000000 -r cfba9bf82ab5 BME280.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BME280.h Fri Jun 03 01:43:36 2016 +0000 @@ -0,0 +1,101 @@ +/** + * BME280 Combined humidity and pressure sensor library + * + * @author Toyomasa Watarai + * @version 1.0 + * @date 06-April-2015 + * + * Library for "BME280 temperature, humidity and pressure sensor module" from Switch Science + * https://www.switch-science.com/catalog/2236/ + * + * For more information about the BME280: + * http://ae-bst.resource.bosch.com/media/products/dokumente/bme280/BST-BME280_DS001-10.pdf + */ + +#ifndef MBED_BME280_H +#define MBED_BME280_H + +#include "mbed.h" + +//#define _DEBUG +#define DEFAULT_SLAVE_ADDRESS (0x76 << 1) + +#ifdef _DEBUG +extern Serial pc; +#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__) +#else +#define DEBUG_PRINT(...) +#endif + + +/** BME280 class + * + * BME280: A library to correct environmental data using Boshe BME280 device + * + * BME280 is an environmental sensor + * @endcode + */ + +class BME280 +{ +public: + + /** Create a BME280 instance + * which is connected to specified I2C pins with specified address + * + * @param sda I2C-bus SDA pin + * @param scl I2C-bus SCL pin + * @param slave_adr (option) I2C-bus address (default: 0x76) + */ + BME280(PinName sda, PinName sck, char slave_adr = DEFAULT_SLAVE_ADDRESS); + + /** Create a BME280 instance + * which is connected to specified I2C pins with specified address + * + * @param i2c_obj I2C object (instance) + * @param slave_adr (option) I2C-bus address (default: 0x76) + */ + BME280(I2C &i2c_obj, char slave_adr = DEFAULT_SLAVE_ADDRESS); + + /** Destructor of BME280 + */ + virtual ~BME280(); + + /** Initializa BME280 sensor + * + * Configure sensor setting and read parameters for calibration + * + */ + void initialize(void); + + /** Read the current temperature value (degree Celsius) from BME280 sensor + * + */ + float getTemperature(void); + + /** Read the current pressure value (hectopascal)from BME280 sensor + * + */ + float getPressure(void); + + /** Read the current humidity value (humidity %) from BME280 sensor + * + */ + float getHumidity(void); + +private: + + I2C *i2c_p; + I2C &i2c; + char address; + uint16_t dig_T1; + int16_t dig_T2, dig_T3; + uint16_t dig_P1; + int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; + uint16_t dig_H1, dig_H3; + int16_t dig_H2, dig_H4, dig_H5, dig_H6; + int32_t t_fine; + +}; + +#endif // MBED_BME280_H
diff -r 000000000000 -r cfba9bf82ab5 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 03 01:43:36 2016 +0000 @@ -0,0 +1,333 @@ +// LedMatrix 2mai Ondo,Situdo,Kiatu + +#include "mbed.h" +#include "BME280.h" + +DigitalOut led(dp28); + +BME280 sensor1(dp5,dp27,0x76 << 1); + +// spi(mosi,miso,sck) +SPI max72_spi(dp2, NC, dp6); +DigitalOut load(dp14);//spi load + +// CPU MT7219 +// dp1 mosi(Master In Salve Out) => DIN +// dp2 miso(Master OutSlave) => nc +// dp6 sck (Serial Clock) => clk +// dp14 =>(Slave Select) load + +int maxInUse = 2; //change this variable to set how many MAX7219's you'll use + +// define max7219 registers +#define max7219_reg_noop 0x00 +#define max7219_reg_digit0 0x01 +#define max7219_reg_digit1 0x02 +#define max7219_reg_digit2 0x03 +#define max7219_reg_digit3 0x04 +#define max7219_reg_digit4 0x05 +#define max7219_reg_digit5 0x06 +#define max7219_reg_digit6 0x07 +#define max7219_reg_digit7 0x08 +#define max7219_reg_decodeMode 0x09 +#define max7219_reg_intensity 0x0a +#define max7219_reg_scanLimit 0x0b +#define max7219_reg_shutdown 0x0c +#define max7219_reg_displayTest 0x0f + +#define LOW 0 +#define HIGH 1 +#define MHZ 1000000 +#define ON 1 +#define OFF 0 + +double p1,t1,h1,pp1; +double hight,hosei; + +void maxSingle( int reg, int col) { +//maxSingle is the "easy" function to use for a +//single max7219 + load = LOW; // begin + max72_spi.write(reg); // specify register + max72_spi.write(col); // put data + load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS) +} + +void maxAll (int reg, int col) { // initialize all MAX7219's in the system + load = LOW; // begin + for ( int c=1; c<= maxInUse; c++) { + max72_spi.write(reg); // specify register + max72_spi.write(col); // put data + } + load = HIGH; +} + +void maxOne(int maxNr, int reg, int col) { +//maxOne is for adressing different MAX7219's, +//while having a couple of them cascaded + int c = 0; + load = LOW; + + for ( c = maxInUse; c >= maxNr; c--) { + max72_spi.write(0); // no-op + max72_spi.write(0); // no-op + } + + max72_spi.write(reg); // specify register + max72_spi.write(col); // put data + + for ( c=maxNr-1; c >= 1; c--) { + max72_spi.write(0); // no-op + max72_spi.write(0); // no-op + } + load = HIGH; +} + +void setup () { + // initiation of the max 7219 + // SPI setup: 8 bits, mode 0 + max72_spi.format(8, 0); + + // going by the datasheet, min clk is 100ns so theoretically 10MHz should work... + // max72_spi.frequency(10*MHZ); + + maxAll(max7219_reg_scanLimit, 0x07); + maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) + maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode + maxAll(max7219_reg_displayTest, 0x00); // no display test + for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off + maxAll(e,0); + } + maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set + // range: 0x00 to 0x0f +} + +void get_sensor1(void){ + t1=sensor1.getTemperature(); + if(t1 > 85.0){ + t1=t1-409.0-1; + } + h1=sensor1.getHumidity(); + pp1=sensor1.getPressure(); + pp1=pp1+int(700/9); +} + +int main(void) { + + const uint64_t moji_font[] = {// bold font , kagami moji font + 0x3c66666e76663c00,//0~9 + 0x7e1818181c181800, + 0x7e060c3060663c00, + 0x3c66603860663c00, + 0x30307e3234383000, + 0x3c6660603e067e00, + 0x3c66663e06663c00, + 0x1818183030667e00, + 0x3c66663c66663c00, + 0x3c66607c66663c00, + 0x6666663e06060600,// 10 h + 0x06063e66663e0000,// 11 p + 0x1818000000000000,// 12 . + 0x30480848340a0a04, // 13 dc + 0x1010157555770101,// 14 hp + 0x18187e7e18180000,// 15 + + 0x00007e7e00000000,// 16 - + 0x082a1c771c2a0800, //17 asuta1 + 0x007e424a52427e00, // 18 sikaku2 + 0x0000000000000000, // 19 blank + 0x60660c1830660600, // 20 % + 0x4242665a5a7e1800, // 21 uchi + 0x220424692e2a2c00 // 22 soto + }; + +/* + + const uint64_t moji_font[]={ + 0x006e333e301e0000,//a + 0x003b66663e060607,//b + 0x001e3303331e0000,//c + 0x006e33333e303038,//d + 0x001e033f331e0000,//e + 0x000f06060f06361c,//f + 0x1f303e33336e0000,//g + 0x006766666e360607,//h + 0x001e0c0c0c0e000c,//i + 0x1e33333030300030,//j + 0x0067361e36660607,//k + 0x001e0c0c0c0c0c0e,//l + 0x00636b7f7f330000,//m + 0x00333333331f0000,//n + 0x001e3333331e0000,//o + 0x0f063e66663b0000,//p + 0x78303e33336e0000,//q + 0x000f06666e3b0000,//r + 0x001f301e033e0000,//s + 0x00182c0c0c3e0c08,//t + 0x006e333333330000,//u + 0x000c1e3333330000,//v + 0x00367f7f6b630000,//w + 0x0063361c36630000,//x + 0x1f303e3333330000,//y + 0x003f260c193f0000 //z + }; + +*/ + +// // Jissainiha konoyouni mieru +// maxSingle(1,1); |-| -------+ +// maxSingle(2,2); |-| ------+- +// maxSingle(3,4); |-| -----+-- +// maxSingle(4,8); |-| ----+--- +// maxSingle(5,16); |-| ---+---- +// maxSingle(6,32); |-| --+----- +// maxSingle(7,64); |-| -+------ +// maxSingle(8,128); |-| +------- + + int i,j,k; + uint8_t bitsum[22][9],bit1gyou,bitline[250]; + uint64_t moji1,load_image[22]; + long hpascal,ttemp,sitemp; + int hpketa[4],tpketa[3],siketa[3]; + int gdot[]={128,64,32,16,8,4,2,1}; + int gbit[16],gbit_s[32]; + long wtime=0; + long hpdata[16],hprange,hpmax,hpmin; + long hpcenter; + int dotdata,fstloop,fshuuki; + double hpscope; + double gctrim=0.0; + + setup(); + + wait(0.1); + get_sensor1(); + + hpascal=long(pp1); + hpmin=hpascal; + hpmax=hpascal; + hpscope=1; + hprange=0; + for(i=0;i<16;i++){ + hpdata[i]=hpascal-3; + } + + while(1){ + + get_sensor1(); + + hpascal=long(pp1); + ttemp=long(t1*10); // seisuuchi henkan + sitemp=long(h1); + + hpketa[0]=int(hpascal/1000);hpascal=hpascal-hpketa[0]*1000; + hpketa[1]=int(hpascal/100); hpascal=hpascal-hpketa[1]*100; + hpketa[2]=int(hpascal/10); hpascal=hpascal-hpketa[2]*10; + hpketa[3]=hpascal; + + tpketa[0]=int(ttemp/100);ttemp=ttemp-tpketa[0]*100;// jyuu noketa + tpketa[1]=int(ttemp/10);ttemp=ttemp-tpketa[1]*10;// ichi no keta + tpketa[2]=int(ttemp); // shousuutenika no keta + + siketa[0]=int(sitemp/100);sitemp=sitemp-siketa[0]*100; + siketa[1]=int(sitemp/10);sitemp=sitemp-siketa[1]*10; + siketa[2]=int(sitemp); + + load_image[0]=moji_font[19]; //blank + load_image[1]=moji_font[19]; //blank + load_image[2]=moji_font[17]; //asuta + for(i=3;i<=6;i++){// kiatu 4keta suuji [3 ~ 6] + for(j=0;j<=9;j++){ + if(hpketa[i-3] == j){ + load_image[i]=moji_font[j]; + } + } + } + load_image[7]=moji_font[14];//hp + load_image[8]=moji_font[19]; //blank + + if(t1 >=0){ + load_image[9]=moji_font[15];//+ + } + else { + load_image[9]=moji_font[16];// - + } + + for(i=0;i<=1;i++){// kion seisuuketa + for(j=0;j<=9;j++){ + if(tpketa[i] == j){ + load_image[i+10]=moji_font[j]; + } + } + } + load_image[12]=moji_font[12];// . + for(j=0;j<=9;j++){// kion shousuu keta + if(tpketa[2] == j){ + load_image[13]=moji_font[j]; + } + } + load_image[14]=moji_font[13]; //dc + + load_image[15]=moji_font[19];//blank + + for(i=0;i<=2;i++){ // situdo 3 keta + for(j=0;j<=9;j++){ + if(siketa[i] == j){ + load_image[i+16]=moji_font[j]; + } + } + } + + load_image[19]=moji_font[20]; // % + load_image[20]=moji_font[19];//blank + load_image[21]=moji_font[19];//blank + + for(i=0;i<22;i++){ // 0~21 22 moji + moji1=load_image[i]; + for(j=0;j<8;j++){// bitsum[1] + bitsum[i][j]=0; + }//j + for(j=0;j<8;j++){ + bit1gyou=moji1 & 0xff; + for(k=0;k<8;k++){// kagami moji no font + //for(k=8;k>=1;k--){ // futuu no font + bitsum[i][k]=bitsum[i][k]+(bit1gyou & 0x01)*pow((double)2,(double)(j)); + bit1gyou=bit1gyou >> 1; + }//k + moji1=moji1 >> 8; + }//j + }// i + + k=0; + for(i=0;i<22;i++){ + for(j=0;j<8;j++){ + bitline[k]=bitsum[i][j]; + k++; + } + } + + for(i=0;i<=(22*8-1-16);i++){// bitline hyouji + maxOne(1,1,bitline[i]); + maxOne(1,2,bitline[i+1]); + maxOne(1,3,bitline[i+2]); + maxOne(1,4,bitline[i+3]); + maxOne(1,5,bitline[i+4]); + maxOne(1,6,bitline[i+5]); + maxOne(1,7,bitline[i+6]); + maxOne(1,8,bitline[i+7]); + maxOne(2,1,bitline[i+8]); + maxOne(2,2,bitline[i+9]); + maxOne(2,3,bitline[i+10]); + maxOne(2,4,bitline[i+11]); + maxOne(2,5,bitline[i+12]); + maxOne(2,6,bitline[i+13]); + maxOne(2,7,bitline[i+14]); + maxOne(2,8,bitline[i+15]); + + wait(0.1); + } + wait(0.1); + + }//while +}//main + +
diff -r 000000000000 -r cfba9bf82ab5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 03 01:43:36 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file