|
#include"reg52.h"
#define u16 unsigned int
#define u8 unsigned int
sbit ST=P1^0;
sbit EOC=P1^1;
sbit OE=P1^2;
sbit ADDA=P1^3;
sbit ADDB=P1^4;
sbit ADDC=P1^5;
sbit ALE=P1^6;
sbit ADC0809CLK=P1^7;
u16 adc0809=0;
sbit p20=P2^0;
sbit p21=P2^1;
sbit p22=P2^2;
sbit p23=P2^3;
sbit p24=P2^4;
sbit p25=P2^5;
sbit p26=P2^6;
sbit p27=P2^7;
u16 sample;
u16 number=0;
u16 MAX=500000;
u16 ber=0;
void delay1(uchar i)
{
uchar j;
while(i--)
{
for(j=125;j>0;j--);
}
}
void AD0809OPERATION()
{
ST=0;
ALE=0;
ADDA=1;
ADDB=0;
ADDC=0;
delay1(15);
ALE=1;
ALE=1;
ST=1;//启动AD转换
ALE=1;
delay1(20);
ALE=0;
ST=0;
while(EOC==0);
if(EOC==1)
{
OE=1;
delay1(3);
adc0809=P2;
adc0809=P2;
}
}
void Usartlnit()
{
SCON=0X50;//设置工作方式1
TMOD=0X20;//设置计数器工作方式2
PCON=0X80;//波特率加倍
TH1=0XF3;//计数器初始值设置,注意波特率是4800的
TL1=0XF3;
ES=1;//打开接收中断
EA=1;//打开总中断
TR1=1;//打开计数器
}
void rs232topc(unsigned char d)
{
SBUF=d;
while(!TI);
TI=0;
}
unsigned char getASCII(unsigned char port)
{
return port+'0';
}
void main()
{
Usartlnit();//串口初始化
TL0=0XFE;
TH0=0XFF;
IE=0X82;
TR0=1;
ADDA=0;
ADDB=0;
ADDC=0;
ADC0809CLK=0;
number=0;
ber=0;
while(1)
{
if (number%MAX==0)
{
AD0809OPERATION();
number=0;
if(p27==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p26==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p25==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p24==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p23==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p22==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p21==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
if(p20==0)
{
rs232topc('0');
}
else
{
rs232topc('1');
}
rs232topc(',');
}
}
}
void T0IP()interrupt 1
{
number=number+1;
TL0=0XFE;
TH0=0XFF;
ADC0809CLK=!ADC0809CLK;
}
|
|