clear sky . 2024-07-24 10:09 采纳率: 23.1%
浏览 1
已结题

EEPROM,软件i2c

main.c:


#include "stm32f4xx.h"                  // Device header
#include "serial.h"
#include "delay.h"
#include "OLED.h"
#include "eeprom.h"



uint8_t currentAddre=0;

uint8_t readData=0;

uint8_t testvalue=0xf0;

int main(void)
{
    Serial_Init();
    EEPROM_Init();
    printf("123success\r\n");  
    
    printf("testvalue=%x\r\n",testvalue);
    
    currentAddre=Read_CurrentAddre();
    printf("currentAddre:%x\r\n",currentAddre);
    
    Write_ByteData(0xff,0x31);
    readData=Read_ByteData(0xff); 

    printf("readData=%x\r\n",readData);   

  while (1)
  {
   
  }
}

eeprom.c:

#include "stm32f4xx.h"                  // Device header
#include "i2c.h"
#include "delay.h"


#define DeviceAddress 0xA0


/*读取当前地址*/
uint8_t Read_CurrentAddre(){
    uint8_t address=0xff;
    i2c_start();
    i2c_WriteData(DeviceAddress|0x01);
    Ack_Receive();
    
    address=i2c_ReadData();
    Ack_Give(1);
    i2c_Stop();
    
    return address;
}

void Write_ByteData(uint8_t wordAddress,uint8_t Data){
    i2c_start();
    i2c_WriteData(DeviceAddress);//地址写0xA0
    Ack_Receive();
    i2c_WriteData(wordAddress);
    Ack_Receive();
    i2c_WriteData(Data);
    Ack_Receive();
    i2c_Stop();
    Delay_ms(1000);//等待写入完成
}

