51单片机读时序问题?
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P3^6;
sbit wela=P3^7;
sbit DQ=P2^0;
//uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table[]={//共阴极0~f数码管编码
0x3f,0x06,0x5b,0x4f, //0~3
0x66,0x6d,0x7d,0x07, //4~7
0x7f,0x6f,0x77,0x7c, //8~b
0x39,0x5e,0x79,0x71 //c~f
};
void delayus(uchar us){
while(us--);
}
void init_ds18b20(){
uchar init_resu;
DQ=1;
delayus(1);
DQ=0;
delayus(60);
DQ=1;
delayus(1);
init_resu=DQ;
delayus(30);
DQ=1;
delayus(20);
}
void write(uchar dat){
uchar i;
for(i=0;i<8;i++){
DQ=0;
delayus(1);
DQ=dat&0x01;
delayus(5);
DQ=1;
dat>>=1;
}
}
uchar read(){
uchar i,dat;
for(i=0;i<8;i++){
dat>>=1;
DQ=0;
delayus(2);
if(DQ)
dat|=0x80;
else
dat&=0x7f;
delayus(10);
DQ=1;
_nop_();
}
return dat;
}
uchar read_temp(){
uchar a,b;
init_ds18b20();
write(0xcc);
write(0x44);
init_ds18b20();
write(0xcc);
write(0xbe);
a=read();//a为低八位数据
b=read();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display(){
uint tt;
int shi,ge;
tt=(int)(read_temp()*0.625);
//tt=12;
shi=tt/10;
ge=tt%10;
P1=0XFF;
dula=1;
P1=table[shi];
dula=0;
delayus(500);
P1=0xff;
wela=1;
P1=0xfd;
wela=0;
dula=1;
P1=table[ge];
dula=0;
delayus(500);
P1=0xff;
wela=1;
P1=0xfe;
wela=0;
}
void main(){
while(1){
display();
}
}
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P3^6;
sbit wela=P3^7;
sbit DQ=P2^0;
//uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table[]={//共阴极0~f数码管编码
0x3f,0x06,0x5b,0x4f, //0~3
0x66,0x6d,0x7d,0x07, //4~7
0x7f,0x6f,0x77,0x7c, //8~b
0x39,0x5e,0x79,0x71 //c~f
};
void delayus(uchar us){
while(us--);
}
void init_ds18b20(){
uchar init_resu;
DQ=1;
delayus(1);
DQ=0;
delayus(60);
DQ=1;
delayus(1);
init_resu=DQ;
delayus(30);
DQ=1;
delayus(20);
}
void write(uchar dat){
uchar i;
for(i=0;i<8;i++){
DQ=0;
delayus(1);
DQ=dat&0x01;
delayus(5);
DQ=1;
dat>>=1;
}
}
uchar read(){
uchar i,dat;
for(i=0;i<8;i++){
dat>>=1;
DQ=0;
delayus(2);
if(DQ)
dat|=0x80;
else
dat&=0x7f;
delayus(10);
DQ=1;
_nop_();
}
return dat;
}
uchar read_temp(){
uchar a,b;
init_ds18b20();
write(0xcc);
write(0x44);
init_ds18b20();
write(0xcc);
write(0xbe);
a=read();//a为低八位数据
b=read();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display(){
uint tt;
int shi,ge;
tt=(int)(read_temp()*0.625);
//tt=12;
shi=tt/10;
ge=tt%10;
P1=0XFF;
dula=1;
P1=table[shi];
dula=0;
delayus(500);
P1=0xff;
wela=1;
P1=0xfd;
wela=0;
dula=1;
P1=table[ge];
dula=0;
delayus(500);
P1=0xff;
wela=1;
P1=0xfe;
wela=0;
}
void main(){
while(1){
display();
}
}