本帖最后由 gujufeng 于 2018-5-14 22:27 编辑
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
};
void delay() //5us
{;;}
void delay1(uint z)//1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
void display(uchar value)
{
uchar bai,shi,ge;
bai=value/100;
shi=value%100/10;
ge=value%100%10;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay1(1);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay1(1);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay1(1);
}
void start()
{
scl=1;
_nop_();
sda=1;
delay();
sda=0;
delay();
scl=0;
_nop_();
}
void stop()
{
scl=1;
_nop_();
sda=0;
delay();
sda=1;
delay();
}
void respons()
{
scl=1;
_nop_();
sda=0;
delay();
scl=0;
_nop_();
}
void norespons()
{
scl=1;
_nop_();
sda=1;
delay();
}
void init()
{
scl=1;
_nop_();
sda=1;
_nop_();
}
void write_byte(uchar dat)
{
uchar i,temp;
temp=dat;
for(i=0;i<8;i++)
{
scl=0;
_nop_();
if((temp&0x80)==0x80)
sda=1; //д1
else
sda=0; //д0
scl=1;
_nop_();
temp<<=1;
}
scl=0;
_nop_();
}
uchar read_byte()
{
uchar dat,i;
sda=1;
_nop_();
scl=0;
_nop_();
for(i=0;i<8;i++)
{
scl=1;
_nop_();
if(sda)
dat|=0x01;
if(i<7)
dat<<=1;
scl=0;
_nop_();
}
return dat;
}
void DAC_write(uchar dat)
{
start();
write_byte(0x90);
respons();
write_byte(0x40);
respons();
write_byte(dat);
respons();
stop();
}
uchar ADC_read(uchar com)
{
uchar dat;
dat=com;
start();
write_byte(0x90);
respons();
write_byte(com);
respons();
start();
write_byte(0x91);
respons();
dat=read_byte();
norespons();
stop();
return dat;
}
void main()
{
uchar i=0,ad_value;
init();
while(1)
{
ad_value=ADC_read(0x42);
display(ad_value);
/*DAC_write(i);
i++; */
delay1(1);
}
}
|