/*任意地址读*/
uint8_t Read_ByteData(uint8_t wordAddress){
    uint8_t readData=0;
    i2c_start();
    i2c_WriteData(DeviceAddress);//写地址
    Ack_Receive();
    i2c_WriteData((uint8_t)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*/
    SCL_GPIO_Init();
    /*SDA*/
    SDA_GPIO_Init();
    /*A0,A1,A2*/

    /*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);
}

i2c.c:

#include "stm32f4xx.h"                  // Device header
#include "delay.h"




void SDA(uint8_t BitValue){
    GPIO_WriteBit(GPIOF,GPIO_Pin_0,(BitAction)BitValue);
    Delay_us(10);
}

void SCL(uint8_t BitValue){
    GPIO_WriteBit(GPIOF,GPIO_Pin_1,(BitAction)BitValue);
    Delay_us(10);
}


void SCL_GPIO_Init(){
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
    GPIO_InitTypeDef SCL_InitStructure;
    SCL_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
    SCL_InitStructure.GPIO_OType=GPIO_OType_OD;
    SCL_InitStructure.GPIO_Pin=GPIO_Pin_1;
    SCL_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    SCL_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOF,&SCL_InitStructure);
    
}

void SDA_GPIO_Init(){
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
    GPIO_InitTypeDef SDA_InitStructure;
    SDA_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
    SDA_InitStructure.GPIO_OType=GPIO_OType_OD;
    SDA_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    SDA_InitStructure.GPIO_Pin=GPIO_Pin_0;
    SDA_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
    GPIO_Init(GPIOF,&SDA_InitStructure);

}


void i2c_start(){
    SCL(1);
    SDA(1);
    Delay_us(10);
    SDA(0);
    Delay_us(10);   
    SCL(0);
    Delay_us(10);
}

void i2c_Stop(){
    SCL(0);
    SDA(0);    
    Delay_us(10);
    SCL(1);
    Delay_us(10);
    SDA(1);
    Delay_us(10);
    
}

/*stm32接收Ack应答*/
void Ack_Receive(){
    SCL(0);
    Delay_us(10);
    
    SDA(1);//SDA等待从机设置
    Delay_us(10);
        
    SCL(1);
    Delay_us(10);
    
    SCL(0);
    Delay_us(10);
        
}

/*stm32给出Ack应答*/
void Ack_Give(uint8_t value){
    SCL(0);
    Delay_us(10);
    if(value==0){
        SDA(0);
        Delay_us(10);
    }
    else{
        SDA(1);
        Delay_us(10);
    }    
    SCL(1);
    Delay_us(10);
    SCL(0);

}

void i2c_WriteData(uint8_t Data){
    uint8_t finger=0x80;
    SCL(0);
    Delay_us(10);        
    for(uint8_t k=0;k<8;k++){
        if(Data&finger){
            SDA(1);
            Delay_us(10); 
        }
        else{
            SDA(0);
            Delay_us(10); 
        }       
        SCL(1);//读走SDA电平
        Delay_us(10);
        SCL(0);
        Delay_us(10);
        finger=finger>>1;
    }    
       
}

uint8_t i2c_ReadData(){
    SCL(0);
    Delay_us(10);
    SDA(1);
    Delay_us(10);
    uint8_t k=0;
    uint8_t ReadData=0x00;
    uint8_t finger=0x80;
    for(k=0;k<8;k++){
        SCL(1);
        Delay_us(10);
        if(GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_0)==1){
            ReadData=ReadData|(finger>>k);            
        }
        else{
        }
        Delay_us(10);
        SCL(0);
        Delay_us(10);
        //finger=finger>>1;        
    }
    return ReadData;
}

img


请问为什么读取数据全是f,代码哪里有问题?

  • 写回答

33条回答 默认 最新

  • 阿里嘎多学长 2024-07-24 10:09
    关注
    获得0.15元问题酬金

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


    根据您提供的代码和描述,读取数据全是f的问题可能由以下几个方面引起:

    1. I2C通信问题:如果I2C通信没有正确建立,可能会导致数据读取不正确。检查i2c.c中的I2C通信函数是否正确实现。

    2. EEPROM设备地址:确保DeviceAddress宏定义正确,并且与实际连接的EEPROM芯片地址相匹配。

    3. 读写操作实现Write_ByteDataRead_ByteData函数的实现需要确保正确地发送和接收数据。

    4. 格式化字符串错误:在printf函数调用中,格式化字符串存在错误,如"readData=%xlrln"应更正为"readData=%x\r\n"

    5. 初始化和延迟:确保EEPROM_Init函数正确初始化了所需的GPIO,并检查Delay_ms(1000)是否足以确保EEPROM写入操作完成。

    6. 硬件连接:检查EEPROM芯片是否正确连接到STM32的I2C引脚上,包括电源和地线。

    7. EEPROM芯片问题:如果EEPROM芯片损坏或存在问题,也可能导致读取数据不正确。

    针对您提供的代码,以下是一些具体的修改建议:

    • 修正Read_ByteData函数中的读取数据行,使用i2c_ReadData()而不是Read_CurrentAddre()

      uint8_t readData = i2c_ReadData(); // 替换原来的 Read_CurrentAddre() 调用
      
    • 确保i2c_ReadData()函数中的finger变量在每次读取位后正确右移:

      for(k=0;k<8;k++){
        ...
        finger=finger>>1; // 确保这行代码没有被注释掉
      }
      
    • 修正main.c中的testvalue赋值错误:

      testvalue = 0xffffffff; // 假设您想设置的值是十六进制的 FFFF FFFF
      
    • 修正printf函数中的格式化字符串错误:

      printf("readData=%x\r\n", readData); // 修正为正确的 \r\n
      
    • 检查EEPROM_Init函数中的GPIO初始化代码是否正确,确保没有遗漏或错误。

    • 如果可能,使用逻辑分析仪检查I2C总线上的信号,确保通信正常。

    • 如果问题仍然存在,考虑检查EEPROM设备或更换设备进行测试。

    请注意,这些建议是基于代码审查和用户提供的信息。实际问题可能需要结合硬件测试和调试来确定。如果问题依然无法解决,可能需要进一步的硬件检查或考虑EEPROM芯片是否损坏。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 8月1日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 一道ban了很多东西的pyjail题
  • ¥15 关于#r语言#的问题:如何将生成的四幅图排在一起,且对变量的赋值进行更改,让组合的图漂亮、美观@(相关搜索:森林图)
  • ¥15 C++识别堆叠物体异常
  • ¥15 微软硬件驱动认证账号申请
  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?