#include "main.h"
#ifndef __I2C_H #define __I2C_H
#define I2CBAUD 800000//800KHz 本程序是读写铁电的,故速度很高
//下面的是I2C的标准状态表,因为PIC的不标准,故凑合着用和补充了加注解的部分~~~ enum enum_I2CState { /* Master */ I2C_START = 0x08,//启动 I2C_REP_START= 0x10,//重启动 /* Master Transmitter */ I2C_MT_SLA_ACK= 0x18,//主发模式下得到从机地址应答信号 I2C_MT_SLA_NACK= 0x20,
I2C_MT_DATA_ACK= 0x28,//主发模式下得到从机数据应答信号 I2C_MT_DATA_NACK= 0x30,
I2C_MT_ARB_LOST= 0x38, /* Master Receiver */ I2C_MR_ARB_LOST= 0x38, I2C_MR_SLA_ACK= 0x40,//主收模式下得到从机地址应答信号 I2C_MR_SLA_NACK= 0x48, I2C_MR_DATA_ACK= 0x50, I2C_MR_DATA_NACK= 0x58,
/* Slave Transmitter */ I2C_ST_SLA_ACK= 0xA8, I2C_ST_ARB_LOST_SLA_ACK= 0xB0, I2C_ST_DATA_ACK= 0xB8, I2C_ST_DATA_NACK= 0xC0, I2C_ST_LAST_DATA= 0xC8, /* Slave Receiver */ I2C_SR_SLA_ACK= 0x60, I2C_SR_ARB_LOST_SLA_ACK= 0x68, I2C_SR_GCALL_ACK= 0x70, I2C_SR_ARB_LOST_GCALL_ACK= 0x78, I2C_SR_DATA_ACK= 0x80, I2C_SR_DATA_NACK= 0x88, I2C_SR_GCALL_DATA_ACK= 0x90, I2C_SR_GCALL_DATA_NACK= 0x98, I2C_SR_STOP= 0xA0, /* Misc */ I2C_NO_INFO= 0xF8,
I2C_MT_ADDRH_ACK= 0x3a,//主发模式下得到从机高位EEPROM地址应答信号 I2C_MT_ADDRL_ACK= 0x3b,//主发模式下得到从机高低位EEPROM地址应答信号 I2C_MR_DATA= 0x5a,//主收模式下接收数据 I2C_MR_DATA_EN= 0x5b,//主收模式下使能接收数据功能 I2C_MR_DATA_STOP= 0x5c,//主收模式下接收数据完成
I2C_FAILED= 0xcc,//读写操作失败 I2C_SUCCEEDED= 0x00,//读写操作成功 I2C_RWBIT= 0,// I2C_READ= 1,// I2C_WRITE= 0// };
#define TRIS_SCL1 _TRISG2 //SCL方向控制 #define TRIS_SDA1 _TRISG3 //SDA方向控制 #define ODC_SCL1 _ODG2 //SCL开漏控制 #define ODC_SDA1 _ODG3 //SDA开漏控制
/*----------------------------------------- 写保护管脚定义,需要硬件的支持 ------------------------------------------*/ #define TRIS_WP _TRISD0 //WP方向控制 #define WP _LATD0//写保护
typedef struct tagI2CBITS { union { unsigned char I2CFlag; struct { unsigned char BusyFlag: 1;//忙标志 unsigned char ReadFlag: 1;//读回调标志 unsigned char WriteFlag: 1;//写回调标志 }; }; }I2CBITS;
extern volatile I2CBITS I2CBits;
typedef struct tagI2CREGS { unsigned char State;//运行状态编码 unsigned char I2CAddr;//器件地址0xa1,0xa0 unsigned int RWAddr;//器件读写地址(自动根据MaxCount判断长短地址) unsigned int Count;//运行计数器 unsigned int TxCount;//发送(写)个数 unsigned int RxCount;//接收(读)个数 unsigned int MaxCount;//器件最大容量 unsigned char TxBuffer[16];//发送缓冲区 unsigned char RxBuffer[256];//接收缓冲区 }I2CREGS;
extern volatile I2CREGS I2CRegs;
void I2cInit(void); void I2CReadBuffers(unsigned int, unsigned int); void I2CReadByte(unsigned int); void I2CWriteBuffers(unsigned int, unsigned int); void I2CWriteByte(unsigned int, unsigned char); void I2CReadCallBack(void); void I2CWriteCallBack(void); void I2cStart(void); void I2cReStart(void); void I2cStop(void); void I2CExec(void);
#endif//__I2C_H |