微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > LCD12864液晶显示的SHT11温湿度传感器程序

LCD12864液晶显示的SHT11温湿度传感器程序

时间:11-23 来源:互联网 点击:
注意:低版本的keil软件不能编译成功,因为无法处理浮点运算。

#include

#include

#define uint unsigned int

#define uchar unsigned char

#define noACK 0

#define ACK 1

#define STATUS_REG_W 0x06

#define STATUS_REG_R 0x07

#define MEASURE_TEMP 0x03

#define MEASURE_HUMI 0x05

#define RESET 0x1e

enum {TEMP,HUMI};

typedef union //定义共用同类型

{

unsigned int i;

float f;

} value;

sbit lcdrs=P2^0;

sbit lcdrw=P2^1;

sbit lcden=P2^2;

sbit SCK = P1^0;

sbit DATA = P1^1;

uchar table2[]="SHT11 温湿度检测";

uchar table3[]="温度为: ℃";

uchar table4[]="湿度为:";

uchar table5[]=".";

uchar wendu[6];

uchar shidu[6];

void delay(int z)

{

int x,y;

for(x=z;x>0;x--)

for(y=125;y>0;y--);

}

void delay_50us(uint t)

{

uint j;

for(;t>0;t--)

for(j=19;j>0;j--);

}

void delay_50ms(uint t)

{

uint j;

for(;t>0;t--)

for(j=6245;j>0;j--);

}

void write_12864com(uchar com)

{

lcdrs=0;

lcdrw=0;

delay_50us(1);

P0=com;

lcden=1;

delay_50us(10);

lcden=0;

delay_50us(2);

}

void write_dat(uchar dat)

{

lcdrs=1;

lcdrw=0;

delay_50us(1);

P0=dat;

lcden=1;

delay_50us(10);

lcden=0;

delay_50us(2);

}

void init12864lcd(void)

{

delay_50ms(2);

write_12864com(0x30);

delay_50us(4);

write_12864com(0x30);

delay_50us(4);

write_12864com(0x0f);

delay_50us(4);

write_12864com(0x01);

delay_50us(240);

write_12864com(0x06);

delay_50us(10);

write_12864com(0x0c);

delay_50us(10);

}

void display1(void)

{

uchar i;

write_12864com(0x80);

for(i=0;i<18;i++)

{

write_dat(table2[i]);

delay_50us(1);

}

}

void display2(void)

{

uchar i;

write_12864com(0x90);

for(i=0;i<18;i++)

{

write_dat(table3[i]);

delay_50us(1);

}

}

void display3(void)

{

uchar i;

write_12864com(0x88);

for(i=0;i<8;i++)

{

write_dat(table4[i]);

delay_50us(1);

}

}

void displaywendu(void)

{

uchar i;

write_12864com(0x94);

for(i=0;i<3;i++)

{

write_dat(wendu[i]);

delay_50us(1);

}

for(i=0;i<1;i++)

{

write_dat(table5[i]);

delay_50us(1);

}

for(i=4;i<5;i++)

{

write_dat(wendu[i]);

delay_50us(1);

}

}

void displayshidu(void)

{

uchar i;

write_12864com(0x8C);

for(i=0;i<3;i++)

{

write_dat(shidu[i]);

delay_50us(1);

}

for(i=0;i<1;i++)

{

write_dat(table5[i]);

delay_50us(1);

}

for(i=4;i<5;i++)

{

write_dat(shidu[i]);

delay_50us(1);

}

}

//写字节程序

char s_write_byte(unsigned char value)

{

unsigned char i,error=0;

for (i=0x80;i>0;i>>=1) //高位为1,循环右移

{

if (i&value) DATA=1; //和要发送的数相与,结果为发送的位

else DATA=0;

SCK=1;

_nop_();_nop_();_nop_(); //延时3us

SCK=0;

}

DATA=1; //释放数据线

SCK=1;

error=DATA; //检查应答信号,确认通讯正常

_nop_();_nop_();_nop_();

SCK=0;

DATA=1;

return error; //error=1 通讯错误

}

//读字节程序

char s_read_byte(unsigned char ack)

{

unsigned char i,val=0;

DATA=1; //释放数据线

for(i=0x80;i>0;i>>=1) //高位为1,循环右移

{

SCK=1;

if(DATA) val=(val|i); //读一位数据线的值

SCK=0;

}

DATA=!ack; //如果是校验,读取完后结束通讯;

SCK=1;

_nop_();_nop_();_nop_(); //延时3us

SCK=0;

_nop_();_nop_();_nop_();

DATA=1; //释放数据线

return val;

}

//启动传输

void s_transstart(void)

{

DATA=1; SCK=0; //准备

_nop_();

SCK=1;

_nop_();

DATA=0;

_nop_();

SCK=0;

_nop_();_nop_();_nop_();

SCK=1;

_nop_();

DATA=1;

_nop_();

SCK=0;

}

//连接复位

void s_connectionreset(void)

{

unsigned char i;

DATA=1; SCK=0; //准备

for(i=0;i<9;i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位

{

SCK=1;

SCK=0;

}

s_transstart(); //启动传输

}

//软复位程序

char s_softreset(void)

{

unsigned char error=0;

s_connectionreset(); //启动连接复位

error+=s_write_byte(RESET); //发送复位命令

return error; //error=1 通讯错误

}

//温湿度测量

char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)

{

unsigned error=0;

unsigned int i;

s_transstart(); //启动传输

switch(mode) //选择发送命令

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top