|
仿真了下18b20,都显示出来了,加了4个模块后,一段时间,?
[复制链接]
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
static uchar th,tl,b,c,d,f,j,e;
uint a, temp;
sbit morehigh=P1^0 ;
sbit high=P1^1 ;
sbit low=P1^2 ;
sbit morelow=P1^3 ;
sbit R=P2^3;
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
sbit speak=P2^5;
sbit di=P2^4;
sbit close=P2^6;
sbit open=P2^7;
unsigned char time ;
void motor();
void motor1();
void rotate();
unsigned char code table[]={"0123456789.:'C"};
unsigned char code table1[]={"temp:"};
unsigned char code table2[]={"the angular is "};
unsigned char code error[]={" error!"};
unsigned char code table4[]={"the angular is "};
void delay1us(unsigned char x)
{
uint i;
x*=5/4;
for(i=0;i
}
void delay1ms()
{ unsigned int x,y; for(x=0;x<10;x++) for(y=33;y>0;y--); }
void delay(unsigned int z)
{ unsigned char x; for(x=z;x>0;x--) delay1ms();}
void change();
void write_com(unsigned char com)
{
lcdrs=0;
SBUF=com; // 原来并行通信时, 这条 是 P1=com;
// P1=com;
while(TI==0);TI=0; // 这条 是我改过的, 原来并行通信时,这条 是没有的
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(unsigned char dat)
{
lcdrs=1;
SBUF=dat; // 原来并行通信时, 这条 是 P1=com;
while(TI==0);TI=0; // 这条 是我改过的, 原来并行通信时,这条 是没有的
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init(void)
{
lcden=0;
lcdrw=0;
write_com(0x01);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
}
/*DS18b20操作program*/
bit datayn(void) //初始化设置
{
bit flag;
di=1;
for(time=0;time<2;time++);
di=0;
for(time=0;time<200;time++);
di=1;
for(time=0;time<10;time++);
flag=di;
for(time=0;time<100;time++);
di=1;
for(time=0;time<10;time++);
return(flag) ;
}
void Delayxus_DS18B20(unsigned int t)
{
for(t;t>0;t--)
{
_nop_();_nop_();_nop_();_nop_();
}
_nop_(); _nop_();
}
unsigned char Read_Bit()
{
unsigned char ret;
di=0;//拉低总线
_nop_(); _nop_();
di=1;//释放总线
// _nop_(); _nop_();
// _nop_(); _nop_();
ret=di;//读时隙产生7 us后读取总线数据。把总线的读取动作放在15us时间限制的后面是为了保证数据读取的有效性
Delayxus_DS18B20(3);//延时60us,满足读时隙的时间长度要求
di=1;//释放总线
return ret; //返回读取到的数据
}
uchar read_onechar(void)
{
unsigned char i;
unsigned char dat=0;
for(i=0;i<8;i++)
{
dat>>=1; //先读最低位
if(Read_Bit())
dat|=0x80;
}
return(dat);
}
void write_onechar(uchar dat)
{
uchar i,time;
di=1; _nop_();
for(i=0;i<8;i++)
{
di=0; // delay1us(2); //for(time=0;time<4;time++);
di=dat&0x01; delay1us(20); // for(time=0;time<10;time++);
di=1;
_nop_(); //_nop_();
dat>>=1;
}
for(time=0;time<2;time++);
}
void display(void)
{
unsigned int i=0;
write_com(0x80);
if(datayn()==1)
{
while(error!='\0')
{
write_data(error);
i++;
}
}
else
{
while(table1!='\0')
{
write_data(table1);
i++;
}
}
}
void readtemp()
{
di=1;_nop_();_nop_();
datayn();
write_onechar(0XCC);
write_onechar(0x44);
for(time=0;time<100;time++);
datayn();
write_onechar(0XCC);
write_onechar(0XBE);
for(time=0;time<200;time++);
tl=read_onechar(); th=read_onechar();
// while ((tl>>4|th<<4)==85);
di=1; _nop_(); _nop_(); // nop_();
if (th>>4==0)
{
e=1;
a=tl>>4|th<<4;
b=a/100;c=(a%100)/10;d=a%10;
j=tl&0x0f;f=(j*10/16)%10;
write_com(0x85); write_data('\0');
}
else
{
e=0;
temp=(~(tl|th<<8)+1);temp*=0.0625;b=temp/100;c=temp/10%10;d=temp%10;f=(temp*10)%10;
write_com(0x85); write_data('-');
}
write_com(0x86); write_data(table);write_data(table[c]);
write_data(table[d]);write_data(0x2e);write_data(table[f]);
write_data(table[12]);write_data('c');
}
void main()
{
SCON=0x00;
R=0; _nop_(); _nop_(); R=1;
init();
display();
while(1)
{
readtemp();
motor();
motor1();
rotate();
}
}
void motor()
{
P0=0XFF;
// uchar t;
while (close==0)
{
uchar i,t; t=0xfe;
while (close==0);
for(i=0;i<4;i++)
{
P0=t;
delay(1000);
t=_crol_(t,1);
}
}
while (open==0)
{
uchar i,t; t=0xf7;
while (open==0);
for(i=0;i<4;i++)
{
P0=t; delay(1000);t=_cror_(t,1);
}
}
}
void motor1()
{
P1=0XFF;
// uchar t;
while ( high==0)
{
uchar i,t;
while (1)
{ t=0xfe;
for(i=0;i<4;i++)
{
P0=t;
delay(100);
t=_crol_(t,1);
}
}
}
while (low==0)
{
uchar i,t;
while (low==0)
{ t=0xf7;
for(i=0;i<4;i++)
{
P0=t;
delay(20); t=_cror_(t,1);
}
}
}
}
void alarm()
{
uchar j=10;
while (j--)
{
speak=~speak;
delay(30);
}
}
void rotate()
{
if(a>100)
{
uchar i,t;
while (1)
{ t=0xfe;
for(i=0;i<4;i++)
{
P0=t;
delay(20);
t=_crol_(t,1);
alarm();
}
}
}
if(a>90&&a<100)
{
uint uk ;
uk=55-a;
while (uk--)
{
{
uchar i,t; t=0xfe;
for(i=0;i<4;i++)
{
P0=t;
delay(20);
t=_crol_(t,1);
}
}
}
}
if(a>50&&a<90) P0=0xff;
if(a>40&&a<50)
{
uint uh ;
uh=55-a;
while (uh--)
{
{
{
uchar i,t; t=0xf7;
for(i=0;i<4;i++)
{
P0=t;
delay(20);
t=_cror_(t,1);
}
}
}
}
}
if(a<40)
{
uchar i,t;
{
t=0xf7;
for(i=0;i<4;i++)
{
P0=t;
delay(20); t=_cror_(t,1);
alarm();
}
}
}
}
仿真了下18b20,都显示出来了,也正常,加了4个模块(根据不同的温度调试电机的转速)后,也正常,按钮也正常,一段时间后,温度反应异常了 。void motor() ,void motor1(),void alarm(),void rotate()。前三个也没事,就是void rotate()出问题了,哪位大神看见后,帮忙分析下 怎么回事啊 ,小弟先在此谢谢了。还有就是刚开始就显示85,怎么一开始就显示正确呢
|
|