include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int uchar temp1,temp2,temp,A; uchar sb[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0xBf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; sbit DQ=P3^7; void delay(uint t) { uint j,k; for(j=t;j>0;j--) for(k=115;k>0;k--); } void display_1820(uchar x) { uint i,ge,shi,xiao; shi=(x/10); ge=(x%10); xiao=(x%10/10); P0=sb[i]; for(i=0;i<4;i++) { if(i==0){P0=0x00;P0=sb[shi];P2=0xfd;delay(1);} if(i==0){P0=0x00;P0=sb[ge]|0x80;P2=0xfb;delay(1);} if(i==0){P0=0x00;P0=sb[xiao];P2=0xf7;delay(1);} } } void delay_18b20(uint i) { for(;i>0;i--); } void init() { uchar x=0; DQ=1; delay_18b20(8); DQ=0; delay_18b20(80); DQ=1; delay_18b20(14); x=DQ; delay_18b20(20); } uchar read() { uchar i=0; uchar dat=0; for(i=8;i>0;i--) { DQ=0; dat>>=1; DQ=1; if(DQ)dat|=0x80; delay_18b20(4); } return dat; } void write(uchar dat) { uchar i=0; for(i=8;i>0;i--) { DQ=0; DQ=dat&0x01; delay_18b20(5); DQ=1; dat>>=1; } } uchar read1() { uchar a=0,b=0,t=0; init(); write(0xcc); write(0x44); delay_18b20(100); init(); write(0xcc); write(0xbe); delay_18b20(100); a=read(); b=read(); temp1=b<<8; temp1+=(a&0x63)>>4; temp2=a&0x63; temp=((b*256+a)>>4); return temp; } void UART_Init() { TMOD&=0x63; TMOD|=0x20; TR1=1; TH1=0xF3; TL1=0xF3; SCON=0x50; PCON|=0x80; ES=1; EA=1; } void UART_SendByte(unsigned char Byte) { SBUF=Byte; while(TI==0); TI=0; } /*1ms=1,±1ms*/void delay_1ms(unsigned int _1ms){_1ms*=110;while(_1ms--){}} void main() { UART_Init(); while(1) { delay_1ms(5); //A=read1(); UART_SendByte(read1()); //display_1820(read1()); } } void UART_Routine() interrupt 4 { if(RI==1) { P2=~SBUF; UART_SendByte(SBUF); RI=0; } } |