求大神帮忙看看那这个程序错在哪?测试时总是无法修改报警温度
#include <reg52.H>
#include <intrins.H>
#include <math.H>
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2^6;
sbit wela = P2^7;
sbit beep=P2^3;
sbit RS = P3^5;
sbit LCDEN = P3^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit rd=P3^7;
uchar b,s,g,x1,x2,flag;
uchar bai,shi,ge,xiao1,xiao2,fl;
unsigned char datas[] = {"0123456789"};
void delayUs()
{
_nop_();
}
void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}
void writeComm(uchar comm)
{
RS = 0;
P0 = comm;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
//写数据:RS=1, RW=0;
void writeData(uchar dat)
{
RS = 1;
P0 = dat;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
void init()
{
dula = wela = 0;
writeComm(0x38);
writeComm(0x0c);
writeComm(0x06);
writeComm(0x01);
}
void writeString(uchar * str, uchar length)
{
uchar i;
for(i = 0; i < length; i++)
{
writeData(str
);
}
}
/**//*****************************DS18B20*******************************/
sbit ds = P2^2;
void dsInit()
{
unsigned int i;
ds = 0;
i = 100;
while(i>0) i--;
ds = 1;
i = 4;
while(i>0) i--;
}
void dsWait()
{
unsigned int i;
while(ds);
while(~ds);
i = 4;
while(i > 0) i--;
}
uchar read_DS18B20(void)
{ uchar dat,numr,time;
dat=0;
for(numr=8;numr>0;numr--)
{
ds=1;
_nop_();
ds=0;
_nop_();
ds=1;
for(time=0;time<3;time++);
dat>>=1;
if(ds==1)
{
dat|=0x80;
}
else
{
dat|=0x00;
}
for(time=0;time<1;time++);
}
return(dat);
}
void writeByte(uchar dat)
{
uint i;
uchar j;
bit b;
for(j = 0; j < 8; j++)
{
b = dat & 0x01;
dat >>= 1;
if(b)
{
ds = 0; i++; i++;
ds = 1;
i = 8; while(i>0) i--;
}
else
{
ds = 0;
i = 8; while(i>0) i--;
ds = 1;
i++; i++;
}
}
}
void sendChangeCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0x44);
}
void sendReadCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0xbe);
}
int getTmpValue()
{
uint value;
uchar TL,TH,flag2;
sendReadCmd();
TL=read_DS18B20();
TH=read_DS18B20();
value=TH;
value<<=8;
value=value|TL;
if(value<0x0fff)
{
flag2=0;
value=value*(6.25);
}else
{value=~value+1;
value=value*(6.25);//温度值扩大100倍,精确到2位小数
flag2=1;
}
return(value);
}
/**//*****************************lcd显示*******************************/
void display(int v)
{
uchar table[] = "Temper:";
unsigned int tmp = abs(v);
bai = tmp / 10000;
shi = tmp % 10000 / 1000;
ge = tmp % 1000 / 100;
xiao1 = tmp % 100 / 10;
xiao2 = tmp % 10;
if(s1==0)
{
delayMs(8);
if(s1==0)
{while(!s1);
flag=0; //s1按键标志
}
}
if(flag==0)
{
writeComm(0xc3);
if(v < 0)
writeData('-');
else
writeData('+');
if(bai!= 0)
writeData(datas[bai]);
if(shi!=0)
{
writeData(datas[shi]);
}
if(ge!=0)
writeData(datas[ge]) ;
else
writeData('0');
_nop_();
writeData('.');
if(xiao1!=0)
writeData(datas[xiao1]);
else{writeData('0');}
if(xiao2!=0)
writeData(datas[xiao2]);
writeComm(0x80);
writeString(table, 7);
}
}
/**//*****************************键盘修改*******************************/
keyscan()
{
uint tmp1,vh;
tmp1=3145;
rd=0;
fl=0;
if(s3==0)
{
delayMs(8);
if(s3==0)
{
while(!s3);
vh=tmp1;
vh++;
writeComm(0xc0+8);
writeComm(0x0f);
flag=1; //s3按键标志
fl=1; //修改标志
}
}
if(fl==1)
{
tmp1=vh;
}
return(tmp1);
}
/**//*****************************报警值*******************************/
void baojing(uint tmp1)
{ uchar table1[] ="Danger:";
if(fl==1)
{
writeComm(0x80);
writeString(table1,7);
writeComm(0xc3);
if(tmp1 < 0)
writeData('-');
else
writeData('+');
b = tmp1 / 10000;
s = tmp1 % 10000 / 1000;
g = tmp1 % 1000 / 100;
x1 = tmp1 % 100 / 10;
x2 = tmp1 % 10;
if(b!= 0)
writeData(datas);
if(s!=0)
writeData(datas);
if(g!=0)
writeData(datas[g]) ;
else
writeData('0');
_nop_();
writeData('.');
if(x1!=0)
{writeData(datas[x1]);}
else{writeData('0');}
writeData(datas[x2]);
writeComm(0xc0+8);
}
}
/**//*****************************DS18B20*******************************/
void main()
{
sendChangeCmd();
init();
while(1)
{
delayMs(1000); //温度转换时间需要750ms以上
sendChangeCmd();
display(getTmpValue());
baojing(keyscan());
}
}
{
if(b!= 0)
writeData(datas);
}
datas里面的值并没有发生变化
一周热门 更多>