#申请原创#
BMP085是一款数字式温度大气压传感器,并以I2C方式工作,相对于I2C方式工作的OLED屏,它不仅使用输出模式输出,还用到输入模式。 图1 整体构成 BMP085与开发板的连接关系为: SCL---P04 SDA---P05 为便于使用,对这2个引脚做如下的定义: #define SCLK P04 #define SDIN P05 #define SDA_IN P05 数据引脚切换输入、输出模式的函数为: void INPUT_MODE_SET()
{
GPIO_Init(GPIO0, GPIO_PIN_5, GPIO_MODE_IN_PU);
}
void OUTPUT_MODE_SET()
{
GPIO_Init(GPIO0,GPIO_PIN_5, GPIO_MODE_OUT_PP);
}
BMP085的初始化函数为: 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);
}
BMP085进行字节数据发送与接收的函数为: void BMP085_Send_Byte(char txd)
{
char t;
OUTPUT_MODE_SET();
SCLK=0;
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
SDIN=1;
else
SDIN=0;
txd<<=1;
Delay_us(2);
SCLK=1;
Delay_us(2);
SCLK=0;
Delay_us(2);
}
}
char BMP085_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
INPUT_MODE_SET();
for(i=0;i<8;i++)
{
SCLK=0;
Delay_us(2);
SCLK=1;
receive<<=1;
if(SDA_IN) receive++;
Delay_us(2);
}
if(!ack)
BMP085_NAck();
else
BMP085_Ack();
return receive;
}
BMP085读取温度和大气压的函数为: long bmp085ReadTemp(void)
{
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0x2E);
while(BMP085_Wait_Ack());
BMP085_Stop();
Delay(1000);
return (long) Multiple_read(0xF6);
}
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0x34);
while(BMP085_Wait_Ack()){}
BMP085_Stop();
Delay(1000);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
实现温度及大气压检测的主程序为: void main(void)
{
Delay(50);
P0CON = 0xFF;
P0PH = 0x00;
P1CON = 0x00;
P1PH = 0x00;
P2CON = 0xFF;
P2PH = 0x00;
P3CON = 0xFF;
P3PH = 0x00;
P4CON = 0xFF;
P4PH = 0x01;
P5CON = 0xFF;
P5PH = 0x00;
GPIO_Init(GPIO0, GPIO_PIN_4 | GPIO_PIN_5 |GPIO_PIN_6, GPIO_MODE_OUT_PP);
GPIO_Init(GPIO2, GPIO_PIN_2, GPIO_MODE_OUT_PP);
GPIO_Init(GPIO5, GPIO_PIN_3, GPIO_MODE_OUT_PP);
GPIO_Init(GPIO4, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_PP);
Delay(100);
Lcd_Init();
LCD_Clear(RED);
BACK_COLOR=RED;
LCD_Picturea();
LCD_ShowChinese(80,10,0,16,YELLOW);
LCD_ShowChinese(96,10,1,16,YELLOW);
LCD_ShowChar(112,10,':',0,YELLOW);
LCD_ShowChinese(80,30,2,16,YELLOW);
LCD_ShowChinese(96,30,3,16,YELLOW);
LCD_ShowChar(112,30,':',0,YELLOW);
LCD_ShowChinese(80,50,4,16,YELLOW);
LCD_ShowChinese(96,50,5,16,YELLOW);
LCD_ShowChar(112,50,':',0,YELLOW);
Init_BMP085();
Delay(500);
while(1)
{
bmp085Convert();
LCD_ShowNum(120,10,temperature/10,2,YELLOW);
LCD_ShowNum(120,30,pressure/100,4,YELLOW);
Delay(1000);
}
}
经编译和下载,其运行效果如图2所示。 图2检测效果
在图片显示方面,受存储空间的限制,需对图片进行缩小,所用工具如图3所示。 图3 图片数组文件生成
该图片的原尺寸为80*80像素点,为了能将其存入程序,需改变图片的尺寸,其处理后的尺寸为50*50像素点。 对应的显示函数为: void LCD_Picturea(void)
{
int i;
LCD_Address_Set(15,15,64,64);
for(i=0;i<6400;i++)
{
LCD_WR_DATA8(gImage_cgq[i*2]);
LCD_WR_DATA8(gImage_cgq[i*2+1]);
}
}
在使用工具软件构建好汉字库后,其显示函数为: void LCD_ShowChinese(u16 x,u16 y,u8 index,u8 size,u16 color)
{
u8 i,j;
u8 *temp,size1;
if(size==16)temp=Hzk16;
LCD_Address_Set(x,y,x+size-1,y+size-1);
size1=size*size/8;
temp+=index*size1;
for(j=0;j<size1;j++)
{
for(i=0;i<8;i++)
{
if((*temp&(1<<i))!=0)
{
LCD_WR_DATA(color);
}
else
{
LCD_WR_DATA(BACK_COLOR);
}
}
temp++;
}
}
图4 中文字模提取
|