SPI in setup() doesn't work inside if condition

Hello,

during setup i read a value from internal eeprom address 0 and if it reads 1 it should send data through SPI. The weird thing is that if the call to SPI_Write is inside IF statement no data is sent through SPI but if i move SPI_Write outside the IF then SPI works fine.

The "assigned_par==1" condition is met, i tested by inserting a digitalWrite to a LED

This code doesn't work:

void setup() 
{
  pinMode(SS, OUTPUT);
  pinMode(CTRL_LED_1, OUTPUT);

  SPI.begin(); //start MCU SPI interface 
  SPI_init(); //set DSP slave port to SPI
  
  assigned_par = EEPROM.read(0);
 
  if(assigned_par > 0)
  {
    exp_assigned = true;
  }
  if(assigned_par==1)
  {
     SPI_write(LPF_asgn_sw_addr, 1); //not executed?
     digitalWrite(CTRL_LED_1, HIGH); //pin goes HIGH accordingly
  }
}

This one works:

void setup() 
{
  pinMode(SS, OUTPUT);
  pinMode(CTRL_LED_1, OUTPUT);

  SPI.begin(); //start MCU SPI interface 
  SPI_init(); //set DSP slave port to SPI
  
  assigned_par = EEPROM.read(0);
 
  if(assigned_par > 0)
  {
    exp_assigned = true;
  }
  if(assigned_par==1)
  {
  }
  SPI_write(LPF_asgn_sw_addr, 1); //it works fine
}

Any idea why SPI_write function is not executed inside the IF statement even if it returns true but it is if i move it outside the IF statement?

I'm using atmega328p with 8Mhz internal RC clock

Adding a serial print might show why.

Try printing the value of assigned_par after you read it from EEPROM. Is it what you expect ?

If not then put a short delay(), perhaps of 100 milliseconds, before the EEPROM/read()

I'd be willing to bet it is something you've done vs a compiler issue. Use the Serial console for debugging.

What do your functions SPI_init() and SPI_write() do? I suspect they are doing something wrong. And what libraries are included? And what model Arduino are you using? And how are all those variables declared? And what does the rest of the code do?

1 Like

I have no serial communication on board but in the end i will try to connect a USB serial converter if i can't get it sorted out...

However the CTRL_LED_1 pin goes HIGH as expected here, as a cheap debugging method:

if(assigned_par==1)
  {
     SPI_write(LPF_asgn_sw_addr, 1); //not executed?
     digitalWrite(CTRL_LED_1, HIGH); //pin goes HIGH accordingly
  }

Yes, it is. I debugged the simple way since i have no serial on board:

if(assigned_par==1)
  {
     SPI_write(LPF_asgn_sw_addr, 1); //not executed?
     digitalWrite(CTRL_LED_1, HIGH); //pin goes HIGH accordingly
  }

I even tried to read/write the EEPROM through AVR ISP from Atmel Studio 7 to check that address zero data is 1.

I'm confident that address 0 has 0x01 data in it.

Why the delay before reading EEPROM? I tried delay after it but doesn't work either.

You may be confident, but that does not mean you're right. Unless/until you PROVE that, you simply don't know. And by FAR the most plausible explanation is that assigned_par is NOT ==1 when the if expression is evaluated.

I'm going to take a wild guess - I bet assigned_par == 0xff.

SPI_init() initializes the SPI slave interface of a DSP

SPI_write(int address, long data) //sends data to DSP via SPI

Libraries:

#include <DualFunctionButton.h>
#include <SPI.h>
#include <EEPROM.h>
#include <MIDI.h>

I'm using "Arduino on breadboard 8Mhz internal clock" as board in the IDE

Just saying i'm confident cause any other code inside the IF statement is executed correctly so i'm sure assigned_par==0x01 returns true.

So i assumed it was the SPI_write() the issue but nope, if i move it outside the IF it works.

Did you serial print

to prove it?

As noted, your confidence doesn't make you correct. If you can't print the value, perhaps blink it on the LED.

Yes i put a digitalWrite(LED, HIGH); and it works fine.

What's SPI_init() ?

That's not what I said. Have the LED blink the number of times equal to assigned_par's value.

Also, you didn't bother to post all your code. So, we don't know the data type of assigned_par. Perhaps you'd be so kind as to tell us.

Also, try:

  if(assigned_par != 1)  {
     SPI_write(LPF_asgn_sw_addr, 1);
  }

I think @Idahowalker was suggesting use a for loop with delays to blink the value of assigned_par.

Ok, i tried that one too and works fine. It blinks the number of times of assigned_par.

Which is how many times?

And, as already requested, tell us the data type of assigned_par.

The problem is that your basic premise that "SPI in setup() doesn’t work inside if condition" is nonsense. So, there's a problem elsewhere in your code or hardware.

If it were my project, the next step would be to observer the SPI lines with a logic analyzer like this one:

You also never answered this question:

Better yet, post your COMPLETE code.

I solved by exporting the compiled sketch as Arduino Uno board and setting fuses for running at 8Mhz with internal oscillator.

So i guess there's a compilation issue when hex file is exported with "ATmega328 on a breadboard". Don't know if this is a user made board or Arduino official.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.