<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          sd卡讀寫(xiě) spi模式

          作者: 時(shí)間:2016-11-29 來(lái)源:網(wǎng)絡(luò) 收藏
           
          (圖一)


          (圖二) /******************************************************************************************************************** 函數(shù)名:unsigned char mmc_command(unsigned char *comm,unsigned char retuen_dat) 功能:向sd卡寫(xiě)命令 返回: 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; } /******************************************************************************************************* 函數(shù)名:void mmc_init(void) 功能:初始化sd卡 返回: 無(wú) *******************************************************************************************************/ 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個(gè)周期 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);//圖二 }

          /******************************************************************************************************* 函數(shù)名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功能:向sd寫(xiě)數(shù)據(jù)(底層) 返回: 無(wú) *******************************************************************************************************/ 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);//開(kāi)始寫(xiě) for(count=0;count> 24); command_dat[2] = (addr >> 16); command_dat[3] = (addr >> 8); command_dat[4] = (addr >> 0); command_dat[5] = 0; write_mmc_dat(command_dat,buffer,512); }
          /******************************************************************************************************* 函數(shù)名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功能:向sd讀數(shù)據(jù)(底層) 返回: 無(wú) *******************************************************************************************************/ 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); } /******************************************************************************************************* 函數(shù)名:void write_mmc_dat(unsigned char *comm,unsigned char *dat,unsigned int len) 功能:讀sd寫(xiě)數(shù)據(jù) 返回: 無(wú) *******************************************************************************************************/ 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的整數(shù)倍 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 寫(xiě)數(shù)據(jù) ***********************************************/ 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 讀數(shù)據(jù) *********************************************/ 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); }
          上行為寫(xiě) 下行為讀 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時(shí)基 start_time0(); //開(kāi)啟定時(shí)器0 Time_Count1_Init(0,2); //用于產(chǎn)生9600串口波特率 start_time1() ; //開(kāi)啟定時(shí)器1 Uart_Config(1); //配置串口 interrupt_Open(); //打開(kāi)中斷 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); }


          關(guān)鍵詞: sd卡讀寫(xiě)spi模

          評(píng)論


          技術(shù)專(zhuān)區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();