俺的51开发板上的AD芯片是PCF8591,IIC总线的,想将AD转换的值实时显示在数码管上,同时还想利用LED灯亮度的变换来反映DA效果。结果数码管值总是不变化,LED灯也不亮,请大家帮忙找找问题,下面上图上代码
[mw_shl_code=c,true]#include <reg52.h>
#include <intrins.h>
sbit SDA=P2^0;
sbit SCL=P2^1;
sbit DU=P2^6;
sbit WE=P2^7;
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delayms(uchar ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void display(uint k)
{
P0=table[k/1000];
DU=1;
DU=0;
P0=0xfb;
WE=1;
WE=0;
delayms(1);
P0=table[k%1000/100];
DU=1;
DU=0;
P0=0xf7;
WE=1;
WE=0;
delayms(1);
P0=table[k%100/10];
DU=1;
DU=0;
P0=0xef;
WE=1;
WE=0;
delayms(1);
P0=table[k%10];
DU=1;
DU=0;
P0=0xdf;
WE=1;
WE=0;
delayms(1);
}
void delayus(void)
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void IIC_Start(void)
{
SDA=1;
delayus();
SCL=1;
delayus();
SDA=0;
delayus();
}
void IIC_Stop(void)
{
SDA=0;
delayus();
SCL=1;
delayus();
SDA=1;
delayus();
}
void IIC_Ack(void)
{
uchar i=0;
SCL=1;
delayus();
while((SDA==1)&&(i<250))
{
i++;
}
SCL=0;
delayus();
}
void IIC_NoAck(void)
{
SDA=1;
delayus();
SCL=1;
delayus();
SCL=0;
delayus();
}
void IIC_WriteOneByte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCL=0;
delayus();
if(dat&0x80)
{
SDA=1;
}
else
{
SDA=0;
}
dat=dat<<1;
SCL=1;
delayus();
}
SCL=0;
delayus();
SDA=1;
delayus();
}
uchar IIC_ReadOneByte(void)
{
uchar i,dat;
SCL=0;
delayus();
SDA=1;
delayus();
for(i=0;i<8;i++)
{
SCL=1;
delayus();
dat<<=1;
dat|=(uchar)SDA;
SCL=0;
delayus();
}
return dat;
}
void DAC(uchar dat)
{
IIC_Start();
IIC_WriteOneByte(0x90);
IIC_Ack();
IIC_WriteOneByte(0x40);
IIC_Ack();
IIC_WriteOneByte(dat);
IIC_Ack();
IIC_Stop();
}
uchar ReadADC(unsigned char Chl)
{
unsigned char Data;
IIC_Start(); //写入芯片地址
//Send(AddWr);
IIC_WriteOneByte(0x90);
IIC_Ack();
IIC_WriteOneByte(0x40|Chl);//写入选择的通道,本程序只用单端输入,差分部分需要自行添加
IIC_Ack();
IIC_Start();
IIC_WriteOneByte(0X91); //读入地址
IIC_Ack();
Data=IIC_ReadOneByte(); //读数据
SCL=0;
IIC_NoAck();
IIC_Stop();
return Data; //返回值
}
void main()
{
uchar temp,ad;
DAC(temp);
while(1)
{
ad=ReadADC(2);
display(ad);
}
}[/mw_shl_code]
|