#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code table[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //表 uchar code table2[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //带DP点 uchar code weiM[4] = {0xf7,0xfb,0xfd,0xfe}; uchar data duanM[4] = {0x00,0x00,0x00,0x00}; sbit DQ = P3^4; //数据口 sbit P12 = P1^2; //时间、日期、温度显示转换 sbit P14 = P1^4; //显示暂停 sbit P11 = P1^1; //选择想要改变的数码管的位选 sbit P13 = P1^3; //码值加1 void delay(uchar i) { while(--i); } void delay2(uint i) { while(--i); } void Init_Ds18b20(void) { DQ = 1; delay(1); DQ = 0; delay(250); DQ = 1; delay(100); }
uchar Read_One_Byte() { uchar i = 0; uchar dat = 0; for(i=8;i>0;i--) { DQ = 0; _nop_(); dat >>= 1; DQ = 1; delay(1); if(DQ) { dat |= 0x80; } delay(10); } return (dat); }
void Write_One_Byte(uchar dat) { uchar i = 0; for(i=8;i>0;i--) { DQ = 0; _nop_(); DQ = dat&0x01; delay(10); DQ = 1; dat >>= 1; delay(1); } }
uint Get_Tmp(void) { double tt; uint temp; uchar a,b; Init_Ds18b20(); Write_One_Byte(0xcc); Write_One_Byte(0x44); Init_Ds18b20(); Write_One_Byte(0xcc); Write_One_Byte(0xbe); a = Read_One_Byte(); b = Read_One_Byte(); temp = b; temp <<= 8; temp = temp|a; tt = temp*0.0625; temp = tt*100+5; return temp; }
void TDisp(uint temp) { uint A1,A2,A3,A4; uint i; A1 = temp/1000; //百位 duanM[3] = A1; A2 = temp%1000/100; //十位 duanM[2] = A2; A3 = temp%1000%100/10; //个位 duanM[1] = A3; A4 = temp%10; duanM[0] = A4;
for(i=0;i<4;i++) { if(i==2) { P2 = 0xff; P0 = 0xff; P0 = table2[duanM]; P2 = weiM; delay2(250); } else { P2 = 0xff; P0 = 0xff; P0 = table[duanM]; P2 = weiM; delay2(250); } } } void main(void) { uint tt; while(1) { tt=Get_Tmp(); TDisp(tt); } }
|