⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_sd.c

📁 Atmega64单片机程序(完整工程)
💻 C
字号:

/*--------------File Info-------------------------------------------------------
** 文   件   名:    TEST_SD.c
** 最后修改日期:    2008.04.04
** 版        本:    V1.0
** 描        述:    SD卡测试函数
**------------------------------------------------------------------------------
** Created   by:
** Created date:
*******************************************************************************/
#include "SYS_Config.h" 
#include "SD_Config.h" 
#include "USART1.h" 
#include "SD_Drive.h" 
#include "SD_EEPROM.h" 
#include "TEST_SD.h" 

/********************************************/
//   描述:连接PC机串口,观察超级终端输出
/********************************************/
#if SD_MMC_TEST_EN 
#if USART1_EN 

//SD卡初始化测试函数
void Test_SD_Initialize(void)
{
    INT8U temp ;
    temp=SD_Initialize();

    switch(temp)
    {
        case SD_ERR_NO_CARD :Uart1_SendString("\nPlease insert SD/MMC card!!!");break ;
        case SD_ERR_VOL_NOTSUSP :Uart1_SendString("\nNOT SUSP 3.3V!!!");break ;
        case SD_NO_ERR :Uart1_SendString("\nSD Initialize OK!!!");break ;
        default :Uart1_SendString("\nSD Initialize Fail!!!");break ;
    }
}

//测试SD卡块长度、数量
void Test_SD_GetCardInfo(void)
{
    INT8U rep=0xFF ;
    INT32U temp32 ;
    
    rep=SD_GetCardInfo();
    
    if(rep==SD_NO_ERR)
    {
        Uart1_SendString("\nSD/MMC Card Sector Num: ");
        temp32=sds.block_num ;
        PUT_INT32U_DecTOString(temp32);
        
        Uart1_SendString("\nSD/MMC Card Sector Length (unit:Byte): ");
        temp32=sds.block_len ;
        PUT_INT32U_DecTOString(temp32);
        
        Uart1_SendString("\nSD/MMC Card Content(unit:M): ");
        temp32=sds.block_len*sds.block_num ;
        temp32=temp32>>20 ;
        PUT_INT32U_DecTOString(temp32);
    }
    else 
        Uart1_SendString("\nGet Card's Information Fail!!!");
}

/* 测试擦除SD卡中的块  */
void Test_SD_EraseBlock(void)
{
    INT8U rep=0xFF ;
    INT32U num32 ; //输入块的数量
    INT32U temp32 ;//输入的块起始地址
    
    Uart1_SendString("\nPlease input Start Sector value: ");
    temp32=StringTODec();
   
    Uart1_SendString("\nPlease input Erase Sector Num: ");
	num32=StringTODec();
	
    /* 擦除SD卡中的块  */
    //extern INT8U SD_EraseBlock(INT32U startaddr,INT32U blocknum);
    //如果是启动扇区则不擦
    if((temp32!=133)&&(temp32>33)&&num32>0)
    {
        rep=SD_EraseBlock(temp32,num32);
    }
    
    if(rep==SD_ERR_WRITE_PROTECT)//如果卡写保护
        Uart1_SendString("\nSD Write PROTECT!!!");
    
    if(rep==SD_NO_ERR)
        Uart1_SendString("\nSD Erase Sector Success!!!");
    else 
        Uart1_SendString("\nSD Erase Sector Fail!!!");
}

void Test_SD_ReadBlock(void)
{
    INT8U rep=0xFF ;
    INT16U temp16 ;
    INT32U temp32 ;//输入的第几块
    
    Uart1_SendString("\nPlease input Sector value: ");
	temp32=StringTODec();
	
    rep=SD_ReadBlock(temp32,Data_Buf);
    if(rep==SD_NO_ERR)
    {
        //如果是启动扇区(逻辑扇区0)或者物理扇区0则保存
        if(temp32==133||temp32==0x00)
		    Save_SD_BootSector();
        
        putchar1('\n');
        putchar1('\n');
        for(temp16=0;temp16<SD_BLOCKSIZE;temp16++)
        {
            if(((temp16%32)==0)&&(temp16>0))
                putchar1('\n');
            
            PUT_INT8U_HexTOString(Data_Buf[temp16]);
            //格式转换函数,
            putchar1(' ');
        }
    }
    else 
        Uart1_SendString("\nSD Read Sector Fail!!!");
}

