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.
2 Answers 2
You need to send two bytes - a high byte and a low byte.
There are two things you need to know with SPI:
- You must wait for one byte to finish transmitting before you send the next
- The whole transaction needs to be wrapped with a digital chip select signal.
So you have:
- Chip Select idles HIGH
- You lower Chip Select
- You send one byte
- You send the other byte
- You raise chip select
At the receiver you:
- Wait for chip select to go low
- Receive one byte
- Receive a second byte
- Chip Select rises
- You combine the two bytes together into one integer.
answered Oct 15, 2015 at 19:42
-
Can i use the arduino's functions lowByte() highByte()?Victor Reis– Victor Reis2015年10月15日 19:58:38 +00:00Commented Oct 15, 2015 at 19:58
-
You can, yes. Or just do it manually with
>> 8
and& 0xFF
Majenko– Majenko2015年10月15日 20:46:03 +00:00Commented Oct 15, 2015 at 20:46
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