您的位置 首页 知识

#单片机#使用pcf8591读取电位器电压值

includereg51h>sbitsda=P2^0;sbitscl=P2^1;defineuintunsignedintdefineucharunsignedcharucharcodetabl

#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() //应对
{
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();
}
//注:主函数中进行AD或许DA的挑选,进行处理
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);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/256868.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部