標(biāo)題: pcf8591 AD轉(zhuǎn)換 [打印本頁]

作者: 淺憶,汐辰!    時間: 2017-11-25 14:17
標(biāo)題: pcf8591 AD轉(zhuǎn)換
#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);
}




作者: admin    時間: 2017-11-25 20:28
缺少頭文件




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1