历史上的今天
今天是:2024年12月31日(星期二)
2021年12月31日 | 基于51单片机的SD卡读卡器MMC存储器
2021-12-31 来源:eefocus
硬件设计
程序设计
#include #define F_OSC 11059200//晶振平率Hz #define F_BAUD 9600 #define RELOAD 256-F_OSC/12/32/F_BAUD #define CR 0x0D //回车 //定义SD卡需要的4根信号线 sbit SD_CLK = P1^4; sbit SD_DI = P1^6; sbit SD_DO = P1^5; sbit SD_CS = P1^7; unsigned char xdata DATA[512]; //定义512字节缓冲区,注意需要使用 xdata关键字 //=========================================================== //写一字节到SD卡,模拟SPI总线方式 void SdWrite(unsigned char n) { unsigned char i; for(i=8;i;i--) { SD_CLK=0; SD_DI=(n&0x80); n<<=1; SD_CLK=1; } SD_DI=1; } //=========================================================== //从SD卡读一字节,模拟SPI总线方式 unsigned char SdRead() { unsigned char n,i; for(i=8;i;i--) { SD_CLK=0; SD_CLK=1; n<<=1; if(SD_DO) n|=1; } return n; } //============================================================ //检测SD卡的响应 unsigned char SdResponse() { unsigned char i=0,response; while(i<=8) { response = SdRead(); if(response==0x00) break; if(response==0x01) break; i++; } return response; } //================================================================ //发命令到SD卡 void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC) { SdWrite(command|0x40); SdWrite(((unsigned char *)&argument)[0]); SdWrite(((unsigned char *)&argument)[1]); SdWrite(((unsigned char *)&argument)[2]); SdWrite(((unsigned char *)&argument)[3]); SdWrite(CRC); } //================================================================ //初始化SD卡 unsigned char SdInit(void) { int delay=0, trials=0; unsigned char i; unsigned char response=0x01; SD_CS=1; for(i=0;i<=9;i++) SdWrite(0xff); SD_CS=0; //Send Command 0 to put MMC in SPI mode SdCommand(0x00,0,0x95); response=SdResponse(); if(response!=0x01) { return 0; } while(response==0x01) { SD_CS=1; SdWrite(0xff); SD_CS=0; SdCommand(0x01,0x00ffc000,0xff); response=SdResponse(); } SD_CS=1; SdWrite(0xff); return 1; } //================================================================ //往SD卡指定地址写数据,一次最多512字节 unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len) { unsigned int count; unsigned char dataResp; //Block size is 512 bytes exactly //First Lower SS SD_CS=0; //Then send write command SdCommand(0x18,address,0xff); if(SdResponse()==00) { SdWrite(0xff); SdWrite(0xff); SdWrite(0xff); //command was a success - now send data //start with DATA TOKEN = 0xFE SdWrite(0xfe); //now send data for(count=0;count for(;count<512;count++) SdWrite(0); //data block sent - now send checksum SdWrite(0xff); //两字节CRC校验, 为0XFFFF 表示不考虑CRC SdWrite(0xff); //Now read in the DATA RESPONSE token dataResp=SdRead(); //Following the DATA RESPONSE token //are a number of BUSY bytes //a zero byte indicates the MMC is busy while(SdRead()==0); dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token SD_CS=1; SdWrite(0xff); if(dataResp==0x0b) { //printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERRORn"); return 0; } if(dataResp==0x05) return 1; //printf("Invalid data Response token.n"); return 0; } //printf("Command 0x18 (Write) was not received by the MMC.n"); return 0; } 文件仅供参考 链接:https://pan.baidu.com/s/1h3ZnGUPP5o_8_NsVMwVQuA 提取码:4ebs
下一篇:基于51单片机的智能温控风扇
史海拾趣
|
大家好,请问谁用过MAX3420的USB芯片呢?有问题请教。 大家好,请问谁用过MAX3420的USB芯片呢?有问题请教。 以往的USB芯片没有安装驱动时候直接连接到计算机上会提示发现新硬件,我手里这个MAX3420芯片连接到主机后,什么提示都没有,但也不应该是芯片坏了。在设备管理器中也看不见这个设备,好奇怪, ...… 查看全部问答> |
|
wince5.0 S3c2440官方bsp,带camera驱动 wince5.0 S3c2440官方bsp,带camera驱动: http://www.itxxh.cn/book/2440/zaxsw1565.shtml WINCE5.0系统下,OV9650 CAMERA驱动程序: http://www.itxxh.cn/book/2440/zaxsw1568.shtml… 查看全部问答> |
|
LED光源的技术日趋成熟,每瓦发光流明迅速增长,促使其逐年递减降价。以1W LED光源为例,2008年春的价格已是2006年春的价格三分之一,2009年春将降至2006年的四分之一。 LED绿色灯具的海量市场和持续稳定数年增长需求将是集成电路 行业继V ...… 查看全部问答> |
|
Bit-banding 又称为:极细微的位处理操作, 精确的位操作,位别名区。 为了减少读-修改-写(RMW)操作的时间,ARM在Cortex-M3处理器中引入了bit-banding技术。在bit-banding使能的处理器中,存储器映射的特定区域(SRAM和外设区)能够使用地址别 ...… 查看全部问答> |





