So Im writing a bitbang SPI sample code for AVR (So I can understand using SPI better and appreciate the SPI hardware), and im a bit confused at some things.
From what I've read on SPI, you shift out the LSB first from master....but im not sure how to do that? I mean essentially I want to pop the LSB off and set the new 8 bit binary value to before but with the LSB popped off
(Im doing this in a loop, taking a unsigned char and then wanting to send out the LSB each loop).
This is what I have so far:
unsigned char transmit_byte(unsigned char data_byte)
{
int i;
char ret_data;
enable_SPI(); /* Set up correct Data Direction Ports */
_delay_ms(100);
ss_low(); /* Set SS Low */
_delay_ms(100);
/* SCLK Currently holding low, MOSI is low, SS is low */
for (i = 0; i <= 7; i++)
{
if (0x80 & SPI_PORT)
{
mosi_high();
}
else
{
mosi_low();
}
/* MOSI Value is latched in */
_delay_ms(10); // Probably unnecessary
//something here?
}
}
Would I just right shift it out?, I will then read the MISO line and check for 1 or 0 and set a new character variable, but that's after each iteration.