问题
I receive data from USB to the internal memory of Atxmega 128A1
if ( (SWITCHPORTL.IN & PIN1_bm) == 0 )
{
j = 0;
while (j < NUM_BYTES)
{
// Wait till there is unread data in the receive buffer
while((USART.STATUS & USART_RXCIF_bm) == 0 ){};
// Read out the received data
__far_mem_write(j+SDRAM_ADDR, USART.DATA);
if (j == (NUM_BYTES-1))
{
// Toggle LED 1
LEDPORT.OUTTGL = PIN1_bm;
}
j++;
}
}
How to write code for transfer data from internal memory via SPI Master to two slaves such that odd and even byte of data should be send separately to two slaves? How to initiate master to two slaves(multiple slaves)?
回答1:
I think this is possible mostly when both the slave will understand data in a similar format, as in have same configuration for CPOL and CPHA. You can select one particular slave by enabling the CS pin respectively. So, your code can look like:
initMaster();
selectSlave1(); //Make CS1 low and CS2 high
spiTransmit(data1); //transmit odd byte
selectSlave2(); //Make CS2 low and CS1 high
spiTransmit(data2); //transmit even byte
回答2:
Since both slaves are connected to the same bus that only can be transfer one data at a time you have two options to choose from:
- implement a simple protocol so that each slave does know, which byte belongs to it. (example: one byte header with 6 bit length and 2 bit receiver code or (1 - slave 1, 2 slave 2 - 3 both slaves even/odd)
- use slave select pins and transfer a byte and make sure you do not send the next and deselect the first receiver until the old was send (calculate clock cycles how long would it take and
nop
them or use them for other usefull stuff))
来源:https://stackoverflow.com/questions/30662682/how-to-write-code-for-transfer-data-from-internal-memory-via-spi-master-to-2-sla