|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2009-5-19 09:26:27
|
显示全部楼层
3、SD_MMC.c文件,它是SD、MMC卡驱动,内容如下:
#ifndef SD_MMC
#define SD_MMC
#include "SD_MMC_DATA.c"
#include "SST89x5x4.H"
void send_byte(unsigned char);
sbit sd_mosi = P1^5;
sbit sd_miso = P1^6;
sbit sd_clk = P1^7;
sbit sd_cs = P1^4;
//读写sd卡时序,高位先发。
unsigned char sd_rw(unsigned char a)
{
SPDR=a; //spi输出
while((SPSR&0x80)==0);
SPSR=0;
return SPDR;
}
//6字节命令
unsigned char sd_cmd(unsigned char *p)
{
unsigned char i,a=0;
sd_cs=1;
sd_rw(0xff);
sd_cs=0;
for(i=0;i<6;i++) sd_rw(*p++);
sd_rw(0xff);
a=sd_rw(0xff);
return a;
}
//mmc/sd 卡初始化
unsigned char sd_init(void)
{
unsigned char cmd[]={0x40,0x00,0x00,0x00,0x00,0x95};
unsigned char i=0;
for(i=0;i<10;i++) sd_rw(0xff);
i=0;
if(sd_cmd(cmd)!=0x01) return 0;
cmd[0]=0x41;cmd[5]=0xff;
while(sd_cmd(cmd)!=0X00) if( (++i)>200 ) return 0;
return 1;
}
//读指定扇区512字节,跳转sd_512data_r()函数处理。
unsigned char sd_read(unsigned long address)
{
unsigned char cmd[]={0x51,0x00,0x00,0x00,0x00,0xff};
address=address<<9;
cmd[1]=(address>>24);
cmd[2]=(address>>16);
cmd[3]=(address>>8);
cmd[4]=(address>>0);
if(sd_cmd(cmd)!=0x00) return 0;
while(sd_rw(0xff)!=0xfe);
sd_512data_r();
sd_rw(0xff);sd_rw(0xff);
return 1;
}
//读指定扇区512字节,跳转sd_512data_r_direct()函数处理。
unsigned char sd_read_direct(unsigned long address)
{
unsigned char cmd[]={0x51,0x00,0x00,0x00,0x00,0xff};
address=address<<9;
cmd[1]=(address>>24);
cmd[2]=(address>>16);
cmd[3]=(address>>8);
cmd[4]=(address>>0);
if(sd_cmd(cmd)!=0x00) return 0;
while(sd_rw(0xff)!=0xfe);
sd_512data_r_direct();
sd_rw(0xff);sd_rw(0xff);
return 1;
}
#endif |
|