# Bit-banging SPI

#### champ1

Joined Jun 4, 2018
136
Hello

I have been followed many links but I do not have clear understanding on Bit-banging SPI communication.

Code:
MISO - Master In Slave Out
MOSI - Master out Slave In
CLK   - Clock
CS    - Chip select
I studied some tutorials and I believe below steps need to send data on SPI bus

1. We need to Initialize SPI Bus
2. We need to set the CS pin low to begin communication
3. We need to set the MOSI pin to a 1 or a 0,
4. We need to set the clock high so data is transmitted by the master and received by the slave
5. We Read the state of MISO to receive the first bit of data from slave
6. We Set CLK Low, so data can be sent on the next rising edge
7. We repeat this process
8. We Set the CS pin High to stop transmission.

I do not understand step 3 and step 4 Is there someone wo can help me to understand these steps

Last edited:

#### ericgibbs

Joined Jan 29, 2010
17,168
hi champ1,

#3, Your program places the 1st Bit of Data on MOSI
#4, Your program Sets CLK High while MOSI has the Data Bit present. [ the program also Reads the MISO Data input at this time]

It is important that you know the timing limitations of the Slave, so that data exchange can be correct.

E

#### Attachments

• 407.3 KB Views: 64

#### jpanhalt

Joined Jan 18, 2008
11,087
What MCU? What language?

Remember, the transfer is always synchronous; although, a transferred byte may be nonsense. Also, SPI can work down to very low baud rates. That is, you can generate the clock with "bit-on/delay/bit-off" code.

#### champ1

Joined Jun 4, 2018
136
What MCU? What language?
8051 and C language

#3, Your program places the 1st Bit of Data on MOSI
#4, Your program Sets CLK High while MOSI has the Data Bit present. [ the program also Reads the MISO Data input at this time]
I didn't understand exactly #3 and #4 points

What happen when MOSI pin set to a High or a Low,

#### ericgibbs

Joined Jan 29, 2010
17,168
hi,
Consider just the Master to Slave transfer and that the Data is a 8 Bit Byte, say Binary10101111
The program Sets the MOSI pin to what ever Bit 7 is of the Byte , in the example '1' = High
then the program sets the CLK High, as the the Clk goes High this triggers the Slave to read the MOSI pin, after a short period the Master sets the CLK low.

Before the next Bit6, the program Sets MOSI '0' =Low and as the the Clk goes High this triggers the Slave to read the MOSI pin and so on for all 8 BIts.

E

#### champ1

Joined Jun 4, 2018
136
hi,
Consider just the Master to Slave transfer and that the Data is a 8 Bit Byte, say Binary10101111
The program Sets the MOSI pin to what ever Bit 7 is of the Byte , in the example '1' = High
then the program sets the CLK High, as the the Clk goes High this triggers the Slave to read the MOSI pin, after a short period the Master sets the CLK low.
Before the next Bit6, the program Sets MOSI '0' =Low and as the the Clk goes High this triggers the Slave to read the MOSI pin and so on for all 8 BIts.
I am sorry ericgibbs but still I am not getting your point.

This is my starting point. I want to convert algorithm into flow chart

The program Sets the MOSI pin to what ever Bit 7 is of the Byte , in the example '1' = High
Where to set MOSI pin in the flow chart

#### ericgibbs

Joined Jan 29, 2010
17,168
hi,
Johns clip shows how its done.

for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address byte
{
if (SPIData & 0x80) // Check for a 1
SPI_MOSI = 1; // and set the MOSI line appropriately
else
SPI_MOSI = 0;
SPI_CK = 1; // Toggle the clock line
SPI_CK = 0;
SPIData <<= 1; // Rotate to get the next bit

} // and loop back to send the next bit
// Repeat for the Data byte

#### champ1

Joined Jun 4, 2018
136
hi,
Johns clip shows how its done.
I have added further information in my flow chart

I do not understand this condition data & 0x80

For example if I want to send data = 10101111

data = data & 0x80 // 0x80 = 10000000
data = 10101111 & 10000000 = 10000000

so the condition is true then we are setting MOSI Pin

Please Look at PDF for clarity

#### Attachments

• 178.8 KB Views: 33

#### djsfantasi

Joined Apr 11, 2010
8,790
I have added further information in my flow chart

View attachment 189673

I do not understand this condition data & 0x80

For example if I want to send data = 10101111

data = data & 0x80 // 0x80 = 10000000
data = 10101111 & 10000000 = 10000000

so the condition is true then we are setting MOSI Pin

Please Look at PDF for clarity
So far, I agree with you. Now, what happens the second time through the loop?
First, what is the value of data after the first loop?

data is now 01011110! Do you see why? And what will be the value of MOSI after the second loop?

data = data & 0x80 // 0x80 = 10000000
data = 01011110 & 10000000 = 00000000

