Skip to content

Instantly share code, notes, and snippets.

@DaelonSuzuka
Last active November 4, 2021 02:47
Show Gist options
  • Save DaelonSuzuka/1c43cdc5f76beadaf6fdd52c73c39c13 to your computer and use it in GitHub Desktop.
Save DaelonSuzuka/1c43cdc5f76beadaf6fdd52c73c39c13 to your computer and use it in GitHub Desktop.
SPI bitbang driver
#include "stdint.h"
/* ************************************************************************** */
#define set_STROBE_PIN(state) LATXbits.LATXY = state // your register here
#define set_CLOCK_PIN(state) LATXbits.LATXY = state // your register here
#define set_DATA_PIN(state) LATXbits.LATXY = state // your register here
static void spi_bitbang_tx_word(uint16_t word) {
set_STROBE_PIN(1);
set_CLOCK_PIN(0);
for (uint8_t i = 0; i < 16; i++) {
if (word & (1 << (15 - i))) {
set_DATA_PIN(1);
} else {
set_DATA_PIN(0);
}
delay_us(10);
set_CLOCK_PIN(1);
delay_us(10);
set_CLOCK_PIN(0);
delay_us(10);
}
set_STROBE_PIN(0);
delay_us(10);
set_STROBE_PIN(1);
delay_us(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment