找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1925|回复: 1
打印 上一主题 下一主题
收起左侧

pcf8591 AD转换

[复制链接]
跳转到指定楼层
楼主
ID:225076 发表于 2017-11-25 14:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h
#include<pcf8591.h>
#define uchar unsigned char
#define uint  unsigned int
sbit     SCL=P1^7;     
sbit     SDA=P1^2;     
void Delay_4us(void)
{
volatile uchar i=0;
i++,i++,i++,i++,i++,i++,i++,i++;
}
void I2c_Start(void)
{
SDA=1;
Delay_4us();
SCL=1;
Delay_4us();
SDA=0;
Delay_4us();
SCL=0;
Delay_4us();
}
void I2c_Stop(void)
{
SCL=0;
Delay_4us();
SDA=0;
Delay_4us();
SCL=1;
Delay_4us();
SDA=1;
Delay_4us();
}
unsigned char Rcv_Byte(void)
{
unsigned char i,rbyte=0;
for(i=8;i;i--)
{
  SCL=1;
  Delay_4us();
  rbyte=rbyte<<1;
  rbyte=rbyte|(SDA);
  SCL=0;
}
return rbyte;
}
{
unsigned char i;
for(i=8;i;i--)
{
  SDA=(bit)(dat&0x80);
  Delay_4us();
  SCL=1;
  Delay_4us();
  SCL=0;
  Delay_4us();
  dat=dat<<1;
}
}
void I2c_Ack(bit a)
{
SDA=a;
Delay_4us();
SCL=1;
Delay_4us();
SCL=0;
Delay_4us();
SDA=1;
Delay_4us();   
}bit Test_Ack(void)
{
bit ack;
SDA=1;
Delay_4us();
SCL=1;
Delay_4us();
ack=SDA;
Delay_4us();
SCL=0;
return ack;
}
bit DAC(unsigned char addr,unsigned char Val)
{
   I2c_Start();              
   Send_Byte(0x90|(addr<<1
   if(Test_Ack())return(0);
   Send_Byte(0x40);            
   if(Test_Ack())return(0);
   Send_Byte(Val);            
   if(Test_Ack())return(0);
   I2c_Stop();                 
}
unsigned char ADC(unsigned char addr,unsigned char channel)
{
unsigned char dat=0;
   EA=0;
   I2c_Start();           
   Send_Byte(0x90|(addr<<1)|0);
   if(Test_Ack())return(0);
   Send_Byte(channel);        
   if(Test_Ack())return(0);
   I2c_Stop();              
    Delay_4us();
   I2c_Start();           
   Send_Byte(0x90|(addr<<1)|1);   
   if(Test_Ack())return(0);
   dat=Rcv_Byte();                     
   I2c_Ack(1);                                    
   I2c_Stop();                        
   EA=1;
   return(dat);
}



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2017-11-25 20:28 | 只看该作者
缺少头文件
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表