我想用TLC2543采集信号,再用数码管显示出来。结果吧,数码管显示的及其不稳定,我本来以为是延时时间太长,现在把延时时间调整到最小,可是依然还是不稳定,而且,当通道0输入0时候,本来数码管该显示0000,但是就一直跳到。大家能帮忙看看这代码的问题吗?不胜感激啊
#include<reg52.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
sbit AD_CLOCK=P3^3;
sbit AD_IN=P3^7;
sbit AD_OUT=P3^5;
sbit AD_CS=P3^6;
uchar table[10]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90} ;
static const uchar ad_channel_select[]=
{0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78,0x88,
0x98,0xa8
};
uint ad2543(uchar chunnel_select)
{ uint din,j;
uchar dout,i;
din=0;
dout=ad_channel_select[chunnel_select];
for(j=0;j<100;j++); //延时大于1uS.
AD_CLOCK=0;
AD_CS=0;
for(i=0;i<12;i++)
{
if(dout&0x80) AD_IN=1;
else AD_IN=0;
AD_CLOCK=1;
dout<<=1;
din<<=1;
if(AD_OUT==1) din|=0x0001;
AD_CLOCK=0;
}
AD_CS=1;
for(j=0;j<100;j++); //延时大于1uS.
return(din);
}
void Delay_ms(uint ms)
{
for(;ms--;);
}
void display(uint num)
{
P1=table[num/1000];
P0=0x08;
Delay_ms(1);
P1=table[(num%1000)/100];
P0=0x04;
Delay_ms(1);
P1=table[((num%1000)%100)/10];
P0=0x02;
Delay_ms(1);
P1=table[((num%1000)%100)%10];
P0=0x01;
Delay_ms(1);
}
void main(void)
{
uint ad;
while (1)
{
ad=ad2543(0);
// ad=ad>>4;
ad=ad*1.2207;
display(ad);
}
}
一周热门 更多>