资源描述:
《DA输出控制LED亮度》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#defineucharunsignedchar#defineuintunsignedintsbitsda=P2^0;sbitscl=P2^1;sbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3F,//"0"0x06,//"1"0x5B,//"2"0x4F,//"3"0x66,//"4"0x6D,//"5"0x7D,//"6"0x07,//"7"0x7F,//"8"0x6F,//"9"0x77,//"A"0x7C,/
2、/"B"0x39,//"C"0x5E,//"D"0x79,//"E"0x71,//"F"};voiddelay()//5us{;;}voiddelay1(uintz)//1ms{uintx,y;for(x=z;x>0;x--)for(y=120;y>0;y--);}voiddisplay(ucharvalue){ucharbai,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
3、;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);}voidstart(){scl=1;_nop_();sda=1;delay();sda=0;delay();scl=0;_nop_();}voidstop(){scl=1;_nop_();sda=0;delay();sda=1;de
4、lay();}voidrespons(){scl=1;_nop_();sda=0;delay();scl=0;_nop_();}voidnorespons(){scl=1;_nop_();sda=1;delay();}voidinit(){scl=1;_nop_();sda=1;_nop_();}voidwrite_byte(uchardat){uchari,temp;temp=dat;for(i=0;i<8;i++){scl=0;_nop_();if((temp&0x80)==0x80)sda=1;//д1elsesda=0;//д
5、0scl=1;_nop_();temp<<=1;}scl=0;_nop_();}ucharread_byte(){uchardat,i;sda=1;_nop_();scl=0;_nop_();for(i=0;i<8;i++){scl=1;_nop_();if(sda)dat
6、=0x01;if(i<7)dat<<=1;scl=0;_nop_();}returndat;}voidDAC_write(uchardat){start();write_byte(0x90);respons();write_byte(0x40);respons()
7、;write_byte(dat);respons();stop();}ucharADC_read(ucharcom){uchardat;dat=com;start();write_byte(0x90);respons();write_byte(com);respons();start();write_byte(0x91);respons();dat=read_byte();norespons();stop();returndat;}voidmain(){uchari=0,ad_value;init();while(1){//ad_va
8、lue=ADC_read(0x42);display(i);DAC_write(i);i++;delay1(20);}}