#include #include "intrins.h" #include "spi.h" #include "spi_cfg.h" #include "pt_timer.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 ByteIn ) { unsigned char Byte = ByteIn; unsigned char Byte1 = 0; //SPI POL 0 //SPI PHASE 1 unsigned char i; disable(); SCLK=0; Delay10us(); for (i = 0; i < 8; i++) { MOSI = Byte & 0x80?1:0; SCLK = 1; Delay10us(); //-------------------------------------------- SCLK = 0; Byte <<= 1; Byte1 <<= 1; Delay10us(); Byte1 |= MISO; } SCLK = 0; Delay10us(); enable(); return (Byte1); } /* unsigned char spiReadWrite(unsigned char Byte ) { unsigned char i; disable(); SCLK=0; //Bus ready for (i = 0; i < 8; i++) { MOSI = (Byte & 0x80)?1:0; SCLK = 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_HI ); 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(); } */ }