📄 at24c02.c
字号:
// read last byte *(Byte*)bufPtr++ = At24c02_ReadByte( ); // cycle scl for no ack I2C_SetClkTriStateEnableBit(); Delay(AT_DELAY); I2C_ClearClkBit(); Delay(AT_DELAY); // signal stop At24c02_StopCode(); } // end else I2C_ConfigurePort(FALSE); ProtectionEnd(); DbgPrint("--- Exiting EEPROM_Read ---\n"); return status;} // end EEPROM_Read//================= LOCAL FUNCTIONS =====================// || ||// || ||// || ||// --- --- --- ---// \ / \ /// \ / \ /// \ / \ /// \/ \/////-----------------------------------------------------------------------------// Function:static Boolean At24c02_Check_Ack()//// Description:// This operation checks for the ACK from the AT24C02 device.//// Parmeters:*///// Returns:// 0 = NACK; 0 < ACK//----------------------------------------------------------------------------{ Boolean ack; // set tri-state to read I2C_ClearDataTriStateEnableBit(); // read Delay(AT_DELAY); I2C_SetClkTriStateEnableBit(); // write I2C_SetClkBit(); // clock = 1 Delay(AT_DELAY); // read the ACK ack = !I2C_GetDataBit(); I2C_ClearClkBit(); // clock = 0 Delay(AT_DELAY);// bugzilla 3380 - for single core systems, must yield. #ifdef GECKO OS_TaskYield();#endif return ack;} // end At24c02_Check_Ack//-----------------------------------------------------------------------------// Function:static void At24c02_StartCode()//// Description:// This operation send a Start signal to the AT24C02.//// Parmeters:*///// Returns:// n/a//----------------------------------------------------------------------------{ I2C_SetClkTriStateEnableBit(); // write I2C_SetDataTriStateEnableBit(); // write I2C_SetDataBit(); // data = 1 I2C_SetClkBit(); // clock = 1 Delay(AT_DELAY); I2C_ClearDataBit(); // data = 0 Delay(AT_DELAY); I2C_ClearClkBit(); // clock = 0 Delay(AT_DELAY);} // end At24c02_StartCode//-----------------------------------------------------------------------------// Function:static void At24c02_StopCode()//// Description:// This operation send a Stop signal to the AT24C02.//// Parmeters:*///// Returns:// n/a//----------------------------------------------------------------------------{ I2C_SetClkTriStateEnableBit(); // write I2C_SetDataTriStateEnableBit(); // write I2C_ClearDataBit(); // data = 0 Delay(AT_DELAY); I2C_SetClkBit(); // clock = 1 Delay(AT_DELAY); I2C_SetDataBit(); // data = 1 Delay(AT_DELAY*600); // Write cycle time delay} // end At24c02_StopCode//-----------------------------------------------------------------------------// Function:static void At24c02_SendByte(//// Description:// This operation write the given byte out to the AT24C02.//// Parmeters:*/ Byte value // value to be sent)//// Returns:// n/a//----------------------------------------------------------------------------{ int i; DbgPrint("writing...(%0.2x): ",value); I2C_SetClkTriStateEnableBit(); // write I2C_SetDataTriStateEnableBit(); // write Delay(AT_DELAY); for( i=7; i>=0; i-- ) { if( (value >> i) & 0x1 ) { I2C_SetDataBit(); // data = 1 } // end if else { I2C_ClearDataBit(); // data = 0 } // end else Delay(AT_DELAY); I2C_SetClkBit(); // clock = 1 Delay(AT_DELAY); I2C_ClearClkBit(); // clock = 0 Delay(AT_DELAY); } // end for DbgPrint("\n");} // end At24c02_SendByte//-----------------------------------------------------------------------------// Function:static Byte At24c02_ReadByte( )//// Description:// This operation will read a byte from the AT24C02. The SDA tri-state// will both return in the read state.//// Parmeters:*///// Returns:// the byte value read from the AT24C02////----------------------------------------------------------------------------{ Uint8 bit; Byte data = 0; I2C_ClearDataTriStateEnableBit(); // read I2C_SetClkTriStateEnableBit(); // write Delay(AT_DELAY); for ( bit = 0; bit < 8; bit++ ) { I2C_SetClkBit(); // clock = 1 Delay(AT_DELAY); data <<= 1; if ( I2C_GetDataBit() == TRUE ) { data++; } // end if // clock in data I2C_ClearClkBit(); // clock = 0 Delay(AT_DELAY); } // end for DbgPrint("At24c02: read %0.2x\n",data); return data;} // end At24c02_ReadByte//-----------------------------------------------------------------------------// Function:static int At24c02_WritePage(//// Description:// This operation performs an 8 bit (byte) write to the AT24C02 eeprom.//// Parmeters:*/ Uint32 addr, // address to be written to Byte *data_ptr // pointer to first data byte to write)//// Returns:// SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{ Uint8 i; ProtectionStart(); DbgPrint("At24c02_WritePage @ addr= %02.x, mode: %02x\n", addr, I2C_GetModeRegister()); At24c02_StartCode(); At24c02_SendByte( AT_DEVICE_ADDR ); AT_WAIT_ACK(); At24c02_SendByte( addr ); // send address AT_WAIT_ACK(); for (i=0;i<AT_PAGESIZE;i++) { At24c02_SendByte(*(data_ptr++)); AT_WAIT_ACK(); } At24c02_StopCode(); ProtectionEnd(); return SUCCESS;}//-----------------------------------------------------------------------------// Function:static void ProtectionStart(//// Description:// This function grabs the I2C semaphore if this is the first time// it is called. This function also increments the lock count.//// Notes://// Parameters: void)//// Returns:////-----------------------------------------------------------------------------{ if ( m_lockCount == 0 ) { // get the semaphore I2C_ProtectionStart(); } m_lockCount++;}//-----------------------------------------------------------------------------// Function:static void ProtectionEnd(//// Description:// This function decrements the lock count. If the lock count is equal to// zero, this function releases the I2C semaphore.//// Notes://// Parameters: void)//// Returns:////-----------------------------------------------------------------------------{ if ( m_lockCount > 0 ) { m_lockCount--; if ( m_lockCount == 0 ) { // free the semaphore I2C_ProtectionEnd(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -