资源简介
AD7745的驱动文件,使用C51编程,通过测试,可用
代码片段和文件信息
/*
****************************************************************************************
* Coder : Ashang
* MCU : STC89C51
* OSC : 12MHz||exterior
* Compiler : KEIL
* File Name: AD7745.c
* VER: 1.0
* Function:
****************************************************************************************
*/
#include
#include “Public.h“
volatile BYTE I2C_Flag=0;
DWORDUnion TempResult;
DWORDUnion CapResult;
#define ERR 0
#define NOACK 1
//AD7745Delay5 : Short delay (5 machine cycles incl CALL time) for the main signals(SCLOCK SDATA)
void AD7745Delay5(void)
{
BYTE i;
for( i=0; i<200 ; i++ );
}
//AD7745I2CStart : Sends the start bit to initiate an I2C communication
void AD7745I2CStart(void)
{
I2C_Flag = 0;
I2CDat = 1;
I2CSCL = 1;
I2CDat = 0; //low O/P on SDATA
AD7745Delay5(); //delay 5 Machine cycles
I2CSCL = 0; //start bit
}
//AD7745I2CStop : Sends the stop bit to end an I2C transmission
void AD7745I2CStop(void)
{
I2CDat = 0; //get SDATA ready for stop
I2CSCL = 1; //set clock for stop
AD7745Delay5();
I2CDat = 1; //this is the stop bit
}
//AD7745I2CSendByte : Send 8-bits in ch to the slave
void AD7745I2CSendByte(BYTE ch)
{
BYTE i;
I2CSCL = 0; //make sure that the clock line is low
for(i=0;i<8;i++)
{
I2CDat = 0; //put data bit on SDATA line
if(ch&0x80)
I2CDat = 1;
I2CSCL = 1; //clock to send bit
I2CSCL = 0; //clear clock
ch = ch<<1; //move ch‘s bit
}
I2CSCL = 1; //send clock for acknowledge
AD7745Delay5();
if(I2CDat) //no acknowledge set flag
I2C_Flag |= 1< I2CSCL = 0; //clear clock
}
//AD7745I2CReceiveByte : receives one byte of data from an I2C slave device.
//ak refers to ackonwledge bit
BYTE AD7745I2CReceiveByte(BYTE ak)
{
BYTE irr=0;
I2CSCL = 0; //make sure the clock line is low
for(i=0;i<8;i++)
{
I2CSCL = 1; //clock to recieve bit
rr = rr<<1; //read data bit
rr |= I2CDat;
I2CSCL = 0; //clear clock
}
I2CDat = ak; //Send (no) acknowledge bit
I2CSCL = 1;
AD7745Delay5();
I2CSCL = 0; //clear clock
return rr;
}
//AD7745Reset : Reset the CDC after send SLAVEADD send AD7745Reset_B(0xBF)
void AD7745Reset()
{
BYTE i;
AD7745I2CStart();
AD7745I2CSendByte(SLAVEADD);
if(!(I2C_Flag&(1< AD7745I2CSendByte(AD7745Reset_ADDR);
else
I2C_Flag |= 1< AD7745I2CStop();
for(i=0;i<254;i++); //wait for CDC reset
}
//AD7745WriteRegistor : write reg to CDC‘s addr register
void AD7745WriteRegistor(BYTE addr BYTE reg)
{
AD7745I2CStart();
AD7745I2CSendByte(SLAVEADD);
if(!(I2C_Flag&(1< AD7745I2CSendByte(addr);
if(!(I2C_Flag&(1< AD7745I2CSendByte(reg);
AD7745I2CStop();
if(I2C_Flag&(1< I2C_Flag |= 1< }
#if 0
//AD7745ReadRegistor : read CDC‘s addr register
BYTE AD7745ReadRegistor(BYTE add
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5345 2011-07-05 21:04 AD7745.c
文件 1284 2011-07-05 21:06 AD7745.h
----------- --------- ---------- ----- ----
6629 2
评论
共有 条评论