新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > #單片機(jī)#利用pcf8591讀取電位器電壓值

#單片機(jī)#利用pcf8591讀取電位器電壓值

作者: 時(shí)間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
#include
sbit sda=P2^0;
sbit scl=P2^1;
#define uint unsigned int
#define uchar unsigned char
uchar code table[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0x40};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的顯示碼
//--定義PCF8591的讀寫地址--//
#define WRITEADDR 0x90 //寫地址
#define READADDR 0x91 //讀地址
void delayms(uint z)
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay()
{ ;; }
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //應(yīng)答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i ;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i )
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=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;
scl=0;
delay();
}
return k;
}
uchar ADC_PCF8591_rd_(uchar channal)
{
uchar date;
start();
write_byte(0x90);
respons();
write_byte(channal);
respons();
start();
write_byte(0x91);
respons();
date=read_byte();
stop();
return date;
}
void ADC_PCF8591_wt_(uchar date)
{
start();
write_byte(0x90);
respons();
write_byte(0x40);
respons();
write_byte(date);
respons();
stop();
}
//注:主函數(shù)中進(jìn)行AD或者DA的選擇,進(jìn)行處理
void display(uchar i)
{
float a; int j;
a=i;
a=a*500/256;
j=a;
P2=0xfd;
P0=0xff;
P0=~table[j/100]&0x7f;
delayms(5);
P2=0xfb;
P0=0xff;
P0=~table[j0/10];
delayms(5);
P2=0xf7;
P0=0xff;
P0=~table[j];
delayms(5);
}
void main()
{
uint temp;
sda=1;
delay();
scl=1;
delay();
while(1)
{
temp=ADC_PCF8591_rd_(0x00);
display(temp);
}
}


評論


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

關(guān)閉