/*用单片机控制AD(PCF8951)进行模数转换,当拧动实验板上A/D旁边的电位器RE2时,在数码管的前三位以十进制方式动态显示出A/D转换后的数字量(8位A/D转换后数值在0~255变化)*/#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitsda=P2^0;sbitscl=P2^1;uchara;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voiddelay1(ucharx)//延时函数{uchara,b;for(a=x;a0;a--)for(b=100;b0;b--);}voiddelay(){;;}voiddisplay(ucharge,ucharshi,ucharbai)//数码管显示{P0=table[bai];P1=0xef;delay1(1);P0=table[shi];P1=0xdf;delay1(1);P0=table[ge];P1=0xbf;delay1(1);}voidstart()//开始信号{sda=1;delay();scl=1;delay();sda=0;delay();}voidstop()//停止{sda=0;delay();scl=1;delay();sda=1;delay();}voidrespons()//应答{uchari;scl=1;delay();while((sda==1)&&(i250))i++;scl=0;delay();}voidinit(){sda=1;delay();scl=1;delay();TMOD=0x21;//定时器1的工作方式2;8位初值自动重装定时器SCON=0x50;//8位数据允许串行接受TH1=0xfd;//产生波特率TL1=0xfd;EA=1;TR0=1;}voidwrite_byte(uchardate)//写一个字节{uchari,temp;temp=date;for(i=0;i8;i++){temp=temp1;scl=0;delay();sda=CY;delay();scl=1;delay();}scl=0;delay();sda=1;delay();}ucharread_byte()//读一个字节{uchari,k;scl=0;delay();sda=1;delay();for(i=0;i8;i++){scl=1;delay();k=(k1)|sda;scl=0;delay();}returnk;}voidad_zhuan()//初始化AD转换{start();//启动信号write_byte(0x90);//寻pcf8591址respons();write_byte(0x40);respons();stop();}ucharread_ad()//读AD转换{uchartemp;start();//启动信号write_byte(0x91);//寻pcf8591址respons();temp=read_byte();//读一个字节respons();stop();returntemp;}voidmain(){uchara,A1,A2,A3,adval;init();//初始化//write_add(23,0xaa);向EEPROM写delay1(100);//P1=read_add(23);读EEPROM中数据while(1){for(a=10;a0;a--){display(A1,A2,A3);}//display();adval=read_ad();A1=adval/100;//分出百,十,个A2=adval%100/10;A3=adval%10;}}