電路連接
LCD1602:
數據端口>>>P2
RS>>>P1.2
RW>>>P1.1
EN>>>P1.3
BMP180:
SCL>>>P3.6
SDA>>>P3.7
BMP180的電源是1.8-3.6V,電壓過高有可能燒壞傳感器,在接5V單片機是需要注意降壓使用。
由于單片機IO口電平5V,如果使用IIC推薦的4.7K上拉電阻,傳感器會由于電壓過高出現數據錯誤,讀取到的氣壓值出現亂碼,解決方法是去掉IIC上的上拉電阻。
20201213115332473.png (1.14 MB, 下載次數: 27)
下載附件
2023-4-26 17:31 上傳
顯示效果如下:
程序:
#include <REG52.H>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <INTRINS.H>
#include "1602.h"
#define uchar unsigned char
#define uint unsigned int
#define DataPort P2
sbit SCL=P3^6;
sbit SDA=P3^7;
sbit LCM_RS=P1^2;
sbit LCM_RW=P1^1;
sbit LCM_EN=P1^3;
#define BMP085_SlaveAddress 0xee
#define OSS 0
typedef unsigned char BYTE;
typedef unsigned short WORD;
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 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 BMP085_Start();
void BMP085_Stop();
void BMP085_SendACK(bit ack);
bit BMP085_RecvACK();
void BMP085_SendByte(BYTE dat);
BYTE BMP085_RecvByte();
void BMP085_ReadPage();
void BMP085_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(0x0e,1);
WriteCommandLCM(0x3c,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 BMP085_Start()
{
SDA = 1;
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
SCL = 0;
}
void BMP085_Stop()
{
SDA = 0;
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
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;
}
void BMP085_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++)
{
dat <<= 1;
SDA = CY;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}
BMP085_RecvACK();
}
BYTE BMP085_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;
}
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();
BMP085_SendACK(0);
lsb = BMP085_RecvByte();
BMP085_SendACK(1);
BMP085_Stop();
Delay5ms();
_data = msb << 8;
_data |= lsb;
return _data;
}
long bmp085ReadTemp(void)
{
BMP085_Start();
BMP085_SendByte(BMP085_SlaveAddress);
BMP085_SendByte(0xF4);
BMP085_SendByte(0x2E);
BMP085_Stop();
delay(10);
return (long) Multiple_read(0xF6);
}
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start();
BMP085_SendByte(BMP085_SlaveAddress);
BMP085_SendByte(0xF4);
BMP085_SendByte(0x34);
BMP085_Stop();
delay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
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()
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
long temperature;
long pressure;
ut = bmp085ReadTemp();
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
up = bmp085ReadPressure();
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
//*************
conversion(temperature);
play(4,0,'T');
play(5,0,':');
play(7,0,bai);
play(8,0,shi);
play(9,0,'.');
play(10,0,ge);
play(11,0,0XDF);
play(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);
play(4,1,'P');
play(5,1,':');
play(6,1,shiwan);
play(7,1,wan);
play(8,1,qian);
play(9,1,'.');
play(10,1,bai);
play(11,1,shi);
play(12,1,ge);
play(13,1,'K');
play(14,1,'p');
play(15,1,'a');
}
void main()
{
delay(50);
init_lcd();
// InitLcd();
Init_BMP085();
while(1)
{
bmp085Convert();
delay(1000);
}
}
1602驅動程序:
#include "1602.h"
/*
LCD1602使用延時函數
*/
void delay_lcd(uint x)
{
uint i;
for(i=0;i<x;i++);
}
/*
給顯示屏發送控制碼
*/
void sentcode(uchar cod)
{
show=cod;
lcdrs=0;
lcde=1;
delay_lcd(10);
lcde=0;
delay_lcd(10);
}
/*
給顯示屏發送顯示數據
*/
void sentdata(uchar dat)
{
show=dat;
lcdrs=1;
lcde=1;
delay_lcd(10);
lcde=0;
delay_lcd(10);
}
/*
顯示屏初始化,只定不讀
*/
void init_lcd(void)
{
lcdrw=0;
sentcode(0x0e);
sentcode(0x3c);
sentcode(0x01);
delay_lcd(100);
}
/*
在a行b列顯示一個字符
*/
void play(uchar a,uchar b,uchar dat)
{
uchar dress;
if(b)
dress=0xc0+a;
else
dress=0x80+a;
sentcode(dress);
sentdata(dat);
}
/*
在x行y列開始顯示字符串
字符串指針*p
speed:顯示每一個字符后停留的時間
delay:顯示完一屏后停留的時間
clear:顯示完一屏后是否進行清屏操作,0為不清屏,其余為清屏
*/
void ShowStr(uchar x , uchar y , char *p , uint speed , uint delay , uchar clear)
{
uchar dress;
while(*p!='\0')
{
if(y)
{
dress = 0xc0+x;
sentcode(dress);
sentdata(*p);
p++;
x++;
if((dress-0xc0)>14)
{
dress = 0x80+x;
y=0;
x=0;
delay_lcd(delay);
if(clear)
{
sentcode(LCD_CLEAR_SCREEN);
}
}
}
else
{
dress = 0x80+x;
sentcode(dress);
sentdata(*p);
p++;
x++;
if((dress-0x80)>14)
{
dress = 0x80+x;
y=1;
x=0;
}
}
delay_lcd(speed);
}
}
/*
清屏操作
*/
void lcd_clear(void)
{
sentcode(LCD_CLEAR_SCREEN);
// sentcode(LCD_HOMING);
}
1602頭文件:
#ifndef __1602_h__
#define __1602_h__
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define show P2//顯示數據總線
sbit lcdrs=P1^2;//控制/數據指示
sbit lcdrw=P1^1;//讀/寫指示
sbit lcde=P1^3;//使能
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光標返回原點
void delay_lcd(uint x);
void sentcode(uchar cod);
void sentdata(uchar dat);
void init_lcd(void);
void play(uchar a,uchar b,uchar dat);
void ShowStr(uchar x , uchar y , char *p , uint speed , uint delay , uchar clear);
void lcd_clear(void);
#endif
|