clear sky . 2024-07-21 21:20 采纳率: 23.1%
浏览 4

stm32f407 EEOPROM读写


#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数据的写入

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-07-21 21:20
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    从您提供的代码和描述来看,有几个可能的问题点,这些可能会导致EEPROM数据写入失败:

    1. GPIO初始化问题

      • EEPROM_Init函数中,您设置了GPIO Pin 0到4为输出模式,但是通常A0, A1, A2是用于选择EEPROM芯片的地址线,应该设置为上拉或下拉,而不是推挽输出。请检查您的EEPROM芯片的地址线配置。
    2. I2C通信问题

      • 确保i2c_WriteDatai2c_ReadData函数能够正确发送和接收数据。如果这些函数没有正确实现,可能会导致写入失败。
      • 检查Ack_ReceiveAck_Give函数是否正确处理I2C的应答信号。如果应答信号处理不当,EEPROM可能不会接收到数据。
    3. 写入延时问题

      • Delay_ms(10)可能不足以让EEPROM完成写入操作。不同的EEPROM芯片写入时间可能不同,需要根据数据手册调整延时。
    4. EEPROM地址问题

      • Write_ByteDataRead_ByteData函数中,您使用了DeviceAddress作为写入和读取的地址。确保这个地址是EEPROM的I2C地址,并且wordAddress参数是EEPROM内部的内存地址。
    5. 读取数据时的I2C重启动问题

      • Read_ByteData函数中,您在发送完地址后立即发送了一个重启动信号来读取数据。这可能不是所有EEPROM都支持的操作。通常,您应该在发送完地址后直接进入接收模式。
    6. 代码逻辑问题

      • Read_ByteData函数中,您调用了Read_CurrentAddre来读取数据,但是这个函数似乎是用于读取当前EEPROM的地址指针,而不是读取数据。您可能需要修改这个函数或者直接使用i2c_ReadData来读取数据。
    7. 硬件连接问题

      • 确保EEPROM与STM32F407之间的硬件连接正确,包括电源、地线、数据线和时钟线。
    8. 调试和测试

      • 使用调试工具,比如串口打印,来检查每一步操作的结果。这可以帮助您确定问题发生的具体位置。

    建议您根据上述可能的问题点逐一检查和调试您的代码和硬件设置。如果问题仍然存在,您可能需要查看EEPROM的数据手册来获取更详细的操作信息,或者使用逻辑分析仪来监控I2C总线上的通信情况。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月21日

悬赏问题

  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件