📄 i2c.c
字号:
//***************************************************************
// I2C functions for ST72321 - master mode only
// Function : Delay10us
// Description : delay 10us each time is called
// can be modified for different CPU speed
// **************************************************************
#include "reg52.h"
#include "..\..\inc\globedef.h"
#include "..\..\inc\i2c.h"
#include "..\..\inc\delay.h"
//***************************************************************
// Send start condition
// ensure data is high then issue a start condition
// see also i2c_Start() macro
//***************************************************************
void I2C_Start (void)
{
Pin_I2CSDA = 1;
Delay5US();
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
Delay5US();
Pin_I2CSDA = 0;
Delay5US();
Delay5US();
Delay5US();
Pin_I2CSCL = 0;
}
//***************************************************************
// Send stop condition
// data low-high while clock high
//***************************************************************
void I2C_Stop (void)
{
Pin_I2CSDA = 0;
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
Delay5US();
Pin_I2CSDA = 1;
}
//***************************************************************
//ROUTINE NAME : I2C_Ack
//INPUT/OUTPUT : None.
//DESCRIPTION : Acknoledge generation from now.
//COMMENTS : Transfer sequence = DATA, ACK.
//***************************************************************
void I2C_Rack(void)
{
Pin_I2CSCL = 0;
Pin_I2CSDA = 1;
Delay5US();
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
Delay5US();
Delay5US();
Pin_I2CSDA = 0;
Pin_I2CSCL = 0;
Delay5US();
}
/*
void I2C_Ack (void)
{
Pin_I2CSCL = 0;
Delay5US();
Pin_I2CSDA = 0;
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
Pin_I2CSCL = 0;
Delay5US();
Pin_I2CSDA = 1;
}
*/
//***************************************************************
//ROUTINE NAME : I2C_nAck
//INPUT/OUTPUT : None.
//DESCRIPTION : Non acknoledge generation from now.
//COMMENTS : Transfer sequence = DATA, NACK.
//***************************************************************
void I2C_nAck (void)
{
Pin_I2CSCL = 0;
Delay5US();
Pin_I2CSDA = 1;
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
Delay5US();
Delay5US();
Pin_I2CSCL = 0;
Delay5US();
Pin_I2CSDA = 1;
}
//***************************************************************
// Send a byte to the slave
// return I2C_ERROR on error
//***************************************************************
void I2C_TxData (BYTE i2c_data)
{
BYTE send_i;
for(send_i=0; send_i<8; send_i++)
{
Pin_I2CSCL = 0;
Delay5US();
if(i2c_data & 0x80)
Pin_I2CSDA = 1;
else
Pin_I2CSDA = 0;
Delay5US();
Pin_I2CSCL = 1;
Delay5US();
i2c_data <<= 1;
}
}
//***************************************************************
//ROUTINE NAME : I2Cm_RxData
//INPUT/OUTPUT : Last byte to receive flag (active high)/Received data byte.
//DESCRIPTION : Receive a data byte.
//COMMENTS : Transfer sequence = DATA, ACK, EV7...
//***************************************************************
BYTE I2C_RxData (void)
{
BYTE read_i;
BYTE readbyte=0;
Pin_I2CSCL = 0;
Pin_I2CSDA = 1;
Delay5US();
for(read_i=0; read_i<8; read_i++)
{
Pin_I2CSCL = 1;
readbyte <<= 1;
Delay5US();
if(Pin_I2CSDA)
readbyte |= 0x01;
Pin_I2CSCL = 0;
Delay5US();
}
return readbyte;
}
//***************************************************************
//ROUTINE NAME : I2Cm_SetAddr
//INPUT/OUTPUT : external I2C device address / None.
//DESCRIPTION : Generates Start-bit and transmits the address byte.
//COMMENTS : Transfer sequence = START, EV5, ADD, ACK...
//***************************************************************
void I2C_SetAddr(char i2c_addr)
{
I2C_TxData (i2c_addr);
}
//***************************************************************
// Send a byte to the slave and acknowledges the transfer
// return I2C_ERROR, or I2C_OK
//***************************************************************
void I2C_PutByte(BYTE DevAddr,BYTE subAddr, BYTE ucData)
{
I2C_Start();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr);
I2C_Rack();
I2C_TxData(ucData);
I2C_Rack();
I2C_Stop();
}
//***************************************************************
// Get a byte from the slave and acknowledges the transfer
// return I2C_OK or I2C_ERROR
//***************************************************************
BYTE I2C_GetByte(BYTE DevAddr,BYTE subAddr)
{
BYTE ucData;
I2C_Start ();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr);
I2C_Rack();
I2C_Start ();
I2C_SetAddr(DevAddr+1);
I2C_Rack();
ucData = I2C_RxData();
I2C_nAck();
I2C_Stop();
return(ucData);
}
//***************************************************************
// Send an array of bytes to the slave
// -and acknowledges the transfer
// return I2C_OK or I2C_ERROR
//***************************************************************
/*
void I2C_PutString(BYTE DevAddr,BYTE subAddr, BYTE *str, BYTE length)
{
BYTE send_cc;
send_cc = 0;
I2C_Start ();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr);
I2C_Rack();
while(length)
{
I2C_TxData(*str);
I2C_Rack();
length--;
send_cc++;
if(length > 0)
str++;
if((send_cc % 8 == 0) && (length > 0))
{
I2C_Stop();
DelayX1ms(4);
{
I2C_Start ();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr+send_cc);
I2C_Rack();
}
}
}
I2C_Stop();
}
*/
/*
void I2C_OutString(BYTE DevAddr,BYTE subAddr, BYTE *str, BYTE length)
{
BYTE CCC;
I2C_Start ();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr);
I2C_Rack();
while(length)
{
I2C_TxData(*str);
I2C_Rack();
length--;
if(length > 0)
str++;
}
I2C_Stop();
for(CCC = 0; CCC <2;CCC++)
;
}
*/
//***************************************************************
// Reads number bytes from the slave, stores them at str
// -and acknowledges the transfer
// return I2C_OK or I2C_ERROR not successfully read
//***************************************************************
/*
void I2C_GetString(BYTE DevAddr,BYTE subAddr, BYTE *str, BYTE number)
{
I2C_Start ();
I2C_SetAddr(DevAddr);
I2C_Rack();
I2C_SetAddr(subAddr);
I2C_Rack();
I2C_Start ();
I2C_SetAddr(DevAddr+1);
I2C_Rack();
while(number)
{
*str = I2C_RxData();
if (number==1)
I2C_nAck();
else
I2C_Ack();
str++;
number -= 1;
}
I2C_Stop();
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -