1

How can i send numbers like 300 or 1024 ?

I'm using the recommendation of the atmel's manual.

void SpiTransmitir(unsigned char data)
{
 SPDR = data;
 while(!(SPSR & (1<<SPIF)));//{Serial.println("While");}
}

I imagined that one bit shift could be the solution, but it still fails.

asked Oct 15, 2015 at 19:35

2 Answers 2

1

You need to send two bytes - a high byte and a low byte.

There are two things you need to know with SPI:

  1. You must wait for one byte to finish transmitting before you send the next
  2. The whole transaction needs to be wrapped with a digital chip select signal.

So you have:

  1. Chip Select idles HIGH
  2. You lower Chip Select
  3. You send one byte
  4. You send the other byte
  5. You raise chip select

At the receiver you:

  1. Wait for chip select to go low
  2. Receive one byte
  3. Receive a second byte
  4. Chip Select rises
  5. You combine the two bytes together into one integer.
answered Oct 15, 2015 at 19:42
2
  • Can i use the arduino's functions lowByte() highByte()? Commented Oct 15, 2015 at 19:58
  • You can, yes. Or just do it manually with >> 8 and & 0xFF Commented Oct 15, 2015 at 20:46
0

Try to join the numbers as follows:

#define NUMERO 342
void setup() {
 Serial.begin(9600);
 Serial.println(NUMERO, BIN); // Mostra valor do número em binário
 uint8_t MSB = 0, LSB = 0; // Dois números de 8 bits
 uint16_t junta = 0; // Valor junto em 16 bits
 MSB = NUMERO >> 4;
 LSB = NUMERO << 4;
 junta = (MSB << 4) | (LSB >> 4) ;
 Serial.println(junta, BIN);
}
void loop() {}
answered Oct 16, 2015 at 11:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.