Can anyone help me convert the below assembly language code to Arduino(Embedded C) Code. I'm trying to program ATTiny24 as an SPI slave to establish communication between it and arduino NANO(Master).
init:
ldi r16,(1<<USIWM0)|(1<<USICS1)
out USICR,r16
...
SlaveSPITransfer:
out USIDR,r16
ldi r16,(1<<USIOIF)
out USISR,r16
SlaveSPITransfer_loop:
in r16, USISR
sbrs r16, USIOIF //sbrs - skip if bit in register is set
rjmp SlaveSPITransfer_loop //relative jump
in r16,USIDR
ret
I've only figured that the initialization part is written as: USICR = USICR | (1<<USIWM0)|(1<<USICS1);
void SPIBegin()
{
// Set Wire Mode 1: Three-wire mode. Uses DO, DI, and USCK pins.
// Set clock select 4: External, positive edge
USICR = (1 << USIWM0) | (1 << USICS1);
}
uint8_t SPITransfer(uint8_t data)
{
USIDR = data; // Load the data register
USISR = 1 << USIOIF; // Clear Counter Overflow Interrupt Flag (yes, by writing a 1 to it)
// Wait for the Counter Overflow Interrupt Flag to be set
// which means the transfer is complete
while (!(USISR && (1 << USIOIF))) {}
return USIDR; // Return received data
}
uint8_t SPITransfer(uint8_t data)
{
USIDR = data; // Load the data register
USISR = 1 << USIOIF; // Clear Counter Overflow Interrupt Flag (yes, by writing a 1 to it)
// Wait for the Counter Overflow Interrupt Flag to be set
// which means the transfer is complete
while (!(USISR && (1 << USIOIF))) {}
return USIDR; // Return received data
}
Thanks a lot bro...will implement this and let you know about the result.
johnwasser:
Doesn't your ATtiny core include an SPI library? It would be much easier to use an existing library than to re-invent the wheel.
No bro there's no such spi library for ATTiny ...infact there's no dedicated spi port... you've to use usi to create spi hence the trouble of writing code