(图一)
(图二) /******************************************************************************************************************** 函数名:unsigned char mmc_command(unsigned char *comm,unsigned char retuen_dat) 功用:向sd卡写指令 回来: 1- >操作失利 0->成功 ********************************************************************************************************************/ unsigned char mmc_command(unsigned char *comm,unsigned char retuen_dat) { unsigned char n,temp,count,n_count; count = 0; do { SSEL_D(0); for(n=0;n<6;n++) SPI_Send_Dat(*comm++); for(n_count=0;n_count<5;n_count++) { temp = SPI_Receiver_Dat(); if(temp == retuen_dat)break; } SSEL_D(1); SPI_Send_Dat(0xff); if(count>100) return 1; }while(temp!=retuen_dat); return 0; } /******************************************************************************************************* 函数名:void mmc_init(void) 功用:初始化sd卡 回来: 无 *******************************************************************************************************/ void mmc_init(void) { unsigned char command_dat[6]; unsigned char n; for(n=0;n<6;n++) command_dat[n] = 0; SPI_Init(); for(n=0;n<16;n++) SPI_Send_Dat(0xff);//图一 同步用至少74个周期 command_dat[0] = 0x40; command_dat[5] = 0x95; mmc_command(command_dat,0x01);//图二 command_dat[0] = 0x41; command_dat[5] = 0xff; mmc_command(command_dat,0x00);//图二 }
/******************************************************************************************************* 函数名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功用:向sd写数据(底层) 回来: 无 *******************************************************************************************************/ void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) { unsigned char n,temp,n_count;//,count; unsigned int count; count = 0; for(count=0;count<5;count++) { SSEL_D(1); SPI_Send_Dat(0xff); SSEL_D(0); for(n=0;n<6;n++) SPI_Send_Dat(*comm++); for(n_count=0;n_count<5;n_count++) { temp = SPI_Receiver_Dat(); if(temp == 0)break; } if(temp == 0)break; } SPI_Send_Dat(0xff); SPI_Send_Dat(0xff); SPI_Send_Dat(0xfE);//开端写 for(count=0;count
/******************************************************************************************************* 函数名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功用:向sd读数据(底层) 回来: 无 *******************************************************************************************************/ void read_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) { unsigned char n,temp,count,n_count; count = 0; for(count=0;count<5;count++) { SSEL_D(1); SPI_Send_Dat(0xff); SSEL_D(0); for(n=0;n<6;n++) SPI_Send_Dat(*comm++); for(n_count=0;n_count<5;n_count++) { temp = SPI_Receiver_Dat(); if(temp == 0)break; } if(temp == 0)break; } for(n_count=0;n_count<50;n_count++) { temp = SPI_Receiver_Dat(); if(temp == 0xfe)break; } while(len--) { *dat = SPI_Receiver_Dat(); dat++; } SPI_Send_Dat(0xff); SPI_Send_Dat(0xff); SSEL_D(1); SPI_Send_Dat(0xff); } /******************************************************************************************************* 函数名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功用:读sd写数据 回来: 无 *******************************************************************************************************/ void MMCRdBolckOne(unsigned long addr,unsigned char *buffer,unsigned int len) { unsigned char command_dat[6]; command_dat[0] = 0x40 + 17; // READ SINGLE BLOCK addr <<= 9; // addr*512,取512的整数倍 command_dat[1] = (addr >> 24); command_dat[2] = (addr >> 16); command_dat[3] = (addr >> 8); command_dat[4] = (addr >> 0); command_dat[5] = 0; read_mmc_dat(command_dat,buffer,len); } #define SCK_IO(x) (x?(P1=0X0f):(P1=0X0f))//1->输出 0->输入 #define MOSI_IO(x) (x?(P1=0X0f):(P1=0X0f)) #define MISO_IO(x) (x?(P1=0X0f):(P1=0X0f)) #define SSEL_IO(x) (x?(P1=0X0f):(P1=0X0f)) #define SCK_D(x) (x?(P1|=0X01):(P1&=0XFE)) #define MOSI_D(x) (x?(P1|=0X02):(P1&=0XFD)) #define SSEL_D(X) (X?(P1|=0X08):(P1&=0XF7)) #define MISO_I() (P1&0X04) /********************************************** 形式零 spi 写数据 ***********************************************/ void SPI_Send_Dat(unsigned char dat) { unsigned char n; for(n=0;n<8;n++) { SCK_D(0); if(dat&0x80)MOSI_D(1); else MOSI_D(0); dat<<=1; SCK_D(1); } SCK_D(0); } /********************************************* 形式零 spi 读数据 *********************************************/ unsigned char SPI_Receiver_Dat(void) { unsigned char n ,dat,bit_t; for(n=0;n<8;n++) { SCK_D(0); dat<<=1; if(MISO_I())dat|=0x01; else dat&=0xfe; SCK_D(1); } SCK_D(0); return dat; } void SPI_Init(void) { SCK_IO(1) ; MOSI_IO(1) ; MISO_IO(0) ; SSEL_IO(1) ; SSEL_D(1); MOSI_D(1); SCK_D(0); }
上行为写 下行为读 code unsigned char leab[512] = {0x55,0xaa,2,3,4,5,6,7,8,9}; unsigned char rmmc[16]; main() { unsigned char i; Time_Count0_Init(0 ,1); //10ms时基 start_time0(); //敞开定时器0 Time_Count1_Init(0,2); //用于发生9600串口波特率 start_time1() ; //敞开定时器1 Uart_Config(1); //装备串口 interrupt_Open(); //翻开中止 mmc_init(); MMCWdBolckOne(0,leab); for(i=0;i<16;i++) Send_Data(leab[i]); MMCRdBolckOne(0x00,rmmc,10); for(i=0;i<16;i++) Send_Data(rmmc[i]); while (1); }