12864液晶屏 基于51单片机运用LCD12864液晶显示
原标题:基于51单片机的LCD12864液晶显示器
注意:较低版本的keil软件无法成功编译,因为它无法处理浮点运算。
#包括
#包括
#定义uint无符号整数
#定义uchar无符号字符
#定义noACK 0
#定义确认1
#定义STATUS_REG_W 0x06
#定义状态寄存器0x07
#定义MEASURE_TEMP 0x03
#定义MEASURE_HUMI 0x05
#定义复位0x1e
enum {TEMP,HUMI };
Typedef union //定义共享同一类型
{
无符号整数I;
float f;
}价值;
斯比特·lcdrs=p2^0;
斯比特·lcdrw=p2^1;
斯比特·lcden=p2^2;
p1^0;
sbit数据= p1^1;
Uchar table2="SHT11温湿度检测";
Uchar table3= "温度为:℃";
Uchar table4= "湿度为:";
uchar table5= " ";
乌查尔·杜文;
乌查尔·杜诗;
无效延迟
{
int x,y;
为
对于;
}
无效延迟_50us
{
uint j;
为
对于;
}
无效延迟_ 50毫秒
{
uint j;
为
对于;
}
void write_12864com
{
LCD RS = 0;
LCD rw = 0;
delay _ 50us
P0 = com
lcden = 1;
delay _ 50us
lcden = 0;
delay _ 50us
}
无效写入_dat
{
LCD RS = 1;
LCD rw = 0;
delay _ 50us
P0 = dat
lcden = 1;
delay _ 50us
lcden = 0;
delay _ 50us
}
void init12864lcd
{
delay _ 50ms
write _ 12864com
delay _ 50us
write _ 12864com
delay _ 50us
write _ 12864com
delay _ 50us
write _ 12864com
delay _ 50us
write _ 12864com
delay _ 50us
write _ 12864com
delay _ 50us
}
无效显示1
{
uchar I;
write _ 12864com
为
{
write _ dat
delay _ 50us
}
}
无效显示2
{
uchar I;
write _ 12864com
为
{
write _ dat
delay _ 50us
}
}
无效显示3
{
uchar I;
write _ 12864com
为
{
write _ dat
delay _ 50us
}
}
void displaywendu
{
uchar I;
write _ 12864com
为
{
write _ dat
delay _ 50us
}
为
{
write _ dat
delay _ 50us
}
为
{
write _ dat
delay _ 50us
}
}
void displayshidu
{
uchar I;
write _ 12864com
为
{
write _ dat
delay _ 50us
}
为
{
write _ dat
delay _ 50us
}
为
{
write _ dat
delay _ 50us
}
}
//字节写入程序
字符s_write_byte
{
无符号字符I,错误= 0;
//的高电平位为1,它向右循环移动
{
if DATa = 1;//和要传输的数字相位,结果是传输的比特
否则数据= 0;
SCK = 1;
_ nop __ nop __ nop _//延迟3us
SCK = 0;
}
数据= 1;//松开数据线
SCK = 1;
错误=数据;//检查响应信号,确认通信正常
_ nop __ nop __ nop _
SCK = 0;
数据= 1;
返回错误;//错误=1个通信错误
}
//字节读取程序
char s_read_byte
{
无符号字符I,val = 0;
数据= 1;//松开数据线
//的高电平位为1,它向右循环移动
{
SCK = 1;
if val =;//读取一条数据线的值
SCK = 0;
}
数据=!ack//如有验证,阅读后交流完毕;
SCK = 1;
_ nop __ nop __ nop _//延迟3us
SCK = 0;
_ nop __ nop __ nop _
数据= 1;//松开数据线
返回值;
}
//开始传输
void s_transstart
{
数据= 1;SCK = 0;//准备好
_ nop _
SCK = 1;
_ nop _
数据= 0;
_ nop _
SCK = 0;
_ nop __ nop __ nop _
SCK = 1;
_ nop _
数据= 1;
_ nop _
SCK = 0;
}
//连接重置
void s_connectionreset
{
无符号字符I;
数据= 1;SCK = 0;//准备好
//数据保持高电平,SCK时钟触发9次,传输开始,通信复位。
{
SCK = 1;
SCK = 0;
}
s _ transstart//开始传输
}
//软复位程序
char s_softreset
{
无符号字符错误= 0;
s _ connectionreset//开始连接重置
错误+= s _ write _ byte;//发送复位命令
返回错误;//错误=1个通信错误
}
//温度和湿度测量
字符s_measure
{
无符号错误= 0;
无符号整数I;
s _ transstart//开始传输
切换//选择发送命令
{
case TEMP:error+= s _ write _ byte;打破;//测量温度
案例HUMI:错误+= s _ write _ byte;打破;//测量湿度
默认值:break
}
forif break//等待测量结束
如果错误+= 1;//如果数据线长时间不拉低,说明测量错误
* = s _ read _ byte//读取第一个字节,高字节
* = s _ read _ byte//读取第二个字节,低字节
* p _校验和= s _ read _ byte//读取CRC校验码
返回错误;//错误=1个通信错误
}
//温湿度值的标度变换和温度补偿
void calc_stp0
{
const float C1 =-4.0;// 12位湿度精度校正公式
const float C2 =+0.0405;// 12位湿度精度校正公式
const float C3 =-0.0000028;// 12位湿度精度校正公式
const float T1 =+0.01;// 14位温度精度5V条件校正公式
const float T2 =+0.00008;// 14位温度精度5V条件校正公式
浮子rh = * p _湿度;//相对湿度:12位湿度
浮子t = * p _温度;// t: 14位温度
浮动rh _ lin// rh_lin:湿度线性值
浮动rh _ true// rh_true:湿度真值
浮动t _ C;// t_C:温度℃
t _ C = t * 0.01-40;//温度补偿
RH _ Lin = C3 * RH * RH+C2 * RH+C1;//相对湿度的非线性补偿
RH _ true = *+RH _ Lin;//相对湿度对温度依赖性的补偿
ifrh _ true = 100//最大湿度校正
ifrh _ true = 0.1//最小湿度校正
* p _ temperature = t _ C;//返回温度结果
* p _湿度= rh _ true//返回湿度结果
}
空隙总管
{
无符号整数温度,米虎;
值米虎值,温度值;//定义两个社区,一个用于湿度,一个用于温度
无符号字符错误;//用于检查是否有错误
无符号字符校验和;//CRC
init12864lcd
显示1;
显示2;
显示3;
s _ connectionreset//开始连接重置
正在…
{
错误= 0;//初始化错误=0,即没有错误。
错误+=s_measure&temp_val.i,&checksum,TEMP);//温度测量
错误+= s _ measure &米虎_val.i,&checksum,HUMI);//湿度测量
如果s _ connectionreset////如果出现错误,系统将被重置
其他
{
米虎_瓦尔. f =米虎_瓦尔. I;//转换为浮点数
temp _ val . f = temp _ val . I;//转换为浮点数
calc _ stp0//校正相对湿度和温度
temp = temp _ val.f * 10
米虎=米虎_瓦尔. * 10
杜文=温度/1000+“0”;//温度百位数
杜文= temp 00/100+“0”;//温度十位数
杜文=温度0/10+“0”;//温度单位
杜文= 0x2E//小数点
杜文=温度+0;//温度小数点后的第一位
displaywendu
杜诗=米虎/1000+“0”;//湿度百位数
杜诗= humi 00/100+“0”;//湿度十位数
杜诗= humi 0/10+“0”;//湿度单位
杜诗= 0x2E//小数点
杜诗=米虎+“0”;//湿度是小数点后的第一位
displayshidu
}
延迟;//等待当前下一次转换的时间足够长
}
}
原文链接: