在
仿真时为什么老是出现无论给一个多大电阻值,数码管显示的总是一个不变的数,可是此时的ad转换后的值也在变。想问大家这是什么问题
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uint datah,datal,i,ad,b;
uint date1;
char a;
sbit CLOCK=P2^1;
sbit CS=P2^2;
sbit EOC=P2^0;
sbit ADIN=P2^3;
sbit DOUT=P2^4;
int duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int wei[]={0x06,0x05,0x03};
char zhong[]={0,0,0};
void init_ding() //定时器初始化
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
}
uint 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);
zhong[2]=date1%10;
zhong[1]=date1%100/10;
zhong[0]=date1/100;
}
}
void
timer0() interrupt 1 //定时器中断函数
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
P3=0;
P0=duan[zhong[a]];
P3=wei[a];
a++;
if(a==3)
a=0;
}
一周热门 更多>