仿真原理图如下
仿真电路图
单片机源程序如下:
- #include
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={ ~0x3F,/*0*/
- ~0x06,/*1*/
- ~0x5B,/*2*/
- ~0x4F,/*3*/
- ~0x66,/*4*/
- ~0x6D,/*5*/
- ~0x7D,/*6*/
- ~0x07,/*7*/
- ~0x7F,/*8*/
- ~0x6F,/*9*/
- };
- uchar Tcount;
- uint wei3,wei2,wei1,wei0,num,wei4,wei5,wei6,wei7;
- bit timerflag,ADflag;
- sbit AD_end =P3^2;
- sbit OE=P3^3;
- sbit ST=P3^4;
- sbit channel=P3^5;
- void AD_INT() interrupt 0
- {ADflag=1;
- AD_end=1;
- }
- void T0_int()interrupt 1
- {TL0|=0xb0;
- TH0=0x3c;
- if(--Tcount==0)
- {Tcount=20;
- timerflag=1;}
-
- }
- void delay(uint z)
- {uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void main()
- {
- channel = 0;
- Tcount=20;
- TMOD=0x01;
- TL0=0xb0;
- TH0=0x3c;
- IT0=1;
- TR0=1;
- ET0=1;
- EX0=1;
- EA=1;
- while(1)
- {
-
- if(timerflag==1)
- {timerflag=0;
- ADflag=0;
- ST=1;
- ST=0;
-
- while(!ADflag);
- {
- if(channel == 0)
- {
- OE=1;
- num=P2*20-2500;
- wei0=num/1000;
- wei1=num%1000/100;
- wei2=num%100/10;
- wei3=num%10;
- }
- else
- {
- OE=1;
- num=P2*20/100*51;
- wei4=num/1000;
- wei5=num%1000/100;
- wei6=num%100/10;
- wei7=num%10;
- }
- OE=0;
- ADflag=0;
- channel = ~channel;
- }
- }
-
- //显示电流
- P1=0x01;
- P0=table[wei0];
- delay(1);
- P1=0x02;
- P0=table[wei1]&0X7F;
- delay(1);
- P1= 0X04;
- P0=table[wei2];
- delay(1);
- // P1=0x08;
- // P0=table[wei3];
- // delay(1);
-
- //显示电压
- P1=0x10;
- P0=table[wei4];
- delay(1);
- P1=0x20;
- P0=table[wei5];
- delay(1);
- P1=0x40;
- P0=table[wei6]&0x7f;
- delay(1);
- P1=0x80;
- P0=table[wei7];
- delay(1);
- }
- }
-