为啥我的数码管的第一位不能显示“1”和“4”,其他都正常,求大佬救命,已经卡bug三天了
- #include"reg51.h"
- #include"intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit wei=P2^0;
- sbit duan=P2^1;
- sbit DQ=P3^7;
- sbit bep=P3^0;
- uchar k=0;
- uchar smg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uint delay5(uchar n)
- {
- do
- {
- _nop_();
- _nop_();
- _nop_();
- n--;
- }
- while(n);
- }
- void init_ds18b20(void)
- {
- uchar x=0;
- DQ =0;
- delay5(120);
- DQ =1;
- delay5(16);
- delay5(80);
- }
- uchar readbyte(void)
- {
- uchar i=0;
- uchar date=0;
- for(i=8;i>0;i--)
- {
- DQ =0;
- delay5(1);
- DQ =1;
- date>>=1;
- if(DQ)
- date|=0x80;
- delay5(11);
- }
- return(date);
- }
- void writebyte(uchar dat)
- {
- uchar i=0;
- for(i=8;i>0;i--)
- {
- DQ =0;
- DQ =dat&0x01;
- delay5(12);
- DQ = 1;
- dat>>=1;
- delay5(5);
- }
- }
- uint retemp(void)
- {
- uchar a,b;
- uint tt;
- uint t;
- init_ds18b20();
- writebyte(0xcc);
- writebyte(0x44);
- init_ds18b20();
- writebyte(0xcc);
- writebyte(0xbe);
- a=readbyte();
- b=readbyte();
- t=b;
- t<<=8;
- t=t|a;
- tt=t*0.0625*10;
- k=1;
- return(tt);
- }
- void Display(uint temp)
- {
- uchar A1,A2,A3;
- A1 = temp/100; //百位
- A2 = temp%100/10; //十位
- A3 = temp%10; //个位
-
- wei = 1; //消隐
- P0 = 0xff;
- wei = 0;
- delay5(255);
- duan = 1;
- P0 = 0x00;
- duan = 0;
- delay5(255);
-
-
- wei = 1;
- P0 = 0xfe;
- wei = 0;
- delay5(255);
- duan = 1;
- P0 = smg[A1];
- duan = 0;
- delay5(255);
-
- wei = 1; //消隐
- P0 = 0xff;
- wei = 0;
- delay5(255);
- duan = 1
- P0 = 0x00
- duan = 0;
- delay5(255);
-
- wei = 1;
- P0 = 0xfd;
- wei=0;
- delay5(255);
- duan = 1;
- P0 = smg[A2]|0x80;
- duan = 0;
- delay5(255);
- wei = 1; //消隐
- P0 = 0xff;
- wei = 0;
- delay5(255);
- duan = 1;
- P0 = 0x00;
- duan = 0;
- delay5(255);
- wei = 1;
- P0 = 0xfb;
- wei=0;
- delay5(255);
- duan=1;
- P0 = smg[A3]; //显示个位
- duan = 0;
- delay5(255);
- wei = 1; //消隐
- P0 = 0xff;
- wei = 0;
- delay5(255);
- duan = 1;
- P0 = 0x00;
- duan = 0;
- delay5(255);
- }
- void main(void)
- {
- uchar i;
- uint temp;
- delay5(100);
-
- while(1)
- {
- temp=retemp();
- if(k==1)
- {k=0;
- Display(temp);
- if(temp>=300)
- {bep=1;}
- else
- {bep=0;}
- }
-
- }
- }
复制代码
|