Teensy 3.6 data read from EEPROM at boot up

Status
Not open for further replies.
Hello Everyone,

I am trying to give the input through serial monitor to the variables and writing that values in the EEPROM. I am able to read and write from EEPROM on teensy 3.6.
according to the code it should read the value from EEPROM after booting up. But it is not reading from EEPROM. if i connect usb and write the code I can see the output but if I remove the usb and connect it again i dont see the output.

I have used ARDUINO _IDE to program teensy.

Thank you
Anurag
 
A small hint:
Forum Rule: Always post complete source code & details to reproduce any issue
 
Below is the complete code here the values are taken through serial and saved into EEPROM. Actually according to this code tthe vales to be saved in EEPROM as the values can be modified at any time from other source and should be read from eeprom at next bootup. So that the loop runs continuously. everytime i power it on.

Everytime i need to reenter the values through serial to make it run even after writing it in eeprom.


Code:
//*****************************variables values to input through serial**************************
uint32_t  ST_F;
uint32_t  STP_F;          
uint32_t  CH_SP;     
int       Delay;          
//*****************************variables values to be read from EEPROM***************************
uint32_t  SF;             
uint32_t  STF;            
uint32_t  CSP;           
int       Del;                           

uint32_t Int_Value;

void EEPROMWritelong(int address, long value)
{
    byte four = (value & 0xFF);
    byte three = ((value>>8)&0xFF);
    byte two = ((value>>16)&0xFF);
    byte one = ((value>>24)&0xFF);

    EEPROM.write(address , four);
    EEPROM.write(address+1, three);
    EEPROM.write(address+2, two);
    EEPROM.write(address+3, one);
}
long EEPROMReadlong(long address)
{
    long four = EEPROM.read(address);
    long three = EEPROM.read(address+1);
    long two = EEPROM.read(address+2);
    long one = EEPROM.read(address+3);

    return ((four<<0)&0xFF)+((three<<8)&0xFFFF)+((two<<16)&0xFFFFFF)+((one<<24)&0xFFFFFFFF);
}

