#include <REGX52.H>#define uchar unsigned char#define uint8 unsigned char#define uint unsigned int#define ulong unsigned long// 累加器带进位右移指令 RRC A #define _rrca_() CY = ACC & 0x01 //汇编代码 rrc a// 累加器带进位左移指令 RLC A #define _rlca_() CY = ACC & 0x80//汇编代码 rlc asbit c_SPI_SI = P1^5;sbit c_SPI_SO = P1^6;sbit c_SPI_CLK = P1^7;#define Macro_Set_SI_High() c_SPI_SI = 1#define Macro_Set_SI_Low() c_SPI_SI = 0#define Macro_Set_CLK_High() c_SPI_CLK = 1#define Macro_Set_CLK_Low() c_SPI_CLK = 0/*//----------------标准C语言版-----------------------------------------//可移植性好,易读,易移植uint8 SD_SPI_ReadByte(void){ uchar ACC; uchar ucCount; ACC = 0; Macro_Set_SI_High(); for(ucCount=0; ucCount<8; ucCount++) { ACC <<= 1; Macro_Set_CLK_Low(); Macro_Set_CLK_High(); if(c_SPI_SO) { ACC |= 0x01; } } return(ACC);}void SD_SPI_WriteByte(uint8 ucSenddata) { uchar ucCount; uchar ucMaskcode; ucMaskcode = 0x80; for(ucCount=0; ucCount<8; ucCount++) { Macro_Set_CLK_Low(); if(ucMaskcode & ucSenddata) { Macro_Set_SI_High(); } else { Macro_Set_SI_Low(); } Macro_Set_CLK_High(); ucMaskcode >>= 1; }} *///-------------------------标准优化版SPI读写函数---------//uchar bdata ACC;sbit ACC_0 = ACC^0;sbit ACC_1 = ACC^1;sbit ACC_2 = ACC^2;sbit ACC_3 = ACC^3;sbit ACC_4 = ACC^4;sbit ACC_5 = ACC^5;sbit ACC_6 = ACC^6;sbit ACC_7 = ACC^7;uint8 SD_SPI_ReadByte(void){//初始化SI引脚状态 Macro_Set_SI_High();//bit7 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); // ACC = P1;//bit6 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit5 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit4 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit3 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit2 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit1 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); //bit0 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); CY = c_SPI_SO; _rlca_(); return(ACC);}void SD_SPI_WriteByte(uint8 ucSenddata) { ACC = ucSenddata; //bit7 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_7; Macro_Set_CLK_High();//bit6 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_6; Macro_Set_CLK_High();//bit5 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_5; Macro_Set_CLK_High();//bit4 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_4; Macro_Set_CLK_High();//bit3 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_3; Macro_Set_CLK_High();//bit2 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_2; Macro_Set_CLK_High();//bit1 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_1; Macro_Set_CLK_High();//bit0 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = ACC_0; Macro_Set_CLK_High();} //2楼: 奉上测试程序及测试记录 //void TestSD_SPIFunc(void)void main(void){ uchar ucCount; uchar ucReaddata; ucCount = 0; //开始 for(ucCount=10; ucCount!=0; ucCount--) //xwj改进的写法,机器周期数从502减少到481,节约21 { SD_SPI_WriteByte(0x88); //用C语言实现的写字节机器周期数为188 //用C51的bdata变量,采用位指令直接传送操作,共用机器周期47 //xwj改进的写法,共用机器周期46 } for(ucCount=50; ucCount!=0; ucCount--) //xwj改进的写法,机器周期数从2607减少到2091,节约516 { ucReaddata = SD_SPI_ReadByte(); //用C语言实现的SPI读字节,机器周期为154 //自用C51的bdata变量,采用位指令操作读字节机器周期为49 //xwj改进的写法,共用机器周期40 } ucCount= 88; //结束// SD_read_sector(0,Page_Buf);}