在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)讀寫SD卡最簡(jiǎn)單最基本的程序

            單片機(jī)讀寫SD卡最簡(jiǎn)單最基本的程序

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

              

              void read_data(U8 *buffer)

              {

              U32 i;

              U8 rsp = 0;

              while(!(rsp == 0xfe)) //答應(yīng)字節(jié)的最低為0則代表起始位

              rsp = spi_read_byte();

              for(i = 0;i < BLOCK_LEN; i++) //讀一個(gè)block的內(nèi)容,一般為512字節(jié)

              buffer = spi_read_byte();

              for(i = 0; i < 2; i++) //讀兩個(gè)CRC校正碼

              send_clk();

              send_clk(); //讀結(jié)束字節(jié)

              }

              

              U8 write_data(U8 *buffer)

              {

              U16 rsp = 0, tmp = 0, busy = 0, i = 6;

              spi_rt_mode();

              spi_write_byte(0xfe); //起始位

              for(i = 0; i < 512; i++) //發(fā)送512個(gè)字節(jié)

              spi_write_byte(buffer);

              for(i = 0; i < 2; i++) //發(fā)送16位的CRC校正

              spi_write_byte(0xff);

              spi_ro_mode(); //等待答應(yīng)

              while(!(rsp == 0x1))

              {

              rsp =(U16)spi_read_byte();

              tmp = rsp;

              rsp &= 0x11;

              }

              while(!(busy == 0xff)) //判忙

              {

              busy = spi_read_byte();

              }

              tmp &= 0xe;

              if (tmp == 4)

              return NO_ERR;

              else

              {

              Uart_Printf("writing error!!!");

              return WR_SGL_BLK_ERR;

              }

              }

              

              U8 read_register(U8 len, U8 *buffer)

              {

              U8 rsp = 0xff, i = 0;

              spi_ro_mode();

              while((rsp == 0xff) && (i < 100))

              {

              rsp=spi_read_byte();

              }

              if (i > 99)

              {

              Uart_Printf("ERR in readding register!!!");

              return rsp;

              }

              if (rsp != 0xfe)

              {

              buffer[0] = rsp;

              i = 1;

              }

              else

              i = 0;

              for( ; i < len; i++)

              buffer = spi_read_byte();

              for(i = 0; i < 2; i++ )

              send_clk();

              send_clk();

              return NO_ERR;

              }

              

              

              void send_clk()

              {

              rSIOCON |= (1 << 3); //使能SPI

              while (!(rINTPND & BIT_SIO)); //等待發(fā)送完畢

              rI_ISPC|=BIT_SIO; //清除中斷標(biāo)志

              }

              

              void spi_write_byte(U8 dat)

              {

              rSIODAT = dat;

              send_clk(); //SPI發(fā)送

              }

              

              U8 spi_read_byte(void)

              {

              send_clk(); //SPI發(fā)送

              return rSIODAT;

              }

              

              void spi_port_init()

              {

              rIVTCNT = 0;

              rPCONF = (rPCONF & 0xe3ff) | 0x1B0C00; //除了CLK,茶葉MISO,MOSI外,不改變其他位

              rPUPF |= 0x160; //使能MISO的上拉電阻

              }

              

              #ifndef _SD_CONG

              #define _SD_CONG

              #define BLOCK_LEN (512) //一個(gè)block的長(zhǎng)度

              #define CMD0 0

              #define CMD1 1 // 讀OCR寄存器

              #define CMD9 9 // 讀CSD寄存器

              #define CMD10 10 // 讀CID寄存器

              #define CMD12 12 // 停止讀多塊時(shí)的數(shù)據(jù)傳輸

              #define CMD13 13 // 讀 Card_Status 寄存器

              #define CMD16 16 // 設(shè)置塊的長(zhǎng)度

              #define CMD17 17 // 讀單塊

              #define CMD18 18 // 讀多塊,直至主機(jī)發(fā)送CMD12

              #define CMD24 24 // 寫單塊

              #define CMD25 25 // 寫多塊

              #define CMD27 27 // 寫CSD寄存器

              #define CMD28 28 // Set the write protection bit of the addressed group

              #define CMD29 29 // Clear the write protection bit of the addressed group

              #define CMD30 30 // Ask the card for the status of the write protection bits

              #define CMD32 32 // 設(shè)置擦除塊的起始地址

              #define CMD33 33 // 設(shè)置擦除塊的終止地址

              #define CMD38 38 //擦除所選擇的塊

              #define CMD42 42 // 設(shè)置/復(fù)位密碼或上鎖/解鎖卡

              #define CMD55 55 // 禁止下一個(gè)命令為應(yīng)用命令

              #define CMD56 56 // 應(yīng)用命令的通用I/O

              #define CMD58 58 // 讀OCR寄存器

              #define CMD59 59 // 使能或禁止

              //錯(cuò)誤返回

              #define INIT_FAIL 0

              #define NO_ERR 1

              #define WR_SGL_BLK_ERR 2

              #define GET_INFO_ERR 3

              #define R1 1 //SD卡答應(yīng)類型,表示一個(gè)字節(jié)

              #define R2 2 //SD卡答應(yīng)類型,表示兩個(gè)字節(jié)

              //一下是移植時(shí)需修改的內(nèi)容

              #define SD_desel() rPDATE=0x20; //使能SD卡

              #define SD_sel() rPDATE=0x00; //放開SD卡

              #define spi_high_speed() rSBRDR = 5; //spi高速模式

              #define spi_low_speed() rSBRDR = 99; //spi低速模式

              #define spi_ro_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x0 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //只讀模式

              #define spi_rt_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x1 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //讀寫模式

              #endif

            上一頁(yè) 1 2 下一頁(yè)

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

            評(píng)論


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

            關(guān)閉