void setup()
{
    EEPROM.begin();
    SPI.begin();
    SPI.beginTransaction(SPISettings(SPI_Speed, MSBFIRST, SPI_MODE0));//60MHz
}
void loop()
{
  if(Serial.available() > 0)
  {
      Int_Value = 0;
      incomingByte = Serial.read();

      if(incomingByte == 'A') // Start _Frequency1
     {
          ST_F = Int_Value * 1000000;
          EEPROMWritelong(1024, ST_F);
          SF = EEPROMReadlong(1024);
          Serial.println(ST_F);
          Int_Value = 0;
    }  
    else if(incomingByte == 'B') // Start _Frequency1
    {
         STP_F = Int_Value * 1000000;
         EEPROMWritelong(1040, STP_F);
         STF = EEPROMReadlong(1040);
         Serial.println(STP_F);
         Int_Value = 0;
    }   
    else if(incomingByte == 'C') // Start _Frequency1
    {
         CH_SP = Int_Value * 1000;
         EEPROMWritelong(1056, CH_SP);
         CSP = EEPROMReadlong(1056);
         Serial.println(CH_SP);
         Int_Value = 0;
    }   
    else if(incomingByte == 'D') // Start _Frequency1
    {
         Delay= Int_Value;
         EEPROMWritelong(1072, Delay);
         Del = EEPROMReadlong(1072);
         Serial.println(Delay);
         Int_Value = 0;
    }     
     else if(incomingByte == 'E') // Start _Frequency1
    {
         incomingByte = ' ';
         while(1)
         {
             for(FQ = SF; FQ <= STF; FQ = FQ + CSP)
             {
              
             }
      }
}
 
Last edited by a moderator:
program seems no valid syntax
maybe bad cut and past.
I would suggest, that you remove all code that is not needed to demonstrate the problem (e.g. SPI and other) and everyone can easily reproduce the problem and test solutions.
 
Maybe I am missing something, but with your code fragment, I don't see anything that does anything unless you have Serial input available?

Like where is the code that actually reads the EEPROM? Where is the code showing that it did not read?
 
there are two function used in the code ,
void EEPROMWritelong(int address, long value) // this function is to write the values to EEPROM
and long EEPROMReadlong(long address) // this function is to read the EEPROM data.

In the code
Int_Value = 0;
incomingByte = Serial.read();

if(incomingByte == 'A') // Start _Frequency1
{
ST_F = Int_Value * 1000000;
EEPROMWritelong(1024, ST_F); // this part writes the value to address
SF = EEPROMReadlong(1024); // this part reads the value from the address.
Serial.println(ST_F);
Int_Value = 0;
}
This code works for reading and writing from EEPROM properly thats not the problem, Actually problem here is after booting up it does not read the value from the address instead I have to enter all the values again through serial monitor and till the usb is plugged in it gives the output. After usb is removed there is no output even if I power it externally.

The input which i am giving through serial monitor should be saved in eeprom and when i boot it up next time that value should be read from memory.
 
Yes, you have functions that read and write the EEPROM... But where is the code that is supposed to read these values in at startup?

That is, your setup function:
Code:
void setup()
{
    EEPROM.begin();
    SPI.begin();
    SPI.beginTransaction(SPISettings(SPI_Speed, MSBFIRST, SPI_MODE0));//60MHz
}
All it does is tell eeprom to initialize... And starts SPI, and starts up some transaction... So nothing here reads in from the EEPROM.

Then the loop code, or at least the fragment you have here...
Code:
void loop()
{
  if(Serial.available() > 0)
  {
      Int_Value = 0;
      incomingByte = Serial.read();

Does nothing if there is no Serial input... So again nothing reading anything from the EEPROM...
 
Hi,

Actually after writing it once in EEPROM, i tried to use this logic in the setup function to read the values. THts logic is shown below. But thats doesn't seem to be working out.
Can u help me out here?

void setup()
{
// EEPROM.init();
EEPROM.begin();
Serial.begin(9600);
SPI.begin();
SPI.beginTransaction(SPISettings(SPI_Speed, MSBFIRST, SPI_MODE0));//60MHz

pinMode(pinSPI_SS, OUTPUT);
pinMode(IO_UPDATEPIN, OUTPUT);
pinMode(MASTER_R, OUTPUT);
pinReset(MASTER_R);


SF = EEPROMReadlong(1024);
STF= EEPROMReadlong(1028);
CSP = EEPROMReadlong(1032);
Del= EEPROMReadlong(1036);

}
 
Again bits and pieces of code is hard to debug...
From your above...
Code:
SF = EEPROMReadlong(1024);
STF= EEPROMReadlong(1028);
CSP = EEPROMReadlong(1032);
Del= EEPROMReadlong(1036);

From your first posting, extracting things you see:
Code:
  {
          ST_F = Int_Value * 1000000;
          [COLOR="#FF0000"]EEPROMWritelong(1024, ST_F);[/COLOR]
          SF = EEPROMReadlong(1024);
          Serial.println(ST_F);
          Int_Value = 0;
    }  
    else if(incomingByte == 'B') // Start _Frequency1
    {
         STP_F = Int_Value * 1000000;
        [COLOR="#FF0000"] EEPROMWritelong(1040, STP_F)[/COLOR];
         STF = EEPROMReadlong(1040);
         Serial.println(STP_F);
         Int_Value = 0;
    }   
    else if(incomingByte == 'C') // Start _Frequency1
    {
         CH_SP = Int_Value * 1000;
         [COLOR="#FF0000"]EEPROMWritelong(1056, CH_SP);[/COLOR]
         CSP = EEPROMReadlong(1056);
         Serial.println(CH_SP);
         Int_Value = 0;
    }   
    else if(incomingByte == 'D') // Start _Frequency1
    {
         Delay= Int_Value;
         [COLOR="#FF0000"]EEPROMWritelong(1072, Delay);[/COLOR]
         Del = EEPROMReadlong(1072);
         Serial.println(Delay);
         Int_Value = 0;
So in your latest shown startup code, you are using adddress: 1024, 1028, 1032, 1036
In loop code you are using: 1024, 1040, 1056, 1072...

So either your code extracts are from different versions or your issue is you are not consistent on which EEPROM addresses you are using.

Which again gets back to posting complete examples as these details matter. Hope that helps
 
you dont need to “always” set it via serial. you can upload a sketch that would write whatever bytes you want to the eeprom, then load your normal sketch which would read it fine.
 
Please post complete programs which demonstrate your problem (eg, the "Forum Rule").

Complete means anyone can copy & paste the code from your message into Arduino and upload to a Teensy 3.6 to recreate the problem. Please, before you post the message, open a new window in Arduino and copy the exact code from your message into that Window. Upload it and check that it really does show the problem.

Please DO NOT give us programs which do not even pass Verify. We want to help you, but incomplete programs which do not even verify waste everyone's time. It's much harder to help you when we can't even run the programs you give without editing them!
 
// I will try to explain my problem with a simple code here

One logic I am trying to figurre out here is to read the data from the EEPROM at startup.
The below does the job till the power is on through usb
it will read and write the data till rhe power is on but if i replug the usb again it does not show the output in the serial monitor I need to enter it again.

I have figured out the problem here
may be its because of (if(Serial.available()>0) as it checks it everytime the program is dumped in the microcontroller) so I just used an else condition to if(Serial.available()>0) which is commented in this code 1. but his does not seem to be working either.

//code_1;

#include<EEPROM.h>

char data, data1;
char read_data, read_data1;

void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
char input_char = Serial.read();

if(input_char == 'a')
{
data = input_char;
EEPROM.write(1024,data);
Serial.print("data = ");
Serial.println(data);
read_data = EEPROM.read(1024);
Serial.print("read_data = ");
Serial.println(read_data);

}
else if(input_char == 'b')
{
data1 = input_char;
EEPROM.write(1024,data1);
Serial.print("data = ");
Serial.println(data1);
read_data1 = EEPROM.read(1025);
Serial.print("read_data = ");
Serial.println(read_data1);
}

}
//else ()
//{
// read_data = EEPROM.read(1024);
// Serial.print("read_data = ");
// read_data1 = EEPROM.read(1025);
// Serial.print("read_data = ");
//}
//}

But in code 2
if i write it this way

code 2:

void setup()
{
a = 1;
EEPROM.write(1024, a);
}

void loop()
{
int b = EEPROM.read(1024);
Serial.print(b);
delay(1000);
}

this code works properly as it reads the value even after the usb is repluggued in microontroller. The same thing I want to make it work using serial monitor.
 
Again you have posted programs which do not Verify when copied into Arduino.

The mistakes appear to be "simple", perhaps commenting the last line on "code 1" and forgetting #include <EEPROM.h> on "code 2". Maybe?

But the point is you are not even verifying the actual code you give us. We want to help you. Why would you make helping so difficult by giving us code with errors?

Please post correct, complete programs. We *will* copy them into Arduino and click Verify, maybe even run then on real hardware to understand what's wrong. Please do not waste everyone's time with broken code that they must try to fix before being able to help you!


sc.png
 
Here, let me demonstrate how easy this can be. Here is a simple test program I wrote just now.

Code:
#include <EEPROM.h>

void setup() {
  while (!Serial);
  Serial.println("EEPROM Test");
}

void loop() {
  int n = EEPROM.read(1024);
  n = n + 1;
  EEPROM.write(1024, n);
  Serial.print("n = ");
  Serial.println(n);
  delay(1000);
}

This is a complete program. You can copy it into a blank Arduino window and click Verify. I double checked just now.

I'm also running it here on a Teensy 3.6. Each time the program runs, it shows the incrementing number which does indeed remember the state from the last time the program ran. If using Teensyduino 1.42 (with the new improvements to the serial monitor) you can even unplug the board while the serial monitor is open, and then plug it back in a few seconds later to see the program restart and continue counting where it left off.

sc.png

Hopefully this shows the EEPROM feature does indeed work, and I hope you can see with just a little extra effort you can post complete programs.

Please copy this program into Arduino and upload it to your Teensy 3.6. What the numbers printing in the serial monitor. I believe you can easily see it does indeed work. That is the point of posting code on the forum, so other people can actually use it and see the results.

Please, if you want us to help you, help save the valuable time of everyone here who is willing to assist you. Post complete programs which you have actually run and actually do demonstrate the problem.
 
Status
Not open for further replies.
Back
Top