spi.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:1k
- #include "my_head.h"
- /*********************************************/
- #define MOSI P1_5
- #define MISO P1_6
- #define SPI_SCK P1_7
- //-------------------------
- void Init_spi(void)
- {
- MISO = 1;
- MISO = 1;
- SPI_SCK= 1;
- //SS = 1;
- }
- BYTE KeySpiByte_SendReceive(BYTE sdat)
- {
- BYTE i,rdat=0;//must=0!
- for(i=0;i<8;i++ )
- {
- SPI_SCK = 0; //(0)
- MOSI = (bit)(sdat & 0x80);//(1)
- rdat<<=1; //(2)
- rdat |= MISO; //(3)
- SPI_SCK = 1; //(4)
- sdat<<=1; //(5)
- }
- return(rdat);
- }
- BYTE MemSpiByte_SendReceive(BYTE sdat)
- {
- BYTE i,rdat=0;//must=0!
- for(i=0;i<8;i++ )
- {
- SPI_SCK = 0; //(0)
- MOSI = (bit)(sdat & 0x80);//(1)
- rdat<<=1; //(2)
- _nop_(),_nop_();
- SPI_SCK = 1; //(3)
- _nop_(),_nop_();
- rdat |= MISO; //(4)
- sdat<<=1; //(5)
- }
- return(rdat);
- }