void Test_SD_WriteBlock(void)
{
    INT8U rep=0xFF ;
    INT16U temp16 ;
    INT32U temp32 ;//写入的第几块

    Uart1_SendString("\nPlease input Write Sector value: ");
	temp32=StringTODec();
	
    /* 向SD卡中写入一个块  */
    //extern INT8U SD_WriteBlock(INT32U blockaddr,INT8U *recbuf);
    //如果是启动扇区则不写
    if((temp32!=133)&&(temp32>33))
    {
        Load_SD_BootSector();//装载SD卡启动扇区信息
        rep=SD_WriteBlock(temp32,Data_Buf);
    }
    
    //如果卡写保护
    if(rep==SD_ERR_WRITE_PROTECT)
        Uart1_SendString("\nSD Write PROTECT!!!");

    if(rep==SD_NO_ERR)
    {
        Uart1_SendString("\n\n\nSD Write Data:\n\n");
        for(temp16=0;temp16<SD_BLOCKSIZE;temp16++)
        {
            if(((temp16%32)==0)&&(temp16>0))
                putchar1('\n');
            
            PUT_INT8U_HexTOString(Data_Buf[temp16]);
            //格式转换函数,见USART1.c
            putchar1(' ');
        }
        Uart1_SendString("\n\n\nSD Read Data:\n\n");
        rep=SD_ReadBlock(temp32,Data_Buf);
        for(temp16=0;temp16<SD_BLOCKSIZE;temp16++)
        {
            if(((temp16%32)==0)&&(temp16>0))
                putchar1('\n');
            
            PUT_INT8U_HexTOString(Data_Buf[temp16]);
            //格式转换函数,见USART1.c
            putchar1(' ');
        }
    }
    else 
        Uart1_SendString("\nSD Write Sector Fail!!!");
}

//测试获得SD驱动版本号
void Test_SD_GetSDVer(void)
{
    Uart1_SendString("\nSD Version:V1.0");
}


void *SD_Function_Test[][2]=
{
    (void*)Test_SD_Initialize, "Init Card    ",
    (void*)Test_SD_GetCardInfo,"Sector Length",
    (void*)Test_SD_ReadBlock,  "Read Sector  ",
    (void*)Test_SD_WriteBlock, "Write Sector ",
    (void*)Test_SD_EraseBlock, "Erase Sector ",
    
    (void*)Test_SD_Initialize, "Function5    ",
    (void*)Test_SD_Initialize, "Function6    ",
    (void*)Test_SD_Initialize, "Function7    ",
    (void*)Test_SD_Initialize, "Function8    ",
    (void*)Test_SD_Initialize, "Function9    ",
    (void*)Test_SD_Initialize, "Function10   ",
    (void*)Test_SD_GetSDVer,   "GetSDVersion ",
    0x00,0x00
};

//超级终端测试程序,波特率19200,8M OSC

void SD_MMC_TEST(void)
{
    INT8U temp,temp1,num ;
    
    while(1)
    {
        Uart1_SendString("\n\n********************************************************************************");
        Uart1_SendString("\n*                 Welcome to use SD/MMC's test program  V1.0                   *");
        Uart1_SendString("\n*                          --Designer:JJX--                                    *");
        Uart1_SendString("\n*                          --Date:2008.05.01--                                   *");
        Uart1_SendString("\n*                            Version 1.0                                       *");
        Uart1_SendString("\n*                          Email:jiangjx@ake.com.cn                            *");
        Uart1_SendString("\n*                    UART Config--COM:38400bps,8Bit,NP                         *\n\n\n");
        
        temp=0 ;
        while(1)
        {
            //display menu
            if(temp<10)
            {
                putchar1(' ');
                putchar1(temp+'0');
            }
            else 
            {
                putchar1(temp/10+'0');
                putchar1(temp%10+'0');
            }
            putchar1(':');
            
            Uart1_SendString(SD_Function_Test[temp][1]);putchar1(' ');
            
            temp++;
            if((int)(SD_Function_Test[temp][0])==0)
            {
                putchar1('\n');
                break ;
            }
            if((temp%4)==0)
                putchar1('\n');
        }
        
        Uart1_SendString("\n************************Please Select the function to test? >> ");
   
        temp=0 ;
        temp1=0 ;
        num=0 ;
        //如果不是回车键
        while(temp1!=0x0D)
        {
            temp1=getchar1();
            //如果是ESC键。退出SD卡测试菜单
            if(temp1==0x1B)
            {
			    Uart1_SendString("\nExit SD/MMC's test program\n");
                return ;
            }
            
            if(temp1>='0'&&temp1<='9')
            {
                putchar1(temp1);
                num++;
                switch(num)
                {
                    case 1 :temp=temp1-'0' ;break ;
                    case 2 :
                    {
                        temp=temp*10+temp1-'0' ;
                        num=0 ;
                        putchar1(' ');
                        break ;
                    }
                    default :num=0 ;break ;
                }
            }
        }
        
        if(temp>=0&&(temp<(sizeof(SD_Function_Test)/4-1)))
        {
            Uart1_SendString("\nThe Result:");
            ((void(*)(void))(SD_Function_Test[temp][0]))();
        }
        else 
            Uart1_SendString("\nCMD Error!!!");
    }
}

#endif 
#endif 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -