|
K1:從開(kāi)始位置開(kāi)始寫(xiě)入0x00-0xFF, 0x00-0xFF(512字節(jié))
K2:從第512字節(jié)位置開(kāi)始寫(xiě)入512個(gè)隨機(jī)字節(jié)
K3:讀取前512個(gè)字節(jié)并顯示
K4:讀取后512個(gè)字節(jié)并顯示
操作過(guò)程中可嘗試"熱插撥"MMC卡,觀察運(yùn)行效果
MMC存儲(chǔ)卡仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
單片機(jī)控制MMC存儲(chǔ)卡源程序如下:
- //-----------------------------------------------------------------
- // 名稱: MMC存儲(chǔ)卡測(cè)試
- //-----------------------------------------------------------------
- // 說(shuō)明: 本例運(yùn)行時(shí),按下K1將向MMC卡第0塊寫(xiě)入512個(gè)有序字節(jié),按下K2時(shí)
- // 將向第1塊寫(xiě)入512個(gè)隨機(jī)字節(jié),按下K3與K4時(shí)將分別讀取并通過(guò)
- // 虛擬終端顯示這些字節(jié)數(shù)據(jù).
- //
- //-----------------------------------------------------------------
- #include <reg51.h>
- #include <intrins.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <absacc.h>
- #define INT8U unsigned char
- #define INT16U unsigned int
- #define INT32U unsigned long
- //定義按鍵操作
- #define K1_DOWN() !(P1 & (1<<0))
- #define K2_DOWN() !(P1 & (1<<2))
- #define K3_DOWN() !(P1 & (1<<4))
- #define K4_DOWN() !(P1 & (1<<6))
- //MMC相關(guān)函數(shù)
- extern void OpenSPI();
- extern INT8U MMC_Initialise();
- extern INT8U MMC_Read_Block(INT32U address);
- extern INT8U MMC_Write_Block(INT32U address);
- //串口相關(guān)函數(shù)
- extern void Init_USART();
- extern void PutChar(char c);
- extern void PutStr(char *s);
- INT8U OP = 0; //當(dāng)前按鍵操作代號(hào)
- INT8U ERROR_Flag = 1; //MMC卡操作錯(cuò)誤標(biāo)識(shí)(為1表示正常,為0表示出錯(cuò))
- //-----------------------------------------------------------------
- // 延時(shí)函數(shù)
- //-----------------------------------------------------------------
- void delay_ms(INT16U x) {INT8U t; while(x--) for(t = 0; t<120; t++);}
- //-----------------------------------------------------------------
- // 以十六進(jìn)制形式顯示所讀取的字節(jié)
- //-----------------------------------------------------------------
- void Show_Byte_by_HEX(INT32U Len)
- {
- INT32U i; char s[] = " ";//字符串初始為三個(gè)空格
- for (i = 0; i < Len; i++)
- {
- }
- PutStr("\r--------Finished!--------\r");
- }
- //-----------------------------------------------------------------
- // 向串口輸出一個(gè)字符
- //-----------------------------------------------------------------
- void PutChar(INT8U c) { SBUF = c; while (TI == 0); TI = 0; }
- //------------------------------------------------------------------
- // 串口輸出字符串
- //------------------------------------------------------------------
- void PutStr(char *s) { while (*s) PutChar(*s++); }
- //-----------------------------------------------------------------
- // 串口配置
- //-----------------------------------------------------------------
- void Init_USART()
- {
- }
- //-----------------------------------------------------------------
- // 主程序
- //-----------------------------------------------------------------
- void main()
- {
- INT32U i,j;
- //SPI,USART初始化
- OpenSPI(); Init_USART(); delay_ms(100);
- //初始化MMC
- PutStr("Initialise MMC, Please Waiting.....");
- ERROR_Flag = MMC_Initialise();
- if (ERROR_Flag) PutStr("OK!\r\r"); else PutStr("ERROR!\r\r");
- //提示進(jìn)行K1-K4操作
- PutStr("Plase Press K1,K2,K3 or K4 to Play MMC Test...\r\r");
- //設(shè)置隨機(jī)種子
- srand(300);
- while(1)
- { while (P1 == 0xFF); //未按鍵則等待-------------------------
- if (K1_DOWN()) { delay_ms(10); if (K1_DOWN()) OP = 1; }
- else if (K2_DOWN()) { delay_ms(10); if (K2_DOWN()) OP = 2; }
- else if (K3_DOWN()) { delay_ms(10); if (K3_DOWN()) OP = 3; }
- else if (K4_DOWN()) { delay_ms(10); if (K4_DOWN()) OP = 4; }
- //如果上次MMC出錯(cuò)則重新初始化SPI接口與MMC卡
- if (ERROR_Flag == 0) //------------------------------------
- {
- }
- //根據(jù)按鍵操作代號(hào)分別進(jìn)行操作,因?yàn)樯鲜隹赡艿闹匦鲁跏蓟瘯?huì)耗費(fèi)較多時(shí)間,
- //如果在這里仍用K1~K4的DOWN判斷,按鍵可能已經(jīng)釋放,從而導(dǎo)致判斷失效.
- //因此這里使用的是提前獲取的按鍵操作代號(hào)
- if (OP == 1) //--------------------------------------------
- {
- }
- else if (OP == 2) //---------------------------------------
- {
- }
- else if (OP == 3) //---------------------------------------
- {
- }
- else if (OP == 4) //---------------------------------------
- {
- }
- next: while (P1 != 0xFF); //等待釋放按鍵-----------------
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
50 MMC存儲(chǔ)卡測(cè)試.zip
(33.82 KB, 下載次數(shù): 39)
2019-3-13 19:23 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|