}
////////////////////////////////////////////////////////////////////////////////
//液晶初始化函数,通过去掉0x0c,可以实时显示光标位置!
///////////////////////////////////////////////////////////////////////////////
voidscreen_initial()
{
P_RS=0;//RS为低电平,进行控制指令
screen_instruct(dis_on);//发送打开显示指令
screen_delay1(70);
screen_instruct(clear);//发送清屏指令
screen_delay1(3000);
screen_instruct(in_mode);//发送输入方式指令
screen_delay1(7);
screen_instruct(0x0c);//显示状态的游标和游标位置不显示
screen_delay1(7);
screen_instruct(home);//发送地址归位指令
screen_delay1(3000);
}
///////////////////////////////////////////////////////////////////////////////
//发送控制指令函数
///////////////////////////////////////////////////////////////////////////////
voidscreen_instruct(uintdat)
{
uintch;
P_RS=1;
ch=0xf8;
screen_bit8_serial_input(ch);
ch=dat&0xf0;
screen_bit8_serial_input(ch);
ch=dat<<4;
screen_bit8_serial_input(ch);
P_RS=0;
}
///////////////////////////////////////////////////////////////////////////////
//输入字节函数
///////////////////////////////////////////////////////////////////////////////
voidscreen_bit8_serial_input(uintix)
{
uinti;
P_E=0;
for(i=0;i<8;i++)
{
if((ix&0x80)==0)
P_RW=0;
else
P_RW=1;
ix<<=1;
P_E=1;
P_E=0;//使能端发出一个负跳变沿脉冲,详见MPU写资料模块
}
}
///////////////////////////////////////////////////////////////////////////////
//写入函数
///////////////////////////////////////////////////////////////////////////////
voiddis_str(ucharx,uchary,uchar*str)//16*4
{
dataucharloc,*p;
codeucharxline[5]={0,1,3,2,4};
p=str;
x=xline[x];
screen_instruct(home);
loc=(16*(x-1)+y-1);
if(loc&0x01){
loc/=2;
screen_instruct(loc|0x80);
screen_write_abyte('');
}
else
{
loc/=2;
screen_instruct(loc|0x80);
}
while(*p)
{
screen_write_abyte(*p++);
}
screen_instruct(0x9e);screen_write_abyte('');
}
///////////////////////////////////////////////////////////////////////////////
//发送数据
///////////////////////////////////////////////////////////////////////////////
voidscreen_write_abyte(uintdat)
{
uintch;
P_RS=1;
ch=0xfa;
screen_bit8_serial_input(ch);
ch=dat&0xf0;
screen_bit8_serial_input(ch);
ch=dat<<4;
screen_bit8_serial_input(ch);
P_RS=0;
}
///////////////////////////////////////////////////////////////////////////////
voidscreen_delay_ms()
{
unsignedinti,j;
for(i=0;i<500;i++)
for(j=0;j<500;j++);
}
//*************主函数*******************************************
voidmain()
{
uintTem;//温度变量
uintTemh;
SCK=1;
SDA=1;
delay(4);
SCK=0;
delay(1000);
SCK=1;
screen_initial();//液晶初始化函数
dis_str(1,3,"东北农业大学");//前边的1表明是哪一行,后边的3表明从第一行的第3个
dis_str(2,1,"温度:
C");
dis_str(3,1,"环境:
C");
while
(1)
{
Tem=readtemp();//读取温度
display(Tem);//显示温度
Temh=wreadtemp();
displayw(Temh);
delay(10000);//延时再读取温度显示10000
delay(10000);
delay(10000);
delay(10000);
}
}
//*********输入转换并显示*********
voiddisplay(uintTem)
{
uintT,a,b;
T=Tem*2;
if(T>=27315)//温度为正
{
T=T-27315;//
a=T/100;//温度整数
b=T-a*100;//温度小数
ltable[0]=a/100+'0';
ltable[1]=a%100/10+'0';
ltable[2]=a%10+'0';
ltable[3]='.';
ltable[4]=b/10+'0';
ltable[5]=b%10+'0';
dis_str(2,7,ltable);
}
else//温度为负
{
T=27315-T;
a=T/100;
b=T-a*100;
ltable[0]='-';
ltable[1]=a/10+'0';
ltable[2]=a%10+'0';
ltable[3]='.';
ltable[4]=b/10+'0';
ltable[5]=b%10+'0';
dis_str(2,7,ltable);
}
}
voiddisplayw(uintTem)
{
uintT,a,b;
T=Tem*2;
if(T>=27315)//温度为正
{
T=T-27315;//
a=T/100;//温度整数
b=T-a*100;//温度小数
ltable[0]=a/100+'0';
ltable[1]=a%100/10+'0';
ltable[2]=a%10+'0';
ltable[3]='.';
ltable[4]=b/10+'0';
ltable[5]=b%10+'0';
dis_str(3,7,ltable);
}
else//温度为负
{
T=27315-T;
a=T/100;
b=T-a*100;
ltable[0]='-';
ltable[1]=a/10+'0';
ltable[2]=a%10+'0';
ltable[3]='.';
ltable[4]=b/10+'0';
ltable[5]=b%10+'0';
dis_str(3,7,ltable);
}
}
//************************************
voidstart(void)//停止条件是SCK=1时,SDA由1到0
{
SDA=1;
delay(4);
SCK=1;
delay(4);
SDA=0;
delay(4);
SCK=0;
delay(4);
}
//------------------------------
voidstop(void)//停止条件是SCK=1时,SDA由0到1
{
SCK=0;
delay(4);
SDA=0;
delay(4);
SCK=1;
delay(4);
SDA=1;
}
//---------发送一个字节---------
voidSendByte(ucharnumber)
{
uchari,n,dat;
n=Nack_number;//可以重发次数
Send_again:
dat=number;
for(i=0;i<8;i++)//8位依次发送
{
if(dat&0x80)//取最高位
{
bit_out=1;//发1
}
else
{
bit_out=0;//发0
}
send_bit();//发送一个位
dat=dat<<1;//左移一位
}
read_bit();//接收1位应答信号
if(bit_in==1)//无应答时重发
{
stop();
if(n!
=0)
{
n--;//可以重发Nack_number=10次
gotoRepeat;//重发
}
else
{
gotoexit;//退出
}
}
else
{
gotoexit;
}
Repeat:
start();//重新开始
gotoSend_again;//重发
exit:
;//退出
}
//-----------发送一个位---------
voidsend_bit(void)
{
if(bit_out==1)
{
SDA=1;//发1
}
else
{
SDA=0;//发0
}
_nop_();
SCK=1;//上升沿
delay(4);delay(4);
SCK=0;
delay(4);delay(4);
}
//----------接收一个字节--------
ucharReadByte(void)
{
uchari,dat;
dat=0;//初值为0
for(i=0;i<8;i++)
{
dat=dat<<1;//左移
read_bit();//接收一位
if(bit_in==1)
{
dat=dat+1;//为1时对应位加1
}
}
SDA=0;//发送应答信号0
send_bit();
returndat;//带回接收数据
}
//----------接收一个位----------
voidread_bit(void)
{
SDA=1;//数据端先置1
bit_in=1;
SCK=1;//上升沿
delay(4);delay(4);
bit_in=SDA;//读数据
_nop_();
SCK=0;
delay(4);delay(4);
}
//------------------------------
uintreadtemp(void)
{
SCK=0;
start();//开始条件
SendByte(0x00);//发送从地址00
SendByte(0x07);//发送命令
start();//开始条件
SendByte(0x01);//读从地址00
bit_out=0;
tempL=ReadByte();//读数据低字节
bit_out=0;
tempH=ReadByte();//读数据高字节
bit_out=1;
err=ReadByte();//读错误信息码
stop();//停止条件
return(tempH*256+tempL);
}
uintwreadtemp(void)
{
SCK=0;
start();//开始条件
SendByte(0x00);//发送从地址00
SendByte(0x06);//发送命令
start();//开始条件
SendByte(0x01);//读从地址00
bit_out=0;
tempL=ReadByte();//读数据低字节
bit_out=0;
tempH=ReadByte();//读数据高字节
bit_out=1;
err=ReadByte();//读错误信息码
stop();//停止条件
return(tempH*256+tempL);
}
//------------延时--------------
voiddelay(uintn)
{
uintj;
for(j=0;j{
_nop_();
}
}