介绍了SD卡的基本结构和技术特征,详细说明了使用MSP430单片机和SD卡设计的FAT16文件系统,给出了接口电路和相应的软件系统设计。
该系统适用于大容量的现场数据采集、存储,并在动态心电图记录系统中得到了应用。
此工程有SCH,程序等。。。。。
########################################################################
以下是SCH图:
###########################################################################
以下是代码,对于MSP430开发主要是多做项目,希望这个项目对大家有用。。。。。
主函数如下:
void main(void)
{
hwInterface sdNow;
uint8 ok,i;
uint8 Name[12]="SD_FAT16TXT";
uint8 buffer[54]=
"If you look this content,the file was created success!";
uint8 rBuf[128];
initMcu();
UCTL1 |= SWRST; // Initialize USART state machine
UCTL1 |= CHAR + SYNC + MM; // 8-bit SPI Master **SWRST**
UTCTL1 = CKPH + SSEL1 + SSEL0 + STC; // SMCLK, 3-pin mode
UBR01 = 0x04; // UCLK/4
UBR11 = 0x00; // 0
UMCTL1 = 0x00; // no modulation
ME2 |= USPIE1; // Enable USART1 SPI mode
UCTL1 &= ~SWRST; // Initialize USART state machine
P5DIR |= SDCS; //SD cs pin as output
P5SEL |= 0x0E; // P5.1-3 SPI option select
sdNow.sectorCount = 1;
sd_Init(&sdNow);
InitFat16();
// memset(rBuf,0,128);
// sd_readSector(&sdNow,0,rBuf,128);
// rBuf[1] = 0;
ok=CreateFile(Name,(uint32)54); //建立长度为54的空文件
if(ok!=SD_FAIL) //文件已存在,退出
{
WriteFile(Name,(uint32)0,(uint32)54,buffer); //将buffer写入文件
};
/*
for(i=0;i<54;i++)buffer=0;
ReadFile(Name,(uint32)0,(uint32)54,buffer); //将文件读入buffer
EreaseFile(Name); //删除文件
*/
while(1);
}
SD卡读程序如下 :
int8 sd_readSector(hwInterface *iface,uint32 address, uint8* buf, uint16 len)
{
uint8 cardresp;
uint8 firstblock;
uint8 c;
uint16 fb_timeout=0xffff;
uint32 i;
uint32 place;
/*DBG((TXT("sd_readSector::Trying to read sector %u and store it at %p.\n"),address,&buf[0]));*/
place=512*address;
sd_Command(iface,CMDREAD, (uint16) (place >> 16), (uint16) place);
cardresp=sd_Resp8b(iface); /* Card response */
/* Wait for startblock */
do
firstblock=sd_Resp8b(iface);
while(firstblock==0xff && fb_timeout--);
if(cardresp!=0x00 || firstblock!=0xfe){
sd_Resp8bError(iface,firstblock);
return(-1);
}
for(i=0;i<512;i++){
c = if_spiSend(iface,0xff);
if(i<len)
buf = c;
}
/* Checksum (2 byte) - ignore for now */
if_spiSend(iface,0xff);
if_spiSend(iface,0xff);
return(0);
}
SD写程序如下:
int8 sd_writeSector(hwInterface *iface,uint32 address, uint8* buf)
{
uint32 place;
uint16 i;
uint16 t=0;
/*DBG((TXT("Trying to write %u to sector %u.\n"),(void *)&buf,address));*/
place=512*address;
sd_Command(iface,CMDWRITE, (uint16) (place >> 16), (uint16) place);
sd_Resp8b(iface); /* Card response */
if_spiSend(iface,0xfe); /* Start block */
for(i=0;i<512;i++)
if_spiSend(iface,buf); /* Send data */
if_spiSend(iface,0xff); /* Checksum part 1 */
if_spiSend(iface,0xff); /* Checksum part 2 */
if_spiSend(iface,0xff);
while(if_spiSend(iface,0xff)!=0xff){
t++;
/* Removed NOP */
}
/*DBG((TXT("Nopp'ed %u times.\n"),t));*/
return(0);
}