The shift operation puts the next data bit in the highest position, which is “picked off” by the line
data = data & 0x80​

#### ericgibbs

Joined Jan 29, 2010
17,168
I do not understand this condition data & 0x80
hi

if (SPIData & 0x80) // Check for a 1

As you should know
If you have a 8Bit Byte and you AND it with 0x80 , the MSBit will be set to either '1' or '0' depending upon the value of Bit 7

For example if I want to send data = 10101111
So 1010 1111 bit 7 [msb] is '1' ANDed with 0x80 [1000 0000] gives 1000 0000, so MOSI is set to '1'

SPIData <<= 1; // Rotate to get the next bit
This makes your original 10101111 into 01011110 so the next ANDing gives '0' for MOSI.

Do you follow.?
E

#### champ1

Joined Jun 4, 2018
136
So far, I agree with you. Now, what happens the second time through the loop?
First, what is the value of data after the first loop?
count = 0, data = 1010 1111
check ( 1010 1111 & 1000 0000) = True
shift 1010 1111 << 1 = 010 11110

count = 1, data = 010 11110
check (010 11110 & 1000 0000) = True Edit :check (010 11110 & 1000 0000) = False
shift 010 11110 <<1 =10 111100

count = 2, data = 10 111100
check (10 111100 & 1000 0000)= True
shift 10 111100 <<1 = 0 1111000

count = 3 , data = 0 1111000
check ( 0 1111000 & 1000 0000) = True Edit : check ( 0 1111000 & 1000 0000) = False
shift 0 1111000 <<1 = 11110000

count = 4, data = 11110000
check (11110000 & 1000 0000)
shift 11110000 << 1 = 1110000 0

count = 5, data = 1110000 0
check (1110000 0 & 1000 0000) = True
shift 1110000 0 <<1 = 110000 00

count = 6, data = 110000 00
check (110000 00 & 1000 0000) = True
shift 110000 00 << 1 = 10000 000

count = 7 , data =10000 000
check (10000 000 & 1000 0000) = True
shift 10000 000 << 1 = 0000 000 0

Update :

Last edited:

#### djsfantasi

Joined Apr 11, 2010
8,790
count = 0, data = 1010 1111
check ( 1010 1111 & 1000 0000) = True
shift 1010 1111 << 1 = 010 11110

count = 1, data = 010 11110
check (010 11110 & 1000 0000) = True
shift 010 11110 <<1 =10 111100
...
... I’m stopping you here! Why do you think the check is true?
__01011110​
&10000000​
is equal to 00000000, and hence is false.

& is a bit-wise AND operator. If both corresponding bits in the two operands are 1s, then the corresponding bit in the result is a 1; otherwise it is a zero.

In your loop, where count equals 1 or three, the test is false.

Got it?

Bonus Question: When the test is false, what do you set MOSI to and what is written to the I2C bus?

Last edited:

#### champ1

Joined Jun 4, 2018
136
... I’m stopping you here! Why do you think the check is true?
Got it?
Thanks for pointing a mistake. I understood
Bonus Question: When the test is false, what do you set MOSI to and what is written to the I2C bus?
if (data & 1000 00) is true then set MOSI Pin else clear MOSI pin

The master sends and receives a byte on the SPI simultaneously

We are just sending byte on SPI so How we will receive a byte on SPI

Last edited:

#### djsfantasi

Joined Apr 11, 2010
8,790

While in real time we can’t send/ receive simultaneously (with one cpu), practically sending and receiving sequentially can occur close enough that it appears simultaneous. That’s why timing diagrams are important. They show how fast sending and receiving must be to be successful.

#### champ1

Joined Jun 4, 2018
136
Bonus Question: When the test is false, what do you set MOSI to and what is written to the I2C bus?
@djsfantasi Where does I2C come ? We were talking on the SPI Bus
same question, Where does I2C come ? we are on SPI Bit-banging communication.

#### djsfantasi

Joined Apr 11, 2010
8,790
@djsfantasi Where does I2C come ? We were talking on the SPI Bus

same question, Where does I2C come ? we are on SPI Bit-banging communication.
My mistake. I meant SPI.

#### champ1

Joined Jun 4, 2018
136
While in real time we can’t send/ receive simultaneously (with one cpu), practically sending and receiving sequentially can occur close enough that it appears simultaneous. That’s why timing diagrams are important. They show how fast sending and receiving must be to be successful.
@ericgibbs shown PDF file see post #2

I was reading section 23. There are timing diagram's 23.9 , 29.10, 29.11,
I don't get any idea from that diagram's

#### ericgibbs

Joined Jan 29, 2010
17,168
hi champ1,
I don't exactly follow what you mean by 'ideas' from those timing diagrams.?
Mark up this clip from the d/s, showing the sections that are giving you a problem.

Do you have any programming experience with MCU's.?
E

#### Attachments

• 33.7 KB Views: 12