0
\$\begingroup\$

Based on the program from this post 28C64 EEPROM Programming Using Arduino, I would like to write, using Arduino Mega, a lookup-table that contains one sinewave cycle, created in Excel. I would like this table to be written in address "1" in the EEPROM. I have encoded the table in hex, but I am not sure how to split it into proper code. I would also like to read the table with the Arduino Serial Plotter instead of Serial Printer. Thanks.

enter image description here

sinus wave in HEX:

0004080B 0F14161C 1E23282A 2F32373A 3F43464B 4E53575B 5F63676B 6F73777B 7E83868B 8E93979A 9FA2A7AB AEB3B6BB BEC3C6CB CED3D7DA DFE2E7EA EFF2F7FA

the code:

#define WE 52
#define OE 51
#define CE 53
#define NUM_ADDR 3

int DP[8] = {22, 23, 24, 25, 26, 27, 28, 29};
int AP[] = {37, 36, 35};
byte data[] = {0xB080400, 0x1C16140F, 0x2A28231E, 0x3A37322F, 0x4B46433F, 0x5B57534E, 0x6B67635F, 0x7B77736F, 0x8B86837E, 0x9A97938E, 0xABA7A29F, 0xBBB6B3AE, 0xCBC6C3BE, 0xDAD7D3CE, 0xEAE7E2DF, 0xFAF7F2EF}; /// Sinus Lookup-Table


//------------------------ Init ----------------------------

void setupDataBus(int mode) {
  for (int i = 0; i < 8; i++) {
    pinMode(DP[i], mode);
  }
}

void setupControlPins(int mode) {
  pinMode(WE, mode);
  pinMode(OE, mode);
  pinMode(CE, mode);
}

void setupAddressBus(int mode) {
  for (int i = 0; i < NUM_ADDR; i++) {
    pinMode(AP[i], mode);
  }
}

//------------------------ Write Byte ----------------------

void writeByte(int address, byte data) {
  setupDataBus(OUTPUT);
  setupAddressBus(OUTPUT);
  setupControlPins(OUTPUT);
  Serial.print("Write Byte: ");
  for (int b = 0; b < 7; b++) {
    digitalWrite(AP[b], bitRead(address, b));

  }
  Serial.print(address, HEX);
  Serial.print(", ");
  Serial.print(data, HEX);
  Serial.println();
  delay(1);
  // Enable write
  digitalWrite(OE, HIGH);
  delay(1);
  digitalWrite(CE, LOW);
  delay(1);
  delay(10);
  for (int b = 0; b < 8; b++) {
    digitalWrite(DP[b], bitRead(data, b));
  }
  unsigned long time_i, time_f, time;
  time_i = micros();
  // Start write
  digitalWrite(WE, LOW);
  // End write
  digitalWrite(WE, HIGH);
  time = micros();
  Serial.println(time_f - time_i, DEC);
  delay(1);
  digitalWrite(OE, LOW);
  delay(1);
  digitalWrite(CE, HIGH);
  delay(1);

}

//------------------------ Read Byte ----------------------

byte readByte(int address) {
  setupDataBus(INPUT);
  setupAddressBus(OUTPUT);
  setupControlPins(OUTPUT);
  byte val;
  digitalWrite(WE, HIGH);
  delay(1);
  digitalWrite(CE, HIGH);
  delay(1);
  digitalWrite(OE, HIGH);
  delay(1);
  Serial.print("Read Byte: ");
  for (int b = 0; b < 7; b++) {
    digitalWrite(AP[b], bitRead(address, b));
  }

  Serial.print(address, HEX); /// print Address
  Serial.print(", ");
  delay(1);
  digitalWrite(WE, HIGH);
  delay(1);
  digitalWrite(CE, LOW);
  delay(1); 
  digitalWrite(OE, LOW);
  delay(1);

  for (int b = 0; b < 8; b++) {
    bitWrite(val, b, digitalRead(DP[b]));
  }

  Serial.print(val, HEX); /// print Bit
  delay(1);
  digitalWrite(OE, HIGH);
  delay(1);
  digitalWrite(CE, HIGH);
  delay(1);
  digitalWrite(WE, HIGH);
  Serial.println();
  return val;
}

//------------------------ SETUP ----------------------

void setup() {
  setupDataBus(OUTPUT);
  setupAddressBus(OUTPUT);
  pinMode(WE, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(CE, OUTPUT);
  Serial.begin(9600);
}

//------------------------ LOOP -----------------------

void loop() {
  // init
  digitalWrite(OE, HIGH);
  digitalWrite(CE, HIGH);
  digitalWrite(WE, HIGH);
  Serial.println("Initializing pins for write...");
  delay(1000);
  //for (int i = 0; i < sizeof(data); i++) {
  //writeByte(i, data[i]);
  //}
  for (int i = 0; i < sizeof(data); i++) {
    readByte(i);
  }
  while (true);
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Excel supports VBA. You can just write a little code to generate and save out a file, already written in C code. Any time you change the table, just run the code to punch out the updated version of code. The rest of your code just .include's the file. Nothing fancy. \$\endgroup\$ Commented Sep 26, 2023 at 2:04
  • \$\begingroup\$ So what exactly will be exported in C? A whole table as an .include file? \$\endgroup\$ Commented Sep 26, 2023 at 6:56
  • \$\begingroup\$ I'm not sure; your question. You can write code in VBA and it will create any kind of file you want. You just write code. If you want a C include file to be exported, you can do that. If you just want a binary file on disk, you can do that. If you want to communicate with a different process on the operating system, or any other operating system on the web, you can do that. The only limit is your imagination. I was merely suggesting generation of code that can be directly consumed by your C compiler. But you do what you want. That was just my suggestion. \$\endgroup\$ Commented Sep 26, 2023 at 11:25

1 Answer 1

1
\$\begingroup\$

To turn spreadsheets into C array code, I have typically done the following, although I use LibreOffice instead of Excel, there should be something similar possible.

In column F (eg F4), create a formula like ="0x"&E4&","

At the top of column G (eg G1), =CONCAT(F4:F66)

Then copy the cell G1 to your Arduino sketch and surround it by:

byte data[] = {

};

It looks like your sample code has 32-bit data for the 8-bit data type, and at least one element is lacking the 0x HEX prefix.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.