原来的8个7端显示的 我想改成四个的就够的
而且他是小数点后两位的 我要一位就可以的
这个在温度是负数的时候好像有点问题的
帮帮看下啦
#include <AT89X52.H> #include <INTRINS.H> unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f}; unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0x00}; unsigned char code dotcode[32]={0,3,6,9,12,16,19,22, 25,28,31,34,38,41,44,48, 50,53,56,59,63,66,69,72, 75,78,81,84,88,91,94,97}; sbit SDA=P1^6; sbit SCL=P1^7; unsigned char displaybuffer[8]={0,1,2,3,4,5,6,7}; unsigned char eepromdata[8]; unsigned char temperdata[2]; unsigned char timecount; unsigned char displaycount; bit secondflag=0; unsigned char secondcount=0; unsigned char retn; unsigned int result; unsigned char x; unsigned int k; unsigned int ks; void delay(void); void delay10ms(void); void i_start(void); void i_stop(void); void i_init(void); void i_ack(void); bit i_clock(void); bit i_send(unsigned char i_data); unsigned char i_receive(void); bit start_temperature_T(void); bit read_temperature_T(unsigned char *p); void delay(void) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } void delay10ms(void) { unsigned int i; for(i=0;i<1000;i++) { delay(); } } void i_start(void) { SCL=1; delay(); SDA=0; delay(); SCL=0; delay(); } void i_stop(void) { SDA=0; delay(); SCL=1; delay(); SDA=1; delay(); SCL=0; delay(); } void i_init(void) { SCL=0; i_stop(); } void i_ack(void) { SDA=0; i_clock(); SDA=1; } bit i_clock(void) { bit sample; SCL=1; delay(); sample=SDA; _nop_(); _nop_(); SCL=0; delay(); return(sample); } bit i_send(unsigned char i_data) { unsigned char i; for(i=0;i<8;i++) { SDA=(bit)(i_data & 0x80); i_data=i_data<<1; i_clock(); } SDA=1; return(~i_clock()); } unsigned char i_receive(void) { unsigned char i_data=0; unsigned char i; for(i=0;i<8;i++) { i_data*=2; if(i_clock()) i_data++; } return(i_data); } bit start_temperature_T(void) { i_start(); if(i_send(0x90)) { if(i_send(0xee)) { i_stop(); delay(); return(1); } else { i_stop(); delay(); return(0); } } else { i_stop(); delay(); return(0); } } bit read_temperature_T(unsigned char *p) { i_start(); if(i_send(0x90)) { if(i_send(0xaa)) { i_start(); if(i_send(0x91)) { *(p+1)=i_receive(); i_ack(); *p=i_receive(); i_stop(); delay(); return(1); } else { i_stop(); delay(); return(0); } } else { i_stop(); delay(); return(0); } } else { i_stop(); delay(); return(0); } } void main(void) { P1=0xff; timecount=0; displaycount=0; TMOD=0x21; TH1=0x06; TL1=0x06; TR1=1; ET1=1; ET0=1; EA=1; if(start_temperature_T()) //向DS1624发送启动A/D温度转换命令,成功则启动T0定时1s。 { secondflag=0; secondcount=0; TH0=55536/256; TL0=55536%256; TR0=1; } while(1) { if(secondflag==1) { secondflag=0; TR0=0; if(read_temperature_T(temperdata)) //T0定时1s时间到,读取DS1624的温度值 { for(x=0;x<8;x++) { displaybuffer[x]=16; } x=2; result=temperdata[1]; //将读取的温度值进行数据处理,并送到显示缓冲区 while(result/10) { displaybuffer[x]=result%10; result=result/10; x++; } displaybuffer[x]=result; result=temperdata[0]; result=result>>3; displaybuffer[0]=(dotcode[result])%10; displaybuffer[1]=(dotcode[result])/10; if(start_temperature_T()) //温度值数据处理完毕,重新启动DS1624开始温度转换 { secondflag=0; secondcount=0; TH0=55536/256; TL0=55536%256; TR0=1; } } } } } void t0(void) interrupt 1 using 0 //T0用于定时1s时间到 { secondcount++; if(secondcount==100) { secondcount=0; secondflag=1; } TH0=55536/256; TL0=55536%256; } void t1(void) interrupt 3 using 0 //T1定时1ms用数码管的动态刷新 { timecount++; if(timecount==4) //T1定时1ms到 { timecount=0; P2=0xff; if (displaycount==5) { P0=(displaycode[displaybuffer][7-displaycount]] | 0x80); //在该位同时还要显示小数点 } else { P0=displaycode[displaybuffer][7-displaycount]]; } P2=displaybit[displaycount]; displaycount++; if(displaycount==8) { displaycount=0; } } }
|