#include "stm32f4xx.h" // Device header
#include "serial.h"
#include "delay.h"
#include "OLED.h"
#include "eeprom.h"
uint8_t currentAddre=1;
uint8_t readData=0;
static int buffer[99]={0};
int main(void)
{
Serial_Init();
EEPROM_Init();
printf("123success\r\n");
currentAddre=Read_CurrentAddre();
printf("currentAddre:%d\r\n",currentAddre);
Write_ByteData(0x00,3);
currentAddre=Read_CurrentAddre();
printf("currentAddre:%d\r\n",currentAddre);
readData=Read_ByteData(0x00);
printf("readData=%d\r\n",readData);
while (1)
{
}
}
#include "stm32f4xx.h" // Device header
#include "i2c.h"
#include "delay.h"
#define DeviceAddress 0xA0
/*读取当前地址*/
uint8_t Read_CurrentAddre(){
uint8_t address=0;
i2c_start();
i2c_WriteData(DeviceAddress|0x01);
Ack_Receive();
address=i2c_ReadData();
return address;
}
void Write_ByteData(uint8_t wordAddress,uint8_t Data){
i2c_start();
i2c_WriteData(DeviceAddress);//地址写
Ack_Receive();
i2c_WriteData(wordAddress);
Ack_Receive();
i2c_WriteData(Data);
Ack_Receive();
i2c_Stop();
Delay_ms(10);//等待写入完成
}
/*任意地址读*/
uint8_t Read_ByteData(uint8_t wordAddress){
uint8_t readData=0;
i2c_start();
i2c_WriteData(DeviceAddress);//写地址
Ack_Receive();
i2c_WriteData(wordAddress);
Ack_Receive();
i2c_start();
i2c_WriteData(DeviceAddress&0x01);
Ack_Receive();
readData=Read_CurrentAddre();
Ack_Give(1);
i2c_Stop();
return readData;
}
void EEPROM_Init(){
/*SCL*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;//进一步分析
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1; //SCL
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/*SDA*/
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//SDA
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOF,&GPIO_InitStructure);
/*A0,A1,A2*/
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //A0,A1,A2
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
/*A0,A1,A2*/
GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4);
/*VCC*/
GPIO_SetBits(GPIOD,GPIO_Pin_3);
GPIO_SetBits(GPIOF,GPIO_Pin_0|GPIO_Pin_1);
}
上述代码为什么不能实现EEPROM数据的写入