uint8_t spi_send_rcv(uint8_t data) { uint8_t i,temp; temp = 0; clk(0); for(i=0; i<8; i++) { if(data & 0x80) { mosi(1); } else mosi(0); data <<= 1; clk(1); _nop_(); _nop_(); temp <<= 1; if(miso())temp++; clk(0); _nop_(); _nop_(); } return temp; }
void SPI::begin() { OutputPin sck(SPI_SCK_PORT, SPI_SCK_PIN); OutputPin mosi(SPI_MOSI_PORT, SPI_MOSI_PIN); InputPin miso(SPI_MISO_PORT, SPI_MISO_PIN); OutputPin ss(SPI_SS_PORT, SPI_SS_PIN); sck.begin(); mosi.begin(); miso.begin(); ss.begin(); sck.clear(); mosi.clear(); ss.set(); // Warning: if the SS pin ever becomes a LOW INPUT then SPI // automatically switches to Slave, so the data direction of // the SS pin MUST be kept as OUTPUT. SPCR = (1<<MSTR) | (1<<SPE); }