⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ads1110.c

📁 高精度ADS1110的驱动程序,I2C接口,自带参考源
💻 C
字号:
 /*
#include <reg52.h>


#include <intrins.h>
#define uchar unsigned char
//*/

sbit SDA_ads1110=P2^2;
sbit SCL_ads1110=P2^3;
 

/********************************************
内部函数,延时1
********************************************/
void Delay_ADS1110()
{
   // {P10=1;_nop_();P10=0;}
    _nop_();
   _nop_(); /*根据晶振频率制定延时时间*/
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();


}
/********************************************
内部函数,I2C开始
********************************************/
void Start_ADS1110()
{  //EA=0;
   SDA_ads1110=1;
   SCL_ads1110=1;
   Delay_ADS1110();
   SDA_ads1110=0;
   Delay_ADS1110();
   SCL_ads1110=0;
   Delay_ADS1110();
}
/********************************************
内部函数,I2C结束
********************************************/
void Stop_ADS1110()
{
   SDA_ads1110=0;
   SCL_ads1110=0;
   Delay_ADS1110();
   SCL_ads1110=1;
   Delay_ADS1110();
   SDA_ads1110=1;
   Delay_ADS1110();
  // EA=1;
}
/********************************************
内部函数,输出ACK ,每个字节传输完成,输出ack=0,结束读书据,ack=1;
********************************************/

void WriteACK_ADS1110(uint8 ack)
{
   SDA_ads1110=ack;
   Delay_ADS1110();
   SCL_ads1110=1;
   Delay_ADS1110();
   SCL_ads1110=0;
   Delay_ADS1110();
}
/********************************************
内部函数,等待ACK
********************************************/
void WaitACK_ADS1110()
{  uint8 errtime=20;
   SDA_ads1110=1;
   Delay_ADS1110(); /*读ACK*/
   SCL_ads1110=1;
   Delay_ADS1110();
   while(SDA_ads1110)
   {  errtime--;
      if(!errtime) Stop_ADS1110();
   }
   SCL_ads1110=0;
   Delay_ADS1110();
}
/********************************************
内部函数.输出数据字节
入口:B=数据
********************************************/
void writebyte_ADS1110(uint8 wdata)
{
   uint8 i;
   for(i=0;i<8;i++)
   {
       if(wdata&0x80) SDA_ads1110=1;
       else SDA_ads1110=0;
       wdata<<=1;
       SCL_ads1110=1;
       Delay_ADS1110();
       SCL_ads1110=0;
   }
   WaitACK_ADS1110();     //I2C器件或通讯出错,将会退出I2C通讯
}
/********************************************
内部函数.输入数据
出口:B
********************************************/
uint8 Readbyte_ADS1110()
{
   uint8 i,bytedata;
   SDA_ads1110=1;
   for(i=0;i<8;i++)
   {
      SCL_ads1110=1;
      bytedata<<=1;
      bytedata|=SDA_ads1110;
      SCL_ads1110=0;
      Delay_ADS1110();
   }
   return(bytedata);
}
/********************************************
初始化ads1100----------外部调用
********************************************/
void wr_ads1100(uint8 add,uint8 mdata)
{ Start_ADS1110();
  writebyte_ADS1110(0x90|((add&0x07)<<1));
  writebyte_ADS1110(mdata);
  Stop_ADS1110();
}
/********************************************
读数据----------------外部调用
********************************************/
int16 Rd_ads1100(uint8 add) /*多字节*/
{ int16 temp;
   //temp=&buff1;
   Start_ADS1110();
   writebyte_ADS1110(0x91|((add&0x07)<<1)); /*写命令*/
   temp=Readbyte_ADS1110();temp=temp<<8;
   WriteACK_ADS1110(0);
   temp=temp|Readbyte_ADS1110();
   WriteACK_ADS1110(1);
   Stop_ADS1110();
  return(temp);
}
/*
main()
{Rd_ads1100();
for(;;);
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -