标题:
BMP180设置压力范围控制led 在某一气压下不同的led灯闪烁
[打印本页]
作者:
2552
时间:
2017-5-29 20:14
标题:
BMP180设置压力范围控制led 在某一气压下不同的led灯闪烁
我想设置在某一气压下不同的led灯闪烁,但是不好使,麻烦大神帮忙修改
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0
sbit SCL=P2^3;
sbit SDA=P2^4;
sbit LCM_RS=P1^0;
sbit LCM_RW=P1^1;
sbit LCM_EN=P2^5;
sbit led1=P1^5;
sbit led2=P1^6;
sbit led3=P1^7;
#define BMP180_SlaveAddress 0xee
#define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
typedef unsigned char BYTE;
typedef unsigned short WORD;
uint pressurewarn;
uchar ge,shi,bai,qian,wan,shiwan;
int dis_data;
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
void delay(unsigned int k);
void InitLcd();
void WriteDataLCM(uchar dataW);
void WriteCommandLCM(uchar CMD,uchar Attribc);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void conversion(long temp_data);
//void warn(uint pressure);
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data);
uchar Single_Read(uchar REG_Address);
void Multiple_Read(uchar,uchar);
void Delay5us();
void Delay5ms();
void BMP180_Start();
void BMP180_Stop();
void BMP180_SendACK(bit ack);
bit BMP180_RecvACK();
void BMP180_SendByte(BYTE dat);
BYTE BMP180_RecvByte();
void BMP180_ReadPage();
void BMP180_WritePage();
void conversion(long temp_data)
{
shiwan=temp_data/100000+0x30 ;
temp_data=temp_data%100000;
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000;
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000;
bai=temp_data/100+0x30 ;
temp_data=temp_data%100;
shi=temp_data/10+0x30 ;
temp_data=temp_data%10;
ge=temp_data+0x30;
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{;}}
}
void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}
void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void Delay5ms()
{
WORD n = 560;
while (n--);
}
void BMP180_Start()
{
SDA = 1;
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
SCL = 0;
}
void BMP180_Stop()
{
SDA = 0;
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
void BMP180_SendACK(bit ack)
{
SDA = ack;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}
bit BMP180_RecvACK()
{
SCL = 1;
Delay5us();
CY = SDA;
SCL = 0;
Delay5us();
return CY;
}
void BMP180_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++)
{
dat <<= 1;
SDA = CY;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}
BMP180_RecvACK();
}
BYTE BMP180_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1;
for (i=0; i<8; i++)
{
dat <<= 1;
SCL = 1;
Delay5us();
dat |= SDA;
SCL = 0;
Delay5us();
}
return dat;
}
/*
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
BMP180_Start();
BMP180_SendByte(SlaveAddress);
BMP180_SendByte(REG_Address);
BMP180_SendByte(REG_data);
BMP180_Stop();
*/
/*
uchar Single_Read(uchar REG_Address)
{ uchar REG_data;
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress);
BMP180_SendByte(REG_Address);
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress+1);
REG_data=BMP180_RecvByte();
BMP180_SendACK(1);
BMP180_Stop();
return REG_data;
}
*/
short Multiple_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress);
BMP180_SendByte(ST_Address);
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress+1);
msb = BMP180_RecvByte();
BMP180_SendACK(0);
lsb = BMP180_RecvByte();
BMP180_SendACK(1);
BMP180_Stop();
Delay5ms();
_data = msb << 8;
_data |= lsb;
return _data;
}
long bmp180ReadTemp(void)
{
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress);
BMP180_SendByte(0xF4);
BMP180_SendByte(0x2E);
BMP180_Stop();
delay(10);
return (long) Multiple_read(0xF6);
}
long bmp180ReadPressure(void)
{
long pressure = 0;
BMP180_Start();
BMP180_SendByte(BMP180_SlaveAddress);
BMP180_SendByte(0xF4);
BMP180_SendByte(0x34);
BMP180_Stop();
delay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
pressurewarn=pressure;
return pressurewarn;
}
void Init_BMP180()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
void bmp180Convert()
{
//long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
// long temperature;
long pressure;
// ut = bmp180ReadTemp();
// ut = bmp180ReadTemp();
//up = bmp180ReadPressure();
up = bmp180ReadPressure();
/* x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
conversion(temperature);
DisplayOneChar(0,0,'W');
DisplayOneChar(1,0,'e');
DisplayOneChar(2,0,'n');
DisplayOneChar(3,0,'D');
DisplayOneChar(4,0,'u');
DisplayOneChar(5,0,':');
DisplayOneChar(7,0,bai);
DisplayOneChar(8,0,shi);
DisplayOneChar(9,0,'.');
DisplayOneChar(10,0,ge);
DisplayOneChar(11,0,0XDF);
DisplayOneChar(12,0,'C'); */
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = (((long)ac1 * 4 + x3) + 2)/4;
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 * 2) / b4 ;
else
p = (b7 / b4) * 2;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
pressure = p + ((x1 + x2 + 3791) >> 4);
conversion(pressure);
DisplayOneChar(0,1,'Q');
DisplayOneChar(1,1,'i');
DisplayOneChar(2,1,'Y');
DisplayOneChar(3,1,'a');
DisplayOneChar(5,1,':');
DisplayOneChar(6,1,shiwan);
DisplayOneChar(7,1,wan);
DisplayOneChar(8,1,qian);
DisplayOneChar(9,1,'.');
DisplayOneChar(10,1,bai);
DisplayOneChar(11,1,shi);
DisplayOneChar(12,1,'K');
DisplayOneChar(13,1,'p');
DisplayOneChar(14,1,'a');
}
void Warn(uint pressurewarn)
{
if(pressurewarn==199.97)
{
led2=0;
delay(500);
led2=1;
delay(500);
}
if(pressurewarn>=199.88)
{
led3=1;
delay(500);
led3=0;
delay(500);
}
if(pressurewarn<=99.99)
{
led1=0;
delay(500);
led1=1;
delay(500);
}
}
void main()
{
delay(50);
InitLcd();
Init_BMP180();
while(2)
{
bmp180Convert();
delay(1000);
Warn(pressurewarn);
}
}
复制代码
作者:
2552
时间:
2017-5-29 20:15
无论数值改成多少,一直都是led3在闪
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1