/*
* Copyright (c) 2008,张涛
* All rights reserved.
*
* 文件名称:ads1100.h
* 文件标识:无
* 摘 要:16位高精度I2C模数
*
* 当前版本:1.0
* 作 者:张涛
* 完成日期:2009年04月01日
*
* 取代版本:
* 原 作者:
* 完成日期:
*/
#ifndef ADS1100_H_H_H
#define ADS1100_H_H_H
#include "myC51Define.h"
sbit SDA = P1^6;
sbit SCL = P1^5;
uchar HIGH_DATA = 0; //高8位
uchar LOW_DATA = 0; //低8位
//延时
void DelayMs (uint number);
//起始信号
void Start();
//终止信号
void Stop();
//发送应答
void Ack();
//发送非应答
void NoAck();
//应答位检查子程序
bit TestAck();
//写一个字节
bit Write8Bit(uchar input);
//读一个字节
uchar Read8Bit();
void Delay_10_uS() //11.0592
{
uchar temp;
for ( temp = 44; temp != 0 ; temp-- )
{
}
}
//延时
void DelayMs (ushort number)
{
uchar temp;
for(;number!=0;number--)
{
for(temp = 224 ; temp!= 0; temp--)
{
}
}
}
//起始信号
void Start()
{
SDA = 1;
Delay_10_uS();
SCL = 1;
Delay_10_uS();
SDA = 0;
Delay_10_uS();
SCL = 0;
Delay_10_uS();
}
//终止信号
void Stop()
{
SCL = 0;
Delay_10_uS();
SDA = 0;
Delay_10_uS();
SCL = 1;
Delay_10_uS();
SDA = 1;
Delay_10_uS();
}
//发送应答
void Ack()
{
SDA = 0;
Delay_10_uS();
SCL = 1;
Delay_10_uS();
SCL = 0;
Delay_10_uS();
SDA = 1;
Delay_10_uS();
}
//发送非应答
void NoAck()
{
SDA = 1;
Delay_10_uS();
SCL = 1;
Delay_10_uS();
SCL = 0;
Delay_10_uS();
}
//应答位检查子程序
bit TestAck()
{
bit ErrorBit;
SDA = 1;
Delay_10_uS();
SCL = 1;
Delay_10_uS();
ErrorBit = SDA;
DelayMs(1);
SCL = 0;
Delay_10_uS();
return ErrorBit;
}
//写一个字节
bit Write8Bit(uchar input)
{
uchar temp;
for(temp = 8 ; temp!=0; temp--)
{
SDA = (bit)(input&0x80);
Delay_10_uS();
SCL = 1;
Delay_10_uS();
SCL = 0;
Delay_10_uS();
input = input << 1;
}
return 1;
}
//读一个字节
uchar Read8Bit()
{
uchar temp,rbyte=0;
for( temp = 8 ; temp!=0; temp--)
{
SCL = 1;
Delay_10_uS();
rbyte = rbyte<<1;
Delay_10_uS();
rbyte = rbyte | ((uchar)(SDA)) ;
SCL = 0;
Delay_10_uS();
}
return rbyte;
}
//写器件地址和配置字
void WriteTo(uchar number)
{
Start();
Write8Bit(0x90); //写地址
TestAck();
Write8Bit(number);
TestAck();
Stop();
Delay_10_uS();
}
//读数据
void ReadTo()
{
Start();
Write8Bit(0x91); //读地址
TestAck();
HIGH_DATA = Read8Bit();
TestAck();
LOW_DATA = Read8Bit();
NoAck();
Stop();
Delay_10_uS();
}
#endif