今天测试了一下AT24C02.
代码:
/*************<include>****************/
#include "i2c.h"
#include "gpio.h"
#include "timer.h"
/*************<macro>******************/
/*************<enum>*******************/
/*************<union>******************/
/*************<struct>*****************/
/*************<variable>***************/
/*************<prototype>**************/
/**
* [url=home.php?mod=space&uid=555622]@prototype[/url] main(void)
*
* @param[in] void
* @return void
*
* [url=home.php?mod=space&uid=247401]@brief[/url] main entry.
* main函数.
*/
int main(void)
{
InitDelay();
InitDebug();
LED_Init();
KEY_Init();
TIM_InitHw();
I2C_InitHw();
while(1)
{
KEY_ScanInput();
}
}
/*************<end>********************/
void KEY_ScanInput(void)
{
if (g_scanKeyTime >= SCAN_KEY_INTVL)
{
g_scanKeyTime = 0;
/*上次**KEY6状态为释放且当前KEY6按下,进行二次确认并重新**KEY6状态.*/
if ((g_getKey6Sts == KEY_RELEASE) && (GET_KEY6_STS() == KEY_PRESS))
{
mdelay(10);
if (GET_KEY6_STS() == KEY_PRESS)
{
g_getKey6Sts = KEY_PRESS;
/*写入数据.*/
I2C_WrDataToAT24C();
}
}
/*上次**KEY6为按下且当前KEY6释放,更新**KEY6状态,以保证每次按键动作有效性.*/
else if ((g_getKey6Sts == KEY_PRESS) && (GET_KEY6_STS() == KEY_RELEASE))
{
g_getKey6Sts = KEY_RELEASE;
}
/*上次**KEY7状态为释放且当前KEY7按下,进行二次确认并重新**KEY7状态.*/
if ((g_getKey7Sts == KEY_RELEASE) && (GET_KEY7_STS() == KEY_PRESS))
{
mdelay(10);
if (GET_KEY7_STS() == KEY_PRESS)
{
g_getKey7Sts = KEY_PRESS;
/*读取数据.*/
I2C_RdDataFromAT24C();
}
}
/*上次**KEY7为按下且当前KEY7释放,更新**KEY7状态,以保证每次按键动作有效性.*/
else if ((g_getKey7Sts == KEY_PRESS) && (GET_KEY7_STS() == KEY_RELEASE))
{
g_getKey7Sts = KEY_RELEASE;
}
}
}
/**
* @prototype I2C_WrDataToAT24C(void)
*
* @param[in] void
* @return void
*
* @brief 写数据到EEPROM.
*/
void I2C_WrDataToAT24C(void)
{
g_wrAT24CDataBuf[0] = 0x00;//写入地址
for (uint8_t ii = 0; ii < 8; ii++)
{
g_wrAT24CDataBuf[ii + 1] = ii;
printf("%d ", g_wrAT24CDataBuf[ii + 1]);
}printf("\r\n");
I2C_MasterBurstWrite(I2C0, AT24C02_DEV_ADDR, g_wrAT24CDataBuf, 9, ENABLE);
}
/**
* @prototype I2C_RdDataFromAT24C(void)
*
* @param[in] void
* @return void
*
* @brief 从EEPROM读取数据.
*/
void I2C_RdDataFromAT24C(void)
{
g_wrAT24CDataBuf[0] = 0x00;//读取地址
I2C_MasterBurstWrite(I2C0, AT24C02_DEV_ADDR, g_wrAT24CDataBuf, 1, DISABLE);
I2C_MasterBurstRead (I2C0, AT24C02_DEV_ADDR, g_rdAT24CDataBuf, 8);
for (uint8_t ii = 0; ii < 8; ii++)
{
printf("%d ", g_rdAT24CDataBuf[ii]);
}printf("\r\n");
}
效果图:
|