基于51单片机运用LCD12864液晶显示
注意:低版本的keil软件不能编译成功,因为无法处理浮点运算。
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define noACK 0
#define ACK
#define STATUS_REG_W 0x06
#define STATUS_REG_R 0x07
#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05
#define RESET
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)
{
if (i&value) DATA=1;
}
DATA=1;
SCK=1;
error=DATA;
_nop_();_nop_();_nop_();
SCK=0;
DATA=1;
return error;
}
//读字节程序
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1;
for(i=0x80;i>0;i>>=1)
{
SCK=1;
}
DATA=!ack;
SCK=1;
_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();
DATA=1;
return val;
}
//启动传输
void s_transstart(void)
{
}
//连接复位
void s_connectionreset(void)
{
unsigned char i;
DATA=1; SCK=0;
for(i=0;i<9;i++)
{
SCK=1;
}
s_transstart();
}
//软复位程序
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset();
error+=s_write_byte(RESET);
return error;
}
//温湿度测量
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
s_transstart();
switch(mode)
51单片机LCD12864液晶显 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)