新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 使用PCF8591進(jìn)行ADDA轉(zhuǎn)換

使用PCF8591進(jìn)行ADDA轉(zhuǎn)換

作者: 時(shí)間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
1.電路圖


本文引用地址:http://butianyuan.cn/article/201611/322104.htm

2.AD轉(zhuǎn)換

//AD轉(zhuǎn)換,P0口連接J12(數(shù)碼管)
//功能:通過轉(zhuǎn)動(dòng)滑動(dòng)變阻器改變模擬輸入
//通過AD轉(zhuǎn)換,把數(shù)值用數(shù)碼管顯示
#include 
#define uchar unsigned char
#define uint unsigned int
#define PCF8591 0x90 //PCF8591 地址
bit write=0;  //寫24c02的標(biāo)志
sbit SCL=P2^1;        //串行時(shí)鐘輸入端
sbit SDA=P2^0;  //串行數(shù)據(jù)輸入端
sbit LS138A=P2^2;//138譯碼器的3位 控制數(shù)碼管的 
sbit LS138B=P2^3;
sbit LS138C=P2^4;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};      //數(shù)顯管字模
void delay()   //延時(shí)4-5個(gè)微秒
{;;}
void delay_1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--)
;
}
void start()//開始信號(hào)
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
}
void stop()      //停止信號(hào)
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
void respons()//應(yīng)答 相當(dāng)于一個(gè)智能的延時(shí)函數(shù)
{
uchar i;
SCL=1;
delay();
while((SDA==1)&&(i<250))
i++;
SCL=0;
delay();
}
void init()      //初始化
{
SDA=1;
delay();
SCL=1;
delay();    
}
uchar read_byte()
{
uchar i,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
k=(k<<1)|SDA;//先左移一位,再在最低位接受當(dāng)前位
SCL=0;
delay();
}
return k;
}
void write_byte(uchar date)       //寫一字節(jié)數(shù)據(jù)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1; //左移一位 移出的一位在CY中
SCL=0;                      //只有在scl=0時(shí)sda能變化值
delay();
SDA=CY;
delay();
SCL=1;
delay();            
}   
SCL=0;
delay();
SDA=1;
delay();
}
void write_add(uchar control,uchar date)
{
start();
write_byte(PCF8591);        //10010000 前四位固定 接下來三位全部被接地了 所以都是0 最后一位是寫 所以為低電平
respons();
write_byte(control);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar control)
{
uchar date;
start();
write_byte(PCF8591);
respons();
write_byte(control);
respons();
start();
write_byte(PCF8591+1);      //把最后一位變成1,讀
respons();
date=read_byte();
stop();
return date;
}				   
				
            
                
			
							
上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: PCF8591ADDA轉(zhuǎn)

評(píng)論


技術(shù)專區(qū)

關(guān)閉