#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit csad=P0^7;//ad片选使能端
sbit wr=P3^6;
sbit rd=P3^7;
sbit diola=P2^5;//LED锁存器使能
uchar flag,i,t,lsp;
uint temp;
uchar code ascii[]={
0x30,0x31,0x32,0x33,
0x34,0x35,0x36,0x37,
0x38,0x39,0x2e,0x56};//ascii码表
uchar num[]={0,0,0,0,0};
void init()
{
TMOD=0x21;//定时器0工作方式1,定时器1工作方式2
SCON=0x40;//串口工作方式1
TH0=(65536-50000)/256;//初值50ms
TL0=(65536-50000)%256;
TH1=0xe8;//波特率1200
TL1=0xe8;
TR0=1;//定时器0启动
TR1=1;//定时器1启动
ET0=1;//定时器0中断打开
EA=1;//总中断打开
diola=0;//关闭LED锁存器,避免闪烁
csad=0;//启动ad芯片
lsp=196;
}
void send_dat()//数据发送函数
{
if(flag==1)//检测数据采集是否完成
{
flag=0;//完成标志复位
temp=temp*lsp;
num[0]=ascii[temp/10000];
num[1]=ascii[10];
num[2]=ascii[temp/1000%10];
num[3]=ascii[temp/100%10];
num[4]=ascii[11];//解析数据 进行数据赋值
for(i=0;i<5;i++)
{
SBUF=num[i];//发送数据
while(!TI);//检测是否发送完成
TI=0;//发送完成标志清零
}
}
}
void main()
{
init();
while(1)
{
send_dat();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;//装初值
t++;//计数
if(t==20)
{
t=0;//计数清零
rd=1;//准备读取数据
rd=0;
temp=P1;//读取数据
rd=1;
wr=1;
wr=0;
wr=1;//启动ad转换
flag=1;//数据采集完成标志置1
}
}
纯手打代码 亲测可用