|
今天刚学习了A/D转换器,设计了一个可测试滑动变阻器电压的简易程序代码如下:
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
void display(uchar bai,uchar shi,uchar ge);
void delay(unsigned int xms);
sbit st=P3^0;
sbit oe=P3^1;
sbit eoc=P3^2;
sbit w1=P2^1;
sbit w2=P2^2;
sbit w3=P2^3;
uchar code a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar adval,bai,shi,ge,t;
int main()
{
while(1)
{
st=0;
delay(1);
st=1;
delay(1);
st=0;
for(t=0;t<10;t++){
display(bai,shi,ge);
}
if(eoc=1){
oe=1;
adval=P1;
bai=adval/100;
shi=adval%100/10;
ge=adval%10;
}
}
return 0;
}
void display(uchar bai,uchar shi,uchar ge){
P0=a[bai];
w1=0;
w2=1;
w3=1;
delay(1);
P2=0xff;
P0=a[shi];
w1=1;
w2=0;
w3=1;
delay(1);
P2=0xff;
P0=a[ge];
w1=1;
w2=1;
w3=0;
delay(1);
P2=0xff;
}
void delay(unsigned int xms){
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
具体仿真图及代码见附件
|
|