新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機(jī)讀寫SD卡程序

        51單片機(jī)讀寫SD卡程序

        作者: 時間:2016-11-29 來源:網(wǎng)絡(luò) 收藏

        //================================================================
        //往SD卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
        //unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
        unsigned char SdWriteBlock(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;countfor(count=0;count//for(count=0;countfor(;count<512;count++) SdWrite(0);
        //data block sent - now send checksum
        SdWrite(0xff); //兩字節(jié)CRC校驗(yàn), 為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 ERROR");
        return 0;
        }
        if(dataResp==0x05)
        return 1;
        //printf("Invalid data Response token.");
        return 0;
        }
        //printf("Command 0x18 (Write) was not received by the MMC.");
        return 0;
        }
        //=======================================================================
        //從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
        unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
        {
        unsigned int count;
        //Block size is 512 bytes exactly
        //First Lower SS
        //printf("MMC_read_block");
        SD_CS=0;
        //Then send write command
        SdCommand(0x11,address,0xff);
        if(SdResponse()==00)
        {
        //command was a success - now send data
        //start with DATA TOKEN = 0xFE
        while(SdRead()!=0xfe);
        for(count=0;countfor(;count<512;count++) SdRead();
        //data block sent - now send checksum
        SdRead();
        SdRead();
        //Now read in the DATA RESPONSE token
        SD_CS=1;
        SdRead();
        return 1;
        }
        //printf("Command 0x11 (Read) was not received by the MMC.");
        return 0;
        }

        void initbaud(void)
        {
        TMOD=0X20;
        TH1=0XFD;
        TL1=0XFD;
        PCON=0X00;
        TR1=1;
        SCON=0X50;//8位波特可變
        //SCON=0X52;//8位波特可變 TI開中斷
        }
        //============================================================
        //主程序
        main()
        {
        unsigned int i;
        unsigned long AddTemp=331264;//SD卡地址第一個數(shù)據(jù)物理地址初始值,可以用winhex查看,這里是641物理扇區(qū),512x641=328192,根據(jù)實(shí)際SD卡內(nèi)容更改
        delayms(5);
        SdInit(); //SD卡初始化
        while(1)
        {
        SdWriteBlock(AddTemp,512);
        delayms(1000);
        AddTemp=331264;
        SdReadBlock(DATA,AddTemp,512);//每次讀出512字節(jié)放到緩沖區(qū)
        initbaud();
        for(i=0;i<512;i++)
        {
        SBUF=DATA[i];
        while(!TI);
        TI=0;
        delayms(1);
        }
        while(KEY); //等待按鍵按下繼續(xù)執(zhí)行

        }
        }
        可以在串口中看到SD卡中被寫入的數(shù)據(jù),程序在MCU-51/AVR開發(fā)板進(jìn)行了實(shí)驗(yàn)。具體介紹可訪問:
        http://item.taobao.com/item.htm?spm=0.0.0.50.xZjZ87&id=21441052281,http://item.taobao.com/item.htm?spm=1103*oQM.3-5SusJ.h-2Yh1mq&id=14049701171& 這是兩款功能強(qiáng)大的51、AVR開發(fā)板,對初學(xué)者和研發(fā)人員都是不錯的選擇,下一節(jié)給大家講下結(jié)合SD卡的TFT真彩屏的相關(guān)讀寫!




        上一頁 1 2 下一頁

        關(guān)鍵詞: 51單片機(jī)讀寫SD

        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 巫山县| 怀安县| 襄城县| 东阳市| 潞西市| 镇原县| 屯留县| 台东县| 乳源| 萨迦县| 雷山县| 宜昌市| 龙海市| 泰顺县| 阿尔山市| 阿荣旗| 合阳县| 绍兴县| 弥勒县| 灌云县| 东港市| 平顶山市| 扎鲁特旗| 龙川县| 称多县| 迁西县| 嵊泗县| 新泰市| 荃湾区| 澎湖县| 页游| 蓝山县| 泽库县| 启东市| 贵溪市| 龙游县| 巴林左旗| 连南| 城口县| 奉新县| 临安市|