#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
int datah,datal,i,ad,b;
int date1;
char a;
sbit CLOCK=P2^1;
sbit CS=P2^2;
sbit EOC=P2^0;
sbit ADIN=P2^3;
sbit DOUT=P2^4;
char duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char wei[]={0x06,0x05,0x03};
char zhong[]={0,0,0};
void delay(uchar t) //延时函数
{
char n;
while(t--)
{
for(n=10;n>0;n--);
}
}
void init_ding() //定时器初始化
{
TMOD=0x01;
TH0=(65536-10000)/256; //5ms扫描一次
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
}
int readTLC1543(uchar port) // ad转换函数
{
CLOCK=0;
CS=0;
EOC=1;
port<<=4;
for(i=0;i<4;i++) //进行通道的选择
{
ADIN=(bit)(port&0x80);
CLOCK=1;
CLOCK=0;
port<<=1;
}
for(i=0;i<6;i++) //产生6个时钟脉冲,进行数据的采样
{
CLOCK=1;
CLOCK=0;
}
CS=1;
delay(10);
CS=0;
for(i=0;i<2;i++) //读出高两位数据
{
CLOCK=1;
datah<<=1;
if(DOUT)datah|=0x01;
CLOCK=0;
}
for(i=0;i<8;i++) //读出后八位数据
{
CLOCK=1;
datal<<=1;
if(DOUT)datal|=0x01;
CLOCK=0;
}
EOC=0;
CS=1;
ad=datah;
ad<<=8;
ad|=datal;
return(ad);
}
void main()
{
init_ding();
while(1)
{
date1=readTLC1543(0x01);
P2=date1;
zhong[2]=date1%10;
zhong[1]=date1%100/10;
zhong[0]=date1/100;
}
}
void timer0() interrupt 1 //定时器中断函数
{
TR0=0;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
P0=duan[zhong[2]];
P3=wei[a];
a++;
if(a==3)
a=0;
TR0=1;
}
|