SPI.beginTransaction not working

I am using a w5500 ethernet shield with my arduino uno with an as5048a encoder to send data over matlab.

I am using spi to get data from as5048a

But whenever I use

SPI.beginTransaction(SPISettings(9000000,MSBFIRST,SPI_MODE1));

for the as 5048a I am not able to send neither receive data from Matlab

without that line the code works fine but then I am not able to get the correct data from the encoder

Can anyone elaborate?

That beginTransaction has the wrong settings for the w5500. The Wiznet library does not use the SPI.beginTransaction and SPI.endTransaction calls, so you must add them before and after the ethernet library calls.

SPI.beginTransaction(SPISettings(4000000,MSBFIRST,SPI_MODE0));
// your ethernet stuff
SPI.endTransaction();

edit: Or set the mode manually before using the w5500.

SPI.setDataMode(SPI_MODE0);

I tried to add SPI.endtransaction() at the end of void loop and SPI.begintransaction(SPI settings) in the first line of the void loop()

I am still not able to send any data

Then don't use SPI.beginTransaction anywhere. It appears the only difference is the SPI mode. Just change it.

If I dont use the SPI.beginTransaction

I get a static value from the encoder

It does not show a change

You can do it the "old fashioned way".

SPI.setDataMode(SPI_MODE1);
// do your encoder stuff
SPI.setDataMode(SPI_MODE0);

It appears you do not need to change any other setting.

Still does not work
:frowning:

Must be your code. How did you troubleshoot it?

Here is my code

%code

/* AS5048A Angle Sensor Firmware

MOSI : pin 11
MISO: pin 12
SCK: pin 13
CS: pin 10
Vcc : 3.3V

*/
#include <SPI.h>
#include <Ethernet.h>
int m = 0,i = 0,j= 0,a = 0,b = 0;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE2 };
IPAddress ip(169,254,173,240);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 30000 is default for HTTP):
EthernetServer server(30000);

/* These registers are declared volatile because they can change unexpectedly.

  • If declared as just unsigned, then compiler may cache in memory. volatile
  • turns off compiler optimizations
    */
    #define ERRFL (volatile unsigned word)0x0001
    #define PROG (volatile unsigned word)0x0003
    #define DIAAGC (volatile unsigned word)0x3ffc
    #define MAG (volatile unsigned word)0x3ffd
    #define ANGLEUNC (volatile unsigned word)0x3ffe
    #define ANGLECOM (volatile unsigned word)0x3fff

#define MAX_SENSOR_VAL 16383 //2^14 - 1
#define pauseTime 5 // change this to increase feedback frequency
#define BAUD_RATE 115200

int CS_PIN = 9;

void setup()
{
Ethernet.begin(mac, ip);
server.begin();
// start the Ethernet connection and the server:
Serial.begin(115200);
//Serial.println(1);
error_flag = false;
set_zero((1.0*12300)/MAX_SENSOR_VAL); // instead of zero make this the analogvalue/(2^15 - 1) to set permanent zero
pinMode(CS_PIN,OUTPUT);
pinMode(CS_PIN-1,OUTPUT);
pinMode(CS_PIN-2,OUTPUT);
pinMode(6,OUTPUT);
SPI.begin();
delay(100);
}

void loop()
{
SPI.setDataMode(SPI_MODE1);
// SPI.endTransaction();
// SPI.end();
//SPI_val();
// Serial.println(“Here”);
// listen for incoming clients
EthernetClient client = server.available();
long tic = millis();
delay(pauseTime);
int16_t val = get_angle_without_compensation();
get_angle_with_compensation();
float val3=get_rotation();
val3 = val3/1.5;
val3 = val3*100;
val3 = (int)val3;
SPI.setDataMode(SPI_MODE0);
Serial.println(val3);
if (client) {
// an http request ends with a blank line
Serial.println(“Here_also”);
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
if(m==0)
{
int a =client.read();
Serial.println(a);
m=1;
}
a=(int)val3%1000;
j= val3/1000;
client.write(a);
client.write(j);
// i++;
}
//break;
}
// give the web browser time to receive the data
delay(1);
// close the connection:
// client.stop();
}
}

%code

I am running the matlab command window and Serial monitor simultaneously

It still does not give me the required result

It's your code. You are setting the wrong mode for the ethernet shield. The w5100 is MODE0, and your encoder is MODE1.

If you mean that by exchanging the modes it should work then

it does not

and it is a w5500 shield