1、51 单片机模拟 spi 串行接口程序51 单片机模拟 spi 串行接口程序,在 keilc51 下编写sbit CS=P35; sbit CLK= P15; sbit DataI=P17; sbit DataO=P16; #define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter; for(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;
2、 DataI=0; / writeif(val val=1; CLK=1; if(DataO)val|=1; / read CLK=0; return val; sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;for(BitCounter=8; BiCounter!=0; BitCou
3、nter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;for(BitCounter=8; BiCounter!=
4、0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;for(BitCounter=8; Bi
5、Counter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;for(BitCoun
6、ter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;fo
7、r(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitC
8、ounter;for(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned
9、char BitCounter;for(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char val) unsigned char BitCounter;for(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(valval=1; CLK=1;if(DataO)val|=1; / read CLK=0; return val;