|
包含完整代码,Proteus仿真等
源码如下:
- #include <at89x51.h>
- #define uchar unsigned char
- #define unit unsigned int
- sbit eoc=P3^0;
- sbit oe =P3^1;
- sbit st =P3^3;
-
- sbit s1=P2^5;
- sbit s2=P2^6;
- sbit s3=P2^7;
- sbit P17=P1^7;
- sbit pwm=P3^5;
-
- uchar code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x0,0x0};//数码管共阳级显示0~9,不显示
- uchar disp[4]={0,0,0,0};
- unit ad_0809;
- uchar out;
- uchar scale=100;
- uchar tt;
- void delaynms(unit x);
- void display();
- void ad0809();
- void key();
- void InitTimer(void)
- {
- TMOD = 0x02;
- TH0 = 0x00;
- TL0 = 0x0FB;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void main()
- {
- EA=1;
- EX0=1;
- IT0=1;//下降沿触发,cpu内存占用少,如果是低电平触发,cpu将耗大量内存在中断上,长时间的取ad转换数据
- INTtimer();
- while(1)
- { s1=s2=s3=0;//channel 0
-
- P0=0xff;//input
- oe=0;
- st=0;
- st=1;//清0
- st=0;//启动
-
- ad_0809=out;
- display();
- scale=disp[3]*200/5;
- }
- }
- void EXINTO(void) interrupt 0
- { EA=0;
- EX0=0;
- oe=1;
- out=P0;
- oe=0;
- EA=1;
- EX0=1;
-
- }
- void delaynms(unit x)
- { while(x-->0)
- {
- unsigned char k;
- for(k=10;k>0;k--);
- }
- }
- void display()
- { unit i,scan;
- disp[3]=(ad_0809*196)/10000; //the highest bit, 255*49/25=4.99V
- disp[2]=((ad_0809*196)/1000)%10;
- disp[1]=((ad_0809*196)/100)%10;
- disp[0]=((ad_0809*196)/10)%10;
-
- scan=1;
- for(i=0;i<4;i++)
- {
- P2=~scan;
- P1=tab[disp[i]];
- delaynms(10);
- scan<<=1;
- if(i==3)
- {
- P17=1;
- }
- }
- }
- void timer0_int () interrupt 1 using 2 // T0溢出中断,使用工作组2
- {
-
- tt++;
- if(tt==200)
- {
- tt=0;
- pwm=1;
- }
- if(scale==tt)
- pwm=0;
- }
复制代码
|
评分
-
查看全部评分
|