51单片机实现大气压传感器GY68测量程序
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <math.h> //Keil library
- #define uint unsigned int
- #define uchar unsigned char
- #define BMP085_SlaveAddress 0xee //定义器件在IIC总线中的从地址
- #define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
-
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- sbit duoji = P1^2;
- sbit lcden = P2^1;
- sbit rs = P2^2;
- sbit key_1 = P3^4;
- sbit key_2 = P3^5;
- sbit key_3 = P3^6;
- sbit key_4 = P3^7;
- sbit SCL=P1^0; //IIC时钟引脚定义
- sbit SDA=P1^1; //IIC数据引脚定义
- uchar count3;
- 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;
- uchar code table1[]="Pressure: HPa";
- uchar code table2[]="status: normal ";
- uchar code table3[]="status: abnormal";
- uchar code table4[]="MQ7: T: ";
- uchar code table5[]="Revise success!!";
- uchar code table6[]=" Revise Menu ";
- void LCD_DISPLAY(uchar num); //LCD显示
- void delay(uint z) //毫秒延时
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- //****************************LCD1602的初始化*******************************
- /**************************************************************************/
- void write_com(uchar com)//LCD写字节
- {
- rs=0;
- lcden=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
-
- void write_date(uchar date)//LCD读字节
- {
- rs=1;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void lcd_sfm(uchar add,int date)//显示位置和数字
- {
- uchar qian,bai,shi,ge;
- qian = date/1000;
- bai = date%1000/100;
- shi = date%100/10;
- ge = date%10;
- write_com(0x80+add);
- write_date(0x30+qian);
- write_date(0x30+bai);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void SetCurPosition(uchar X, uchar Y)//设置光标
- {
- Y &= 0x01;
- X &= 0x0F; // 限制X不能大于15,Y不能大于1
- if (Y)
- {
- X |= 0xc0; // 当要显示第二行时地址码:0xc0
- }
- X |= 0x80; // 第一行的地址码:0x80
- write_com(X); // 这里不检测忙信号,发送地址码
- //write_com(0x02); //光标归位
- write_com(0x0f); //关光标,闪烁
-
- }
- void DisplayOneChar(uchar X,uchar Y,uchar DData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- write_com(X);
- write_date(DData);
-
- }
- 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;
- }
- /**********************************************************************************************
- ** BMP180初始化及测试函数
- **********************************************************************************************/
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /**************************************
- 延时5毫秒(STC90C52RC@12M)
- 不同的工作环境,需要调整此函数
- 当改用1T的MCU时,请调整此延时函数
- **************************************/
- void Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- /**************************************
- 起始信号
- **************************************/
- void BMP085_Start()
- {
- SDA = 1; //拉高数据线
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SDA = 0; //产生下降沿
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- }
- /**************************************
- 停止信号
- **************************************/
- void BMP085_Stop()
- {
- SDA = 0; //拉低数据线
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SDA = 1; //产生上升沿
- Delay5us(); //延时
- }
- /**************************************
- 发送应答信号
- 入口参数:ack (0:ACK 1:NAK)
- **************************************/
- void BMP085_SendACK(bit ack)
- {
- SDA = ack; //写应答信号
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- }
- /**************************************
- 接收应答信号
- **************************************/
- bit BMP085_RecvACK()
- {
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- CY = SDA; //读应答信号
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- return CY;
- }
- /**************************************
- 向IIC总线发送一个字节数据
- **************************************/
- void BMP085_SendByte(BYTE dat)
- {
- BYTE i;
- for (i=0; i<8; i++) //8位计数器
- {
- dat <<= 1; //移出数据的最高位
- SDA = CY; //送数据口
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- }
- BMP085_RecvACK();
- }
- /**************************************
- 从IIC总线接收一个字节数据
- **************************************/
- BYTE BMP085_RecvByte()
- {
- BYTE i;
- BYTE dat = 0;
- SDA = 1; //使能内部上拉,准备读取数据,
- for (i=0; i<8; i++) //8位计数器
- {
- dat <<= 1;
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- dat |= SDA; //读数据
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- }
- return dat;
- }
- //*********************************************************
- //读出BMP085内部数据,连续两个
- //*********************************************************
- short Multiple_read(uchar ST_Address)
- {
- uchar msb, lsb;
- short _data;
- BMP085_Start(); //起始信号
- BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
- BMP085_SendByte(ST_Address); //发送存储单元地址
- BMP085_Start(); //起始信号
- BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
- msb = BMP085_RecvByte(); //BUF[0]存储
- BMP085_SendACK(0); //回应ACK
- lsb = BMP085_RecvByte();
- BMP085_SendACK(1); //最后一个数据需要回NOACK
- BMP085_Stop(); //停止信号
- Delay5ms();
- _data = msb << 8;
- _data |= lsb;
- return _data;
- }
- //*************************************************************
- long bmp085ReadPressure(void)
- {
- long pressure = 0;
- BMP085_Start(); //起始信号
- BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
- BMP085_SendByte(0xF4); // write register address
- BMP085_SendByte(0x34); // write register data for pressure
- BMP085_Stop(); //发送停止信号
- delay(10); // max time is 4.5ms
-
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
-
- return pressure;
- //return (long) bmp085ReadShort(0xF6);
- }
- //**************************************************************
- //初始化BMP085,根据需要请参考pdf进行修改**************
- void Init_BMP085()
- {
- 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 bmp085Convert()//bmp180转换
- {
- long up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
- long pressure;
- float temp;
- int pressure_int;
- up = bmp085ReadPressure();
- up = bmp085ReadPressure(); // 读取压强
-
- //*************
-
- 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);
- temp = (float)pressure / 100.0 + 0.5;//转换为百帕 并四舍五入
- pressure_int = (int)temp; //强制转换
- lcd_sfm(0x09,pressure_int);//显示
-
- }
- void init()
- {
- lcden=0;
- write_com(0x38); //打开显示模式设置
- write_com(0x0c); //打开显示,光标等等设置未零
- write_com(0x06); //当读或写一个字符后地址指针加一,且光标加一,当写一个字符后整频显示左移,
- write_com(0x01); //清零指令
- write_com(0x80);
-
- TMOD = 0x01;
- TH0 = (65536-100)/256;//100us进入一次中断
- TL0 = (65536-100)%256;
- TR0 = 1;
- ET0 = 1;
- EA = 0;//开启总中断
-
- }
- /**********************************************************************************************
- ** TIMER0中断服务子函数产生PWM
- **********************************************************************************************/
- uchar count = 0;
- uchar pwm = 15;
- void timer0()interrupt 1 using 0
- {
- TH0 = (65536-100)/256;
- TL0 = (65536-100)%256;
- count++;
- if(count >= 200)
- {
- count = 0;
- }
- if(count <= pwm) duoji = 1;
- else duoji = 0;
-
- }
- /**********************************************************************************************
- ** 主函数
- **********************************************************************************************/
- int max_pressure = 910;//大气压阈值
- void main()
- {
- delay(100);//开机延时
- init();//初始化
- Init_BMP085();//BMP初始化
- //lcd_sfm(0x05,666);
- LCD_DISPLAY(1);
- LCD_DISPLAY(2);
- while(1)
- {
- bmp085Convert();
- delay(1000);
- }
- }
- /********************************************************************************************
- * LCD显示函数
- ********************************************************************************************/
- void LCD_DISPLAY(uchar num) //LCD显示
- {
- if(num == 1)
- {
- //write_com(0x01);
- write_com(0x80);
- for(count3=0;count3<16;count3++)
- {
- write_date(table1[count3]); //
- delay(5);
- }
- }
- if(num == 2)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
test.zip
(62.13 KB, 下载次数: 26)
|