to:huntington 出现写入的数据读出会不一样,但并没有出现错误. 没有错误是指器件的应答正常. 另外程序读写字节里面需要关中断吗?我发现似乎关不关都一样. 我帖上程序看看... #defineAT24C256_I2cWRITE0xA0// 定义24CXX的器件地址SLA和方向位W #defineAT24C256_I2cREAD0xA1//定义24CXX的器件地址SLA和方向位R #defineI2CEPR_DELAY_nop_();_nop_(); _nop_(); _nop_();_nop_();_nop_(); _nop_(); #defineSET_SDAEPR_HIGHepr_SDA = 1//io_T_SDA = 1 /* set SDA to high */ #defineSET_SDAEPR_LOWepr_SDA = 0//io_T_SDA = 0 /* set SDA to low */ #defineSET_SCLEPR_HIGHepr_SCL = 1//io_T_SCL = 1 /* set SCL to high */ #defineSET_SCLEPR_LOWepr_SCL = 0//io_T_SCL = 0 /* set SCL to low */ #defineSET_SDAEPR_TO_INPUTepr_SDA = 1//io_T_SDA = 1 #defineSET_SDAEPR_TO_OUTPUT #defineGET_SDAEPR_VALUEepr_SDA;//io_T_SDA static void_hwi2c_I2cSendStart(void); static void_hwi2c_I2cSendStop(void); static void_hwi2c_I2cSendNAck(void); static u1_t_u1_hwi2c_I2cGetAck(void); static u1_t_u1_hwi2c_I2cSendByte(u1_t value); static void_hwi2c_I2cSendAck(void); static u1_t_u1_hwi2c_I2cGetByte(void); static u1_t_u1_hwi2c_I2cGetLastByte(void); /* ----------------------------------------------------------------------------- Function name : _hwrtc_I2cSendStart Purpose :i2c bus write/read start program Input parameters :none Output parameters : none ----------------------------------------------------------------------------- */ static void _hwi2c_I2cSendStart(void) { SET_SDAEPR_HIGH;//发送起始条件的数据信号 SET_SCLEPR_HIGH;/* set SDA,SCL to high */ I2CEPR_DELAY;//起始条件建立时间大于4.7us延时 SET_SDAEPR_LOW;//发送起始信号/* set SDA to low */ I2CEPR_DELAY;//起始条件锁定时间大于4us SET_SCLEPR_LOW;//钳住总线,准备发送或接收数据/* set SCL to low */ I2CEPR_DELAY; } /* ----------------------------------------------------------------------------- Function name : _hwrtc_I2cSendStop Purpose :i2c bus write/read stop program Input parameters :none Output parameters : none ----------------------------------------------------------------------------- */ static void _hwi2c_I2cSendStop(void) { SET_SDAEPR_LOW;/* set SDA to low */ I2CEPR_DELAY ; SET_SCLEPR_HIGH;/* set SCL to high */ I2CEPR_DELAY ; SET_SDAEPR_HIGH;/* set SDA to high */ I2CEPR_DELAY ; SET_SCLEPR_LOW; } /* ----------------------------------------------------------------------------- Function name : _hwrtc_I2cSendNAck Purpose :Send NACK Signal Input parameters :none Output parameters : none ----------------------------------------------------------------------------- */ static void _hwi2c_I2cSendNAck(void) { SET_SDAEPR_HIGH;/* set SDA to high */ I2CEPR_DELAY ; SET_SCLEPR_HIGH;/* set SCL to high */ I2CEPR_DELAY ; SET_SCLEPR_LOW;/* set SCL to low */ I2CEPR_DELAY ; } /* ----------------------------------------------------------------------------- Function name : _u1_hwrtc_I2cGetAck Purpose :Get ACK Signal Input parameters :none Output parameters : ACK Signal ----------------------------------------------------------------------------- */ static u1_t _u1_hwi2c_I2cGetAck(void) { u1_t __temp = TRUE, __i = 0xFF; //bool_t __b_ea ; //__b_ea = _testbit_(EA) ; SET_SDAEPR_HIGH;/* set SDA to high,Release SDA for ACK */ I2CEPR_DELAY ; SET_SCLEPR_HIGH;/* set SCL to high,raise ACK clock */ while((--__i) && (__temp)) { __temp = GET_SDAEPR_VALUE; } SET_SCLEPR_LOW;/* set SCL to low */ I2CEPR_DELAY ; //EA = __b_ea ; if (__temp)/* if success return 0x01,else return 0x00 */ { return FALSE; } else { return TRUE; } } /* ----------------------------------------------------------------------------- Function name : _u1_hwrtc_I2cSendByte Purpose :Send one byte to I2C interface Input parameters :value-the Byte that will be sent Output parameters : TRUE or FALSE ----------------------------------------------------------------------------- */ static u1_t _u1_hwi2c_I2cSendByte(u1_t value) { u1_t__i, __temp; //bool_t__b_ea ; //__b_ea = _testbit_(EA) ; for (__i = 0; __i < BITSOFBYTE; __i++) { __temp = value; __temp >>= 7 - __i; __temp &= 0x01; if (__temp == 0) { SET_SDAEPR_LOW; }/* set SDA to low */ else { SET_SDAEPR_HIGH; }/* set SDA to high */ I2CEPR_DELAY ; SET_SCLEPR_HIGH;/* set SCL to high */ I2CEPR_DELAY ; SET_SCLEPR_LOW;/* set SCL to low */ I2CEPR_DELAY ; } //EA = __b_ea ; return(_u1_hwi2c_I2cGetAck());/* if sucess return 0x01,else return 0x00 */ } /* ----------------------------------------------------------------------------- Function name : _hwrtc_I2cSendAck Purpose :Send ACK Signal Input parameters :none Output parameters : none ----------------------------------------------------------------------------- */ static void _hwi2c_I2cSendAck(void) { SET_SDAEPR_LOW; I2CEPR_DELAY ; SET_SCLEPR_HIGH; I2CEPR_DELAY ; SET_SCLEPR_LOW; I2CEPR_DELAY ; } /* ----------------------------------------------------------------------------- Function name : _u1_hwrtc_I2cGetByte Purpose :Get one byte fromI2C interface Input parameters :none Output parameters : the byte from I2C interface ----------------------------------------------------------------------------- */ static u1_t _u1_hwi2c_I2cGetByte(void) { u1_t__i, __temp, __temp1; //bool_t__b_ea ; //__b_ea = _testbit_(EA) ; __temp = 0; SET_SDAEPR_TO_INPUT; for (__i = 0; __i < BITSOFBYTE; __i++) { __temp <<= 1; SET_SCLEPR_HIGH; I2CEPR_DELAY ; __temp1 = GET_SDAEPR_VALUE; __temp |= __temp1; SET_SCLEPR_LOW; I2CEPR_DELAY ; } SET_SDAEPR_TO_OUTPUT; _hwi2c_I2cSendAck(); //EA = __b_ea ; return(__temp); } /* ----------------------------------------------------------------------------- Function name : _u1_hwrtc_I2cGetLastByte Purpose :Get the last byte form I2C interface Input parameters :none Output parameters : the last byte from I2C interface ----------------------------------------------------------------------------- */ static u1_t _u1_hwi2c_I2cGetLastByte(void) { u1_t__i, __temp, __temp1; //bool_t__b_ea ; //__b_ea = _testbit_(EA) ; __temp = 0; SET_SDAEPR_TO_INPUT; for (__i = 0; __i < BITSOFBYTE; __i++) { __temp <<= 1; SET_SCLEPR_HIGH; I2CEPR_DELAY ; __temp1 = GET_SDAEPR_VALUE; __temp |= __temp1; SET_SCLEPR_LOW; I2CEPR_DELAY ; } //SET_SDAEPR_TO_OUTPUT;edit by whsno define _hwi2c_I2cSendNAck(); //EA = __b_ea ; return(__temp); } /*===================================================================================== 函数: u1_hwi2cepr_I2ceprReadBlock 功能: eeprom连续写操作 参数: address:读数据的地址;pdat:读出数据存储地址;size:写入长度 返回: true:成功;false:失败 备注: 创建: whs-07-11-23 修改: ========================================================================================*/ u1_t u1_hwi2cepr_I2ceprReadBlock(u2_t address, u1_t* pdat, u1_t size) { u1_t__i; u1_t__status; _hwi2c_I2cSendStart(); __status = _u1_hwi2c_I2cSendByte(AT24C256_I2cWRITE); if (__status == TRUE) { __status = _u1_hwi2c_I2cSendByte((u1_t)(address>>BITSOFBYTE));/*send the high byte first*/ } if (__status == TRUE) { __status = _u1_hwi2c_I2cSendByte((u1_t)address); } if (__status == TRUE) { _hwi2c_I2cSendStart(); __status = _u1_hwi2c_I2cSendByte(AT24C256_I2cREAD); } if (__status == TRUE) { for (__i=0; __i<size-1; __i++) { pdat[__i] = _u1_hwi2c_I2cGetByte(); } pdat[__i] = _u1_hwi2c_I2cGetLastByte(); } _hwi2c_I2cSendStop(); return (__status); } /*===================================================================================== 函数: u1_hwi2cepr_I2ceprWriteBlock 功能: eeprom连续写操作 参数: address:写入地址;pdat:写入的数据;size:写入长度 返回: true:成功;false:失败 备注: 创建: whs-07-11-23 修改: ========================================================================================*/ u1_t u1_hwi2cepr_I2ceprWriteBlock(u2_t address, u1_t* pdat, u1_t size) { u1_t__i; u1_t__status; //bool_tb_ea ; //b_ea = _testbit_(EA) ; _hwi2c_I2cSendStart(); __status = _u1_hwi2c_I2cSendByte(AT24C256_I2cWRITE); if (__status == TRUE) { __status = _u1_hwi2c_I2cSendByte((u1_t)(address>>BITSOFBYTE));/*send the high byte first*/ } if (__status == TRUE) { __status = _u1_hwi2c_I2cSendByte((u1_t)address); } if (__status == TRUE) { for (__i=0; __i<size; __i++) { __status = _u1_hwi2c_I2cSendByte( *(pdat+__i) ); if (__status == FALSE) { break; } } } _hwi2c_I2cSendStop(); //EA = b_ea; return (__status); } /*===================================================================================== 函数: hwi2cepr_I2ceprInit 功能: 初始化 参数: 返回: 备注: 创建: whs-07-11-23 修改: ========================================================================================*/ void hwi2cepr_I2ceprInit(void) { u1_t __i ; SET_SDAEPR_HIGH ; for(__i = 0; __i < 9; __i++) { SET_SCLEPR_HIGH ; I2CEPR_DELAY; SET_SCLEPR_LOW ; I2CEPR_DELAY; } _hwi2c_I2cSendStop(); }
作者:alphal 2008-1-27 18:10:00 |