新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > avr的SD卡基本讀寫程序(一)

        avr的SD卡基本讀寫程序(一)

        作者: 時間:2016-11-13 來源:網絡 收藏
        /* SD Code for M32L */

        /* By pasyong */
        /* 2006-4 */
        /* Base ICC6.31A */
        /************************************************************/
        #include
        #include
        #include "1011.h"
        #define uchar unsigned char
        #define uint unsigned int
        #define MMC_CS_PIN BIT(4) //PORTB.4
        #define MMC_PORT PORTB

        本文引用地址:http://www.104case.com/article/201611/316550.htm

        uchar reading=0,a=0,pointer=0;
        void sd_port_init()
        {
        MMC_PORT|=MMC_CS_PIN;
        }
        uchar BUFFER[512]; //扇區緩沖區
        uint i=0;
        void delay_nus(uint n)
        {
        unsigned char b;
        for (b = 1; b;
        }
        //****************************************************************************
        //Send a Command to MMC/SD-Card
        //Return: the second byte of response register of MMC/SD-Card
        //****************************************************************************
        uchar SD_Write_Command(uchar cmd,unsigned long arg)
        {
        uchar tmp;
        uchar retry=0;

        //MMC_PORT|=MMC_CS_PIN; //SD卡關閉
        //send 8 Clock Impulse
        Write_Byte_SPI(0xFF);

        //set MMC_Chip_Select to low (MMC/SD-Card active)
        MMC_PORT&=~MMC_CS_PIN; //SD卡使能

        Write_Byte_SPI(cmd|0x40); //送頭命令
        Write_Byte_SPI(arg>>24);
        Write_Byte_SPI(arg>>16); //send 6 Byte Command to MMC/SD-Card
        Write_Byte_SPI(arg>>8);
        Write_Byte_SPI(arg&0xff);
        Write_Byte_SPI(0x95); //僅僅對RESET有效的CRC效驗碼

        //get 8 bit response
        //Read_Byte_MMC(); //read the first byte,ignore it.
        do
        { //Only last 8 bit is used here.Read it out.
        tmp = Read_Byte_SPI();
        retry++;
        }
        while((tmp==0xff)&&(retry<100)); //當沒有收到有效的命令的時候

        if(reading==0)
        MMC_PORT|=MMC_CS_PIN; //MMC_CS_PIN=1;
        else MMC_PORT&=~MMC_CS_PIN; //MMC_CS_PIN=0;
        return(tmp);
        }
        //****************************************************************************
        //SD卡初始化(SPI-MODE)
        //****************************************************************************
        uchar SD_Init(void)
        {
        uchar retry,temp;
        uchar i;
        MMC_PORT&=~MMC_CS_PIN; //SD卡使能

        delay_nus(250); //Wait MMC/SD ready...
        for (i=0;i<0x0f;i++)
        {
        Write_Byte_SPI(0xff); //send 74 clock at least!!!
        }
        //Send Command CMD0 to MMC/SD Card
        retry=0;

        do
        { //retry 200 times to send CMD0 command
        temp=SD_Write_Command(0,0);
        retry++;
        if(retry==100)
        {
        ;//CMD0 Error!
        }
        }
        while(temp!=1);

        //Send Command CMD1 to MMC/SD-Card
        retry=0;
        do
        { //retry 100 times to send CMD1 command
        temp=SD_Write_Command(1,0);
        retry++;
        if(retry==100)
        {
        ;
        }
        }
        while(temp!=0);
        retry=0;
        SD_Write_Command(16,512); //設置一次讀寫BLOCK的長度為512個字節

        MMC_PORT|=MMC_CS_PIN; //MMC_CS_PIN=1; //set MMC_Chip_Select to high
        return(0); //All commands have been taken.
        }
        //****************************************************************************
        //從SD卡讀一個扇區 Return 0 if no Error.
        //****************************************************************************
        uchar SD_Read_Block(unsigned long address)
        {
        uchar temp=0;uint i=0;
        reading=1;
        temp=SD_Write_Command(17,address); //讀出RESPONSE
        while (Read_Byte_SPI()!= 0xfe)
        {;} //直到讀取到了數據的開始頭0XFE,才繼續
        for(i=0; i<512; i++)
        {
        BUFFER[i]=Read_Byte_SPI();
        }
        Read_Byte_SPI();//CRC - Byte
        Read_Byte_SPI();//CRC - Byte
        reading=0;
        MMC_PORT|=MMC_CS_PIN; //關閉SD卡
        return(temp);
        }



        關鍵詞: avrSD卡讀寫程

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 淮阳县| 青神县| 宝鸡市| 如皋市| 成安县| 乌鲁木齐市| 南康市| 察隅县| 龙泉市| 湖南省| 卓尼县| 精河县| 天镇县| 南充市| 龙里县| 大方县| 甘谷县| 开远市| 孝感市| 辉南县| 扶余县| 青河县| 潜江市| 盘山县| 绥化市| 平乡县| 九寨沟县| 鄄城县| 阿拉善左旗| 行唐县| 博爱县| 喀喇| 克东县| 连平县| 城步| 山西省| 安达市| 绥化市| 陈巴尔虎旗| 桂东县| 石狮市|