bytevalue reset during SPI transfer

Osiris

New member
Hello,

I am using a Teensy 4.1 with a 74HC595 shiftregister. The data is stored in a byte which is send via SPI.
After the SPI-transfer the bytevalue is resetted to zero. Thats not how it should be. The bytevalue should be zero using bitWrite(IL,i,0);

Am I coding something wrong?
Osiris

My code:

#include <SPI.h>
const int latchPin = 10;
// Teensy 4.1 MOSI = 11 => 74HC595 pin 14
// Teensy 4.1 SCK = 13 => 74HC595 pin 11
// Teensy 4.1 CS = 10 => 74HC595 pin 12

int i;
byte IL ;
int Time_IL_off = 500;

//+------------------------------------------------------------------------------------------------------------------------
void setup()
{
SPI.begin();
pinMode(latchPin, OUTPUT);
//+-----------------------------
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
digitalWrite(latchPin, LOW);
SPI.transfer(B00000000);
digitalWrite(latchPin, HIGH);
SPI.endTransaction();
//+-----------------------------
Serial.begin(9600);
}
//+------------------------------------------------------------------------------------------------------------------------

void loop()
{
for(i = 0; i < 8; i++)
{
// ---------------------IL on ---------------------------------+
bitWrite(IL,i,1);
Serial.print("IL-1 "); Serial.println(IL, BIN);

//---------------SEND DATA --------------------------------------+
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
Serial.print("IL-1a "); Serial.println(IL, BIN); // data IL = value
digitalWrite(latchPin, LOW);
Serial.print("IL-1b "); Serial.println(IL, BIN); // data IL = value
SPI.transfer(&IL,1);
Serial.print("IL-1c "); Serial.println(IL, BIN); // data IL = zero
digitalWrite(latchPin, HIGH);
Serial.print("IL-1d "); Serial.println(IL, BIN); // data IL = zero
SPI.endTransaction();

// ---------------------IL off ------------------------------------+
bitWrite(IL,i,0);
Serial.print("IL-2 "); Serial.println(IL, BIN);
//--------------------SEND DATA------------------------------------+
delay(Time_IL_off);
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
digitalWrite(latchPin, LOW);
SPI.transfer(&IL,1);
digitalWrite(latchPin, HIGH);
SPI.endTransaction();


delay(500);
}
}
 
Code:
#include <SPI.h>
const int latchPin = 10;
// Teensy 4.1 MOSI = 11 => 74HC595 pin 14
// Teensy 4.1 SCK = 13 => 74HC595 pin 11
// Teensy 4.1 CS = 10 => 74HC595 pin 12

int i;
byte IL;
int Time_IL_off = 500;

//+------------------------------------------------------------------------------------------------------------------------
void setup()
{
	SPI.begin();
	pinMode(latchPin, OUTPUT);
	//+-----------------------------
	SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
	digitalWrite(latchPin, LOW);
	SPI.transfer(B00000000);
	digitalWrite(latchPin, HIGH);
	SPI.endTransaction();
	//+-----------------------------
	Serial.begin(9600);
}
//+------------------------------------------------------------------------------------------------------------------------

void loop()
{
	for (i = 0; i < 8; i++)
	{
		// ---------------------IL on ---------------------------------+
		bitWrite(IL, i, 1);
		Serial.print("IL-1 "); Serial.println(IL, BIN);

		//---------------SEND DATA --------------------------------------+
		SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
		Serial.print("IL-1a "); Serial.println(IL, BIN); // data IL = value
		digitalWrite(latchPin, LOW);
		Serial.print("IL-1b "); Serial.println(IL, BIN); // data IL = value
		SPI.transfer(&IL, 1);
		Serial.print("IL-1c "); Serial.println(IL, BIN); // data IL = zero
		digitalWrite(latchPin, HIGH);
		Serial.print("IL-1d "); Serial.println(IL, BIN); // data IL = zero
		SPI.endTransaction();

		// ---------------------IL off ------------------------------------+
		bitWrite(IL, i, 0);
		Serial.print("IL-2 "); Serial.println(IL, BIN);
		//--------------------SEND DATA------------------------------------+
		delay(Time_IL_off);
		SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
		digitalWrite(latchPin, LOW);
		SPI.transfer(&IL, 1);
		digitalWrite(latchPin, HIGH);
		SPI.endTransaction();


		delay(500);
	}
}
Please enclose your posted code between CODE tags using the # button above.
It makes your code so much more readable and helps others to understand your code and be able to help you.
 
Without looking through all the SPI code to make sure that I am looking at the Teensy 4.1 code, the behavior of sending with a reference ( pointer ) rather than by value is to put the receive data in the transmit array.

Code:
	inline static void transfer(void *buf, size_t count) {
		if (count == 0) return;
		uint8_t *p = (uint8_t *)buf;
		SPDR = *p;
		while (--count > 0) {
			uint8_t out = *(p + 1);
			while (!(SPSR & _BV(SPIF))) ;
			uint8_t in = SPDR;
			SPDR = out;
			*p++ = in;
		}
		while (!(SPSR & _BV(SPIF))) ;
		[COLOR="#FF0000"][B]*p = SPDR;[/B][/COLOR]
	}

You could send by value to prevent your variable being overwritten.
 
Or you could do:
Code:
SPI.transfer(src_buffer, (void *) 0,  num_bytes ); // Use the SPI class's internal buffer transfer function to indicate "no receive".
 
The extra "0" solved the problem. I totally forgot the SPI bus is bi-directional since I was not using the recieve side.

Changed SPI.transfer(&IL,1) into SPI.transfer(&IL,0,1)

Thank you very much!
Osiris
 
Back
Top