最近在调试我设计的MP3,用到SPI读写SD卡和向VS1003B发送数据,因为MP3对数据流速度相当敏感,遂对软件SPI进行了优化,目前已能流畅播放了。 我的SPI总线单字节读写函数如下,自认为速度最快了,已至极致,纵然汇编实现,也只能打个平手,51高手请指正。
#include "..incincludes.h"
sbit 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 ucReadData; uchar ucCount;
ucReadData = 0; Macro_Set_SI_High();
for(ucCount=0; ucCount<8; ucCount++) { ucReadData <<= 1; Macro_Set_CLK_Low();
Macro_Set_CLK_High(); if(c_SPI_SO) { ucReadData |= 0x01; } }
return(ucReadData); }
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 ucReadData; sbit ReadData_Bit0 = ucReadData^0; sbit ReadData_Bit1 = ucReadData^1; sbit ReadData_Bit2 = ucReadData^2; sbit ReadData_Bit3 = ucReadData^3; sbit ReadData_Bit4 = ucReadData^4; sbit ReadData_Bit5 = ucReadData^5; sbit ReadData_Bit6 = ucReadData^6; sbit ReadData_Bit7 = ucReadData^7;
uchar bdata ucWriteData; sbit WriteData_Bit0 = ucWriteData^0; sbit WriteData_Bit1 = ucWriteData^1; sbit WriteData_Bit2 = ucWriteData^2; sbit WriteData_Bit3 = ucWriteData^3; sbit WriteData_Bit4 = ucWriteData^4; sbit WriteData_Bit5 = ucWriteData^5; sbit WriteData_Bit6 = ucWriteData^6; sbit WriteData_Bit7 = ucWriteData^7;
uint8 SD_SPI_ReadByte(void) { //初始化SI引脚状态 Macro_Set_SI_High();
//Bit7 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit7 = c_SPI_SO;
//Bit6 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit6 = c_SPI_SO;
//Bit5 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit5 = c_SPI_SO;
//Bit4 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit4 = c_SPI_SO;
//Bit3 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit3 = c_SPI_SO;
//Bit2 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit2 = c_SPI_SO;
//Bit1 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit1 = c_SPI_SO;
//Bit0 Shift Out Macro_Set_CLK_Low(); Macro_Set_CLK_High(); ReadData_Bit0 = c_SPI_SO;
return(ucReadData); }
void SD_SPI_WriteByte(uint8 ucSendData) { ucWriteData = ucSendData; //Bit7 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit7; Macro_Set_CLK_High();
//Bit6 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit6; Macro_Set_CLK_High(); //Bit5 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit5; Macro_Set_CLK_High(); //Bit4 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit4; Macro_Set_CLK_High(); //Bit3 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit3; Macro_Set_CLK_High(); //Bit2 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit2; Macro_Set_CLK_High(); //Bit1 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit1; Macro_Set_CLK_High(); //Bit0 Shift Out To SD Card Macro_Set_CLK_Low(); c_SPI_SI = WriteData_Bit0; Macro_Set_CLK_High(); }
|