📄 i2c.h
字号:
#ifndef I2C_h
#define I2C_h
/******* I2C definitions: PORT, I/O PINs, R/W bits *******
*
* SCL - P2.1, SDA - P2.0
************************************************************/
/****************** Physical connections I2C pins on uC *****************************/
#define I2C_PORT_IN P3IN
#define I2C_PORT_OUT P3OUT
#define I2C_PORT_DIR P3DIR
// I2C clock
#define SCL BIT2 // 0x01 = pin.0 - used with I2C_PORT_xxx
// I2C data
#define SDA BIT1 // 0x02 = pin.1 - used with I2C_PORT_xxx
#define I2C_MASK ( SCL + SDA ) // SCL and SDA must be on same port
// determines the speed of the I2C bus
#define I2C_DELAY 0x80//uC_CLK*0xff//uC_CLK*0x0022 // larger delay => low bitrate
#define I2C_WAIT 0xFF // wait for I2C bus to free up - used in i2C_SendStart
#define SCL_MAX_WAIT_CYCLES 0xFFFF
// Wait cycle maximum time delay (dafault = 0xFFFF).
// Needed by the slave
// to release the SCL bus (process the data, etc.)
#define I2C_READ 0x01 // W/R bit -> W = 0, R = 1
// default ucDevAddr = write address !
#define I2C_LAST_BYTE 0x01
/**************************************************************/
// Status codes:
// static unsigned char ucI2C_Err;
#define I2C_OK 0x00 // 0x00 - no error
#define I2C_BUSY 0x01 // 0x01 - I2C Bus is busy
#define I2C_NACK 0x02 // 0x02 - NACK
#define I2C_SCL_LOW 0x03 // 0x03 - SCL line is stuck low -
// SCL_MAX_WAIT_CYCLES time-out
/**************************** Masking Interrupts using I2C **************************/
// Masking is only needed if the interrupt must use I2C
#define I2C_MASK_IE_PORT P2IE // P1IE
#define I2C_IRQ_MASK 0x00 // use (nRF_IRQ_PIN | xxxx | yyyy) to mask additional
// IRQs on this port
// 0x00 -> no IRQ masking
/******************************* Public Variables **********************************/
extern unsigned char ucI2C_Err;
/******************************* Public functions **********************************/
void vInitI2C ( void ); // sets SCL and SDA to 1
// Status code is cleared when sending I2C Start signal
// when using Read/Write functions
void I2C_DelayWriteByte ( unsigned char ucDevAddr,
unsigned char ucDataAddr,
unsigned char ucData,
unsigned int uiDelay );
// Delay,
// Sends 1 Byte to external device
// Sets Status code
void vI2C_DelayWrite4Bytes (unsigned char ucDevAddr,
unsigned char ucDataAddr,
unsigned long ulData,
unsigned int uiDelay );
// Delay,
// Sends to external device unsigned long (4 bytes)
// Sets Status code
void I2C_DelayWriteByteNum ( unsigned char ucDevAddr,
unsigned char ucDataAddr,
unsigned char const *pucData,
unsigned char ucNumBytes,
unsigned int uiDelay );
// Delay,
// Sends to external device ucNumBytes bytes
// taken from address - pucData (an array)
// Sets Status code
unsigned char I2C_ReadByte ( unsigned char ucDevAddr,
unsigned char ucDataAddr);
// Assigns Byte that was read from external device
// Sets Status code
// Returns data byte
#define mIS_I2C_PINs_HIGH (( I2C_PORT_IN & I2C_MASK) == I2C_MASK )
// to use with if() ... check pin status
/************************ HELP *************************************
Typical implementation of I2C protocol
Write Bytes:
...
unsigned char ucAddrX = 0x68; // write address of device X = address + WRITE
unsigned char ucDataAddr = 0x34; // data address
unsigned char ucData; // data
unsigned int uiWriteDelay = 0x003F; // delay for write
...
I2C_Init();
...
ucData = 0x45;
ucErr = I2C_DelayWriteByte ( ucAddrX, ucDataAddr, ucData, uiWriteDelay );
...
Read Bytes:
...
ucErr = I2C_ReadByte ( ucAddrX, ucDataAddr, &ucData );
...
********************************************************************/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -