原创 用Arduino兼容版做气压温度监控

2013-1-31 15:39 2830 11 11 分类: MCU/ 嵌入式

  朋友找来一个GR-SAKURA小板,是瑞萨做的兼容Arduino的板子,主IC是R5F563NEDDFP:

x20130131135439114.jpg.pagespeed.ic.OmLp6d50zg.jpg
背面:
      x20130131135354403.jpg.pagespeed.ic.h5kiM1_z6E.jpg
刚好之前研究过MPL115A2,于是就连上做气压温度监控:
电路示意:
      x20130131135814740.gif.pagespeed.ic.SnIm-ytwR-.png
然后就写程序,这个板子的ide比较特别,是云的,在http://tool-cloud.renesas.com/index.php。
MPL115A2是I2C的,要用Wire库,用法与Arduino一样,就这3点:
1】需要在在library里选上wire、并#include
2】setup()里启用并配置好2线的定义:
   Wire.begin();
    Wire.assignSdaScl (PIN_SDA, PIN_SCL);
 
3】写操作:
 
    Wire.beginTransmission (SLAVE_ADDRESS);
    Wire.write (COMAND_WRITE);//写命令
    Wire.endTransmission ();
4】读操作:
 
    int i,j;
    i=Wire.requestFrom(SLAVE_ADDRESS,READ_LENGTH);//开始读READ_LENGTH的返回
 
    while(Wire.available()
 
    for(j=0;j
        i=Wire.read();//读1byte
     }
 
然后就好办了,加进数据转换之后觉得老这么干等着也看不出来是否在工作,就加了个闪灯,板子看上去这样:
        e0a94f1b553cd71c03bbc5f30eb67ef6.GIF
用串口工具做上位机,最后
        x20130131141106420.gif.pagespeed.ic._N1bIEUpAN.png
var后面大括号里是原始字节数据,最后3行分别是气压、温度和校验值。
 
以下是代码:
/*GR-SAKURA Sketch Template Version: V1.02*/
#include <rxduino.h>
#include <Wire.h>

#define I2C_SLAVE_ADDR   0x60
#define I2C_PIN_SDA      1
#define I2C_PIN_SCL      2
#define I2C_DATA_LENGTH  12

#define MY_DEBUG_STR(s)  Serial.println(s)
#define MY_DEBUG_HEX(s)  Serial.println(s,HEX)

    
//#define POP_LIMIT 3600
#define POP_LIMIT 10
unsigned long time_count_limit;

//获取传感器数据
void getRAW(byte *var)
{
    int i,j;
    Wire.beginTransmission (I2C_SLAVE_ADDR);
    i=Wire.write (0x12);
    i=Wire.write (0x00);
    Wire.endTransmission ();
    delay(400);
    //MY_DEBUG_STR("VARs{");
    Wire.beginTransmission (I2C_SLAVE_ADDR);
    i=Wire.write (0x00);
    Wire.endTransmission ();
    delay(30);
    i=Wire.requestFrom(I2C_SLAVE_ADDR,I2C_DATA_LENGTH);
    //MY_DEBUG_STR(i);
    //MY_DEBUG_STR("=");
    while(Wire.available()<I2C_DATA_LENGTH){}
    for(j=0;j<I2C_DATA_LENGTH;j++){
        i=Wire.read();
        (*(var++))=i;
        //MY_DEBUG_HEX(i);
    }
    //MY_DEBUG_STR("}");
}
//计算气压值
signed short MPL115A_calculatePressure(unsigned short Padc, unsigned short Tadc,
                signed short a0, signed short b1, signed short b2, signed short c12)
{
        signed long Pressure;
        signed long c12x2, a1, a1x1, y1, a2x2, PComp;
        Padc >>= 6;
        Tadc >>= 6;
        c12x2 = (((signed long)c12) * Tadc) >> 11;
        a1    = (signed long)b1 + c12x2;
        a1x1  = a1 * Padc;
        y1    = (((signed long)a0) << 10) + a1x1;
        a2x2  = (((signed long)b2) * Tadc) >> 1;
        PComp = (y1 + a2x2) >> 9;
        //return (signed short)PComp;
        Pressure = ((((signed long)PComp) * 1041) >> 14) + 800;
        return (unsigned short)Pressure;
}

void setup()
{
    pinMode(PIN_LED0,OUTPUT);//指示灯:数据处理
    pinMode(PIN_LED3,OUTPUT);//指示灯:等待中
    Wire.begin();//使能I2C
    
    Serial.begin(9600); //set baudrate 9600bps
    Wire.assignSdaScl (I2C_PIN_SDA, I2C_PIN_SCL);//I2C的引脚定义
        
    time_count_limit=POP_LIMIT ;/计时器初始化
    MY_DEBUG_STR("Ready!");
}
void loop()
{
    int i,j;
    signed short p,t;
    byte ucVar[12];
    float ftemp,fpres;

    //while(!Serial.available()); //wait command from upper computer
    //char c=Serial.read();
    time_count_limit--;
    if(0==time_count_limit)//时间到
    {
        time_count_limit=POP_LIMIT ;
        digitalWrite(PIN_LED0,1); //turn on the led0
        getRAW(ucVar);
        MY_DEBUG_STR("var{");
        for(j=0;j<I2C_DATA_LENGTH;j++)MY_DEBUG_HEX(ucVar[j]);//输出原始数据
        MY_DEBUG_STR("}");

        //MY_DEBUG_STR("var done");

        p= MPL115A_calculatePressure(
                ((unsigned short)ucVar[0x0]<<8)|((unsigned short)ucVar[0x1]),
                ((unsigned short)ucVar[0x2]<<8)|((unsigned short)ucVar[0x3]),
                ((unsigned short)ucVar[0x4]<<8)|((unsigned short)ucVar[0x5]),
                ((unsigned short)ucVar[0x6]<<8)|((unsigned short)ucVar[0x7]),
                ((unsigned short)ucVar[0x8]<<8)|((unsigned short)ucVar[0x9]),
                ((unsigned short)ucVar[0xa]<<8)|((unsigned short)ucVar[0xb])
        );
        fpres=((float)(p))/(1<<4);
        //MY_DEBUG_STR("PRES=");
        //MY_DEBUG_STR(p);
        MY_DEBUG_STR(fpres);//输出气压值

        t=( ((unsigned short)ucVar[0x2]<<8)|((unsigned short)ucVar[0x3]) )>>6;
        ftemp=((float) t- 498.0F) / -5.35F +25.0F; 
        //MY_DEBUG_STR("TEMP=");
        //MY_DEBUG_STR(t);
        MY_DEBUG_STR(ftemp);//输出温度值
        delay(1000);

        digitalWrite(PIN_LED0,0); //set led0 off

    }
    digitalWrite(PIN_LED3,1); 
    delay(500);
    digitalWrite(PIN_LED3,0); 
    delay(500);
}
;j++){<>
){}>
in

文章评论0条评论)

登录后参与讨论
我要评论
0
11
关闭 站长推荐上一条 /2 下一条