/**************************************************

实验名称:AD实验一

实验目的:利用单片机控制ADC0804进行模数转换,拧动

 电位器Re2时数码管前三位以十进制动态显示

 A/D转换后的数字量(0~255) ?

实验备注:

**************************************************/

#include

#include


#define uchar unsigned char

#define uint unsigned int


sbit DUAN = P2^6;

sbit WEI = P2^7;

sbit ADRD = P3^7;

sbit ADWR = P3^6;


uchar code duan_table[]=

{0x3f,0x06,0x5b,0x4f,

 0x66,0x6d,0x7d,0x07,

 0x7f,0x6f,0x77,0x7c,

 0x39,0x5e,0x79,0x71};


uchar code wei_table_ad[]=

{0x7e,0x7d,0x7b,

 0x77,0x6f,0x5f};


void delay1ms(uchar);

void display(uchar);


void main()

{

uchar i,advar;

WEI = 1;

P0 = 0x7f;

WEI = 0;

while(1)

{

ADWR = 1;

_nop_();

ADWR = 0;

_nop_();

ADWR = 1;

for(i = 0; i < 5; i++)

{

display(advar);

}

ADRD = 1;

_nop_();

ADRD = 0;

_nop_();

advar = P1;//从高电平变低电平后,

ADRD = 1;  //读完书再变回高电平。

_nop_();

}

}


void delay1ms(uchar x)

{

int i, j;

for(i = 0; i < x; i++)

for(j = 0; j < 110; j++);

}


void display(uchar num)

{

DUAN = 1;

P0 = duan_table[num/100];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[0];

WEI = 0;

delay1ms(1);

DUAN = 1;

P0 = duan_table[num%100/10];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[1];

WEI = 0;

delay1ms(1);


DUAN = 1;

P0 = duan_table[num%10];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[2];

WEI = 1;

delay1ms(1);

}