#include #include "intrins.h" #include "spi.h" #include "spi_cfg.h" //----------------------------------------------------------------------------- /* bit SS = 0; bit SCLK = 0; bit MISO = 0; bit MOSI = 0; */ void CS_HIGH(void) {SS = 1;} void CS_LOW(void) {SS = 0;} unsigned char spiReadWrite(unsigned char Byte ) { unsigned char i; disable(); SCLK=0; //Bus ready for (i = 0; i < 8; i++) { SCLK = 0; MOSI = (Byte & 0x80)?1:0; Delay10us(); SCLK = 1; Byte <<= 1; Byte |= MISO; Delay10us(); } SCLK=0; //Bus ready Delay10us(); enable(); return (Byte); } void InitSPI(void) { GPIO_Init(GPIO_SPI,GPIO___SS,GPIO_MODE_OUT_PP); GPIO_Init(GPIO_SPI,GPIO_SCLK,GPIO_MODE_OUT_PP); GPIO_Init(GPIO_SPI,GPIO_MISO,GPIO_MODE_IN_PU ); GPIO_Init(GPIO_SPI,GPIO_MOSI,GPIO_MODE_OUT_PP); _nop_(); /* GPIO_WriteHigh (GPIO_SPI, GPIO___SS); GPIO_WriteLow (GPIO_SPI, GPIO_SCLK); GPIO_WriteHigh (GPIO_SPI, GPIO_MOSI); GPIO_WriteHigh (GPIO_SPI, GPIO_MISO); */ SS = 1; // SCLK = 0; // MOSI = 1; // MISO = 1; // _nop_(); Delay10us(); /* while(1) { SS = !SS; // SCLK = !SCLK; // MOSI = !MOSI; // Delay10us();Delay10us();Delay10us(); } */ } void Delay10us(void) { _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); }