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.

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.
  • Can i use the arduino's functions lowByte() highByte()? – victorhrs Oct 15 '15 at 19:58
  • You can, yes. Or just do it manually with >> 8 and & 0xFF – Majenko Oct 15 '15 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() {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.