气压传感器MS5611
根据C51改编了为arduino 程序,编译通过了,还没有空试验是否正常。先发个帖子,以后再补全。很奇怪,直接把代码复制过来再复制到arduino 022里就编译不了,我把能编译的程序放到RAR里了,哪位童鞋有空帮测试一下是否成功。
#include <SPI.h>
unsigned int w1,w2,w3,w4,d1,d2;
float pressure,temperature;
unsigned int fc;
const int dir=6;
const int clk=5;
const int mosi=4;
const int miso=3;
const int sck=2;
void setup()
{
Serial.begin(9600);
SPI.begin();
pinMode(dir,OUTPUT);
pinMode(clk,OUTPUT);
}
unsigned int ms_read_w(unsigned char sel)
{
unsigned int w;
switch(sel)
{
case 1:
ms_w(0x1d);
ms_w(0x50);
w=ms_r();
break;
case 2:
ms_w(0x1d);
ms_w(0x60);
w=ms_r();
break;
case 3:
ms_w(0x1d);
ms_w(0x90);
w=ms_r();
break;
case 4:
ms_w(0x1d);
ms_w(0xa0);
w=ms_r();
break;
default:
break;
}
return(w);
}
unsigned int ms_read_d(unsigned char sel)
{
unsigned int d;
switch(sel)
{
case 1:
ms_w(0x0f);
ms_w(0x40);
delay(35);
d=ms_r();
break;
case 2:
ms_w(0x0f);
ms_w(0x20);
delay(35);
d=ms_r();
break;
default:
break;
}
return(d);
}
unsigned int change_w_to_C(unsigned char ix,unsigned int W1,unsigned int
W2,unsigned int W3,unsigned int W4)
{
unsigned int c=0;
unsigned int x,y;
switch(ix)
{
case 1:
c=(W1>>1)&0x7FFF;
break;
case 2:
x=(W3<<6)&0x0FC0;
y=W4&0x003F;
c=x|y;
break;
case 3:
c=(W4>>6)&0x03FF;
break;
case 4:
c=(w3>>6)&0x03FF;
break;
case 5:
x=(W1<<10)&0x0400;
y=(W2>>6)&0x03FF;
c=x|y;
break;
case 6:
c=W2&0x003F;
break;
}
return(c);
}
unsigned int trans_num(unsigned int d1_arg,unsigned int d2_arg)
{
float dt,off,sens;
float fd1,fd2,x;
float T2,P2;
d1_arg=d1_arg&0xFFFF;
d2_arg=d2_arg&0xFFFF;
fd1=(float)d1_arg;
fd2=(float)d2_arg;
dt=fd2-((8.0*fc)+20224.0);
off=fc*4.0+(((fc-512.0)*dt)/4096.0);
sens=24576.0+fc+((fc*dt)/1024.0);
x=((sens*(fd1-7168.0))/16384.0)-off;
temperature=200.0+((dt*(fc+50.0))/1024.0);
pressure=2500.0+x*10/32;
if (temperature<200)
{
T2=11.0*(fc+24)*(200.0-temperature)*(200.0-temperature)/1048576.0;
P2=3*T2*(pressure-3500)/16384.0;
}
else if(temperature>450)
{
T2=3.0*(fc+24)*(450.0-temperature)*(450.0-temperature)/1048576.0;
P2=T2*(pressure-10000)/8192.0;
}
else{
T2=0;
P2=0;
}
{
temperature=temperature-T2-40;
}
return(temperature,pressure);
}
void ms_rst(void)
{
ms_w(0x15);
ms_w(0x55);
ms_w(0x40);
}
unsigned int ms_w(unsigned char dat)
{
digitalWrite(dir,1);
SPI.transfer(dat);
digitalWrite(clk,0);
digitalWrite(clk,1);
digitalWrite(dir,0);
return(dat);
}
unsigned int ms_r()
{
byte inByte=0;
unsigned int result=0;
digitalWrite(dir,1);
digitalWrite(clk,1);
digitalWrite(clk,0);
digitalWrite(dir,0);
result=SPI.transfer(0x00);
result=result<<8;
inByte=SPI.transfer(0x00);
result=result|inByte;
return (result);
}
void loop()
{
unsigned int jieguo;
unsigned char i;
ms_rst();
w1=ms_read_w(1);
w2=ms_read_w(2);
w3=ms_read_w(3);
w4=ms_read_w(4);
d1=ms_read_d(1);
d2=ms_read_d(2);
for (i=1;i<=6;i++)
fc=change_w_to_C(i,w1,w2,w3,w4);
jieguo=trans_num(d1,d2);
Serial.print(jieguo);
Serial.println();
} 都是同学,差距怎么那么大啊?我怎么像在看天书啊!我的大学啊{:soso_e109:}泪奔啊 本帖最后由 悠然小调 于 2011-12-8 18:52 编辑
美芳 真的是你吗?咦, 那么巧啊 经过测试。。。程序正常编译~~我贴出来了
@@论坛的代码高亮貌似对非IE支持不是很好。。。非IE浏览器要进入普通模式复制 悠然小调 发表于 2011-12-8 18:15 static/image/common/back.gif
美芳 真的是你吗?咦, 那么巧啊
:lol有内情。。。 有没MS5611的I2C程序 这个代码复制到arduino,没有什么反应,借口为A4A5 ,求其他可用代码
页:
[1]