51单片机AD采集电压,输出十进制数给数码管显示
时间:10-02
整理:3721RD
点击:
![](../imgqa/pcb/pcb-160521nf133dnhk3z18kk85828.png)
/* Main.c file generated by New Project wizard
*
* Created: 周一 一月 16 2017
* Processor: AT89C52
* Compiler: Keil for 8051
*/
#include <reg51.h>
#include <stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan_sel=P2^6;
sbit wei_sel=P2^7;
sbit ad_rd=P3^6;
sbit ad_wr=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar bai,uchar shi,uchar ge)
{
duan_sel=1; //百位数据送数码管
P0=table[bai];
duan_sel=0;
P0=0xff;
wei_sel=1;
P0=0x7e;
wei_sel=0;
delayms(50);
duan_sel=1;
P0=table[shi];
duan_sel=0;
P0=0xff;
wei_sel=1;
P0=0x7d;
wei_sel=0;
delayms(50);
duan_sel=1;
P0=table[ge];
duan_sel=0;
P0=0xff;
wei_sel=1;
P0=0x7b;
wei_sel=0;
delayms(50);
}
void main()
{
uchar a,A1,A2,A3,adval;
wei_sel=1;
P0=0x7f;
wei_sel=0;
while(1)
{
ad_wr=1;
_nop_();
ad_wr=0;
_nop_();
ad_wr=1;
for(a=10;a>0;a--)
{
display(A1,A2,A3);
}
P1=0xff;
ad_rd=1;
_nop_();
ad_rd=0;
_nop_();
adval=P1;
A1=adval/100;
A2=adval%100/10;
A3=adval%10;
}
}
在display子函数中,3位十进制数那段代码后面的延时设置成5时,完全看不到数码管的点亮,设置成50还能看到正确的数字闪烁。
要它是什么用处for(a=10;a>0;a--)