Before revision: // transmit byte serially, MSB first void send_8bit_serial_data(unsigned char data) { int i; // select device (active low) output_low(SD_CS); // send bits 7..0 for (i = 0; i < 8; i++) { // consider leftmost bit // set line high if bit is 1, low if bit is 0 if (data & 0x80) output_high(SD_DI); else output_low(SD_DI); // pulse the clock state to indicate that bit value should be read output_low(SD_CLK); delay(); output_high(SD_CLK); // shift byte left so next bit will be leftmost data <<= 1; } // deselect device output_high(SD_CS); } Considerations The question whether to deploy bit banging or not is a trade-off between load, performance and reliability on the one hand, and the availability of a hardware alternative on the other.
After revision: // transmit byte serially, MSB first void send_8bit_serial_data(unsigned char data) { int i; // select device (active low) output_low(SD_CS); // send bits 7..0 for (i = 0; i < 8; i++) { // consider leftmost bit // set line high if bit is 1, low if bit is 0 if (data & 0x80) output_high(SD_DI); else output_low(SD_DI); // pulse clock state to indicate that bit value should be read output_low(SD_CLK); delay(); output_high(SD_CLK); // shift byte left so next bit will be leftmost data <<= 1; } // deselect device output_high(SD_CS); } Considerations The question whether to deploy bit banging or not is a trade-off between load, performance and reliability on the one hand, and the availability of a hardware alternative on the other.