📄 eeprom.c
字号:
//#define _I2C_H
#include "71x_lib.h"
#include "eeprom.h"
void Init_I2C0()
{
//Configure I2C0
GPIO_Config (GPIO1, I2C0_SCL_Pin| I2C0_SDA_Pin, GPIO_AF_OD); // Alternation Open Drian setting
I2C_Init(I2C0); // register initial
I2C_FCLKConfig(I2C0);
I2C_OnOffConfig(I2C0, ENABLE); // I2C Interface Enable
I2C_SpeedConfig(I2C0,I2C_Speed); // 100KHz(Standard I2C Mode)
I2C_AcknowledgeConfig(I2C0, ENABLE); //Acknowledge Enable
}
void EEP_Write (u16 MemAddress, u8 Data)
{
u16 i;
Init_I2C0();
// Start Bit Generation
I2C_STARTGenerate (I2C0, ENABLE); // start bit generation
while (I2C_FlagStatus (I2C0,DIRECT,I2C_SB)==RESET); //Start bit flag check(1:generation)
//send the eeprom address
I2C_AddressSend(I2C0,EEP_ADDR,I2C_Mode7,I2C_TX); //7bit addressing mode, write enable
while (I2C_FlagStatus (I2C0,DIRECT,I2C_ENDAD )==RESET);
I2C_FlagClear(I2C0, I2C_ENDAD);
//send byte address
I2C_ByteSend(I2C0, (u8)MemAddress);
// Wait until BTF bit is set
while (I2C_FlagStatus (I2C0, DIRECT, I2C_BTF )==RESET);
//send the String to be written in the eeprom
I2C_ByteSend(I2C0, Data);
// Wait till I2C_BTF bit is set
while (!(I2C0->SR1 & 0x08));
//Generate stop condition
I2C_STOPGenerate(I2C0, ENABLE);
// Wait until the stop bit is sent
while(I2C0->CR&0x2);
for (i=0; i<0xfff; i++);
//Disable the I2C0
I2C_OnOffConfig (I2C0, DISABLE);
Delay_ms(10); //DataSheet 24LC02 : Write Time = 5ms
}
/********************************************************************************/
/* Byte Read Function */
/********************************************************************************/
u8 EEP_Read(u16 MemAddress)
{
u8 Data;
Init_I2C0();
//Enable Start generation
I2C_STARTGenerate (I2C0, ENABLE);
// Wait until start condition is generated
while (I2C_FlagStatus (I2C0,DIRECT,I2C_SB )==RESET);
//Send the eeprom address with LSB bit reset
// I2C_AddressSend (I2C0,(M24C08_Address|(MemAddress>>7)),I2C_Mode7,I2C_TX);
I2C_AddressSend (I2C0,EEP_ADDR,I2C_Mode7,I2C_TX);
//Wait end of address transmission
while (I2C_FlagStatus (I2C0,DIRECT,I2C_ENDAD )==RESET);
I2C_FlagClear (I2C0, I2C_ENDAD);
//Send address of byte
I2C_ByteSend (I2C0,(u8)MemAddress);
//RE-START generate
I2C_STARTGenerate (I2C0, ENABLE);
while (I2C_FlagStatus (I2C0,DIRECT,I2C_SB )==RESET);
//Send address with the LSB bit set
// I2C_AddressSend (I2C0,(M24C08_Address|(MemAddress>>7)),I2C_Mode7,I2C_RX);
I2C_AddressSend (I2C0,EEP_ADDR,I2C_Mode7,I2C_RX);
//Test on the end of address transmission
while (I2C_FlagStatus (I2C0,DIRECT,I2C_ENDAD )==RESET);
I2C_FlagClear (I2C0, I2C_ENDAD);
I2C_AcknowledgeConfig (I2C0, DISABLE);
//Receive data
Data= I2C_ByteReceive (I2C0);
//Generate stop condition
I2C_STOPGenerate (I2C0, ENABLE);
// Wait until the stop bit is sent
// while(I2C0->CR&0x2);
// for (i=0; i<0xff; i++);
I2C_OnOffConfig (I2C0, DISABLE);
return Data;
}
/********************************************************************************/
/* Delay_ms Initial */
/********************************************************************************/
void Delay_ms(u16 time)
{
u16 i,j;
for (i=0; i<time; i++)
for (j=0; j<4000; j++);
}
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -