/* General support for SPI devices, but using bit banging and using pin definitions rather than the built in spi functions in the microcontroller. No specific initialisation is needed except port directions. requires definitions of ATMEL ports: SPI_PORT Port for the SPI signals eg PORTB SPI_DDR DDR for the SPI signals eg DDRB SPI_PIN Input Port for the SPI signals eg PINB SPI_SCK Clock from master to slave SPI_MOSI Data from Master to slave SPI_MISO Data from slave to master SPI mode 0 where data to slave is latched on leading edge of SCK, and data from slave to master is latched by master on fallling edge of SCK Uses delay functions which require definition of F_CPU in config.h */ /* 23/2/2011 svn 282 */ #include #include #include #include #include inline void spi_clock_up_down(void) { SPI_PORT |= (1<> SPI_MISO) & 0x01); result = (result << 1) | bitresult; spi_clock_up_down(); } return result; } uint16_t read_spi_2byte(void) { uint8_t i; uint8_t bitresult; uint16_t result = 0; SPI_PORT &= ~(1<> SPI_MISO) & 0x01); result = (result << 1) | bitresult; spi_clock_up_down(); } return result; } void write_spi_byte(uint8_t data) { /* enter and leave with with SCK low */ uint8_t bitpos; uint8_t bitvalue; SPI_PORT &= ~(1<0;--bitpos) { bitvalue = ((data >> (bitpos-1)) & 0x01); if (bitvalue) { SPI_PORT |= 1<