Need help on writing and reading characters on EEPROM

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
LORDHADES
Posts: 24
Joined: Thu Nov 19, 2020 4:53 am

Need help on writing and reading characters on EEPROM

Post by LORDHADES »

Hey all !!!

I hope y'all are doing well... I'm really sorry, but I need help on a problem with reading/writing character/string on STM32's emulated EEPROM.

I'm using STM32F103C6/C8 with 128 kb internal flash.

I used this tutorial for preliminary setup : "https://www.instructables.com/Getting-S ... duino-IDE/"
Arduino IDE: v1.8.13(Windows Store 1.8.42.0) from Microsoft store
Board Manager: STM32F1XX/GD32F1XX by stm32duino v2020.11.14
Additional board manager URL: "http://dan.drown.org/stm32duino/package ... index.json"

I am trying to :

1). Display a command selection with two options, like one shown on example EEPROM program.
2). If command 1 is selected, user can save a word or short sentence in EEPROM.
3). If command 2 is selected, either existing or current data on EEPROM can be displayed on serial monitor.

The problem is I'm still a student learning programming, and I can't find a proper way to receive characters, write to EEPROM and read from EEPROM. I found examples to write and read numbers, but can't find anything for characters... :(

I wrote a dummy code based on several examples around web, and it fails to do what it is intended to do.

The code is:

Code: Select all

#include <EEPROM.h>
#define ledPin PC13

uint16 DataWrite = 0;
uint16 AddressWrite = 0;
uint16 address = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  char coName_[50];
  uint16 Data;

  while (Serial.available())
  {
    char cmd = (char)Serial.read();
    
    if (cmd == '0')
    {
     Serial.print("Enter Name:");
     coName_ = Serial.read();
     int size_ = sizeof(coName_);
     for (int j = 0; j<size; j++)
     {
      EEPROM.write(j, coName[j]);
     }
    }
 
  uint16 Status;
  uint16 Data;

  while (Serial.available())
  {
    char cmd = (char)Serial.read();
    Serial.println(cmd);

    if(cmd == '0')
    {
      Serial.println("Enter something");
      inByte = Serial.read();
      EEPROM.write(addr, inByte);
      addr = addr + 1;

      if (addr == EEPROM.length())
      {
        addr = 0;
      }
    delay(100);
    }
    
    else if (cmd == '1')
    {
      inByte_1 = EEPROM.read(address);
      Serial.print(address);
      Serial.print('\t');
      Serial.print(inByte_1, DEC);
      Serial.println();

      address = address + 1;

      if (address == EEPROM.length())
      {
        address = 0;
      }
    delay(1000);
    }

    else if (cmd == '2')
    {
      Status = EEPROM.write(AddressWrite, DataWrite);
      Serial.print("EEPROM.write(0x");
      Serial.print(AddressWrite, HEX);
      Serial.print(", 0x");
      Serial.print(DataWrite, HEX);
      Serial.print(") : Status : ");
      Serial.println(Status, HEX);
    }

    else

    Serial.print(welMsg);
  }

  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}


Kindly guide me in this concern. Even a little help or suggestion will be so valuable to me. Thanks in advance for gracious people out there. Forgive me if the code has too errors. I uploaded amidst I was debugging.
Post Reply

Return to “General discussion”