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

📄 sd.c

📁 用atmega64+vs1003(音频解码芯片)+SD制成的板子
💻 C
字号:
/************************************************************/
/*		         		SD Code 					        */
/************************************************************/
#include <avr/io.h>
#include "vs1003.h"
#include <avr/delay.h>
#define uchar unsigned char
#define uint unsigned int
#define MMC_CS_PIN     0x02    //PORTF.1
#define MMC_PORT       PORTF   
#define MMC_CS_DDR     DDRF 
uchar reading=0;
void sd_port_init()
{
MMC_CS_DDR |= MMC_CS_PIN;
MMC_PORT|=MMC_CS_PIN;
} 
uchar BUFFER[512];                   //缓冲区
//****************************************************************************
//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_us(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);
}
//****************************************************************************
//从SD卡读一个扇区  Return 0 if no Error.
//****************************************************************************
uchar SD_Read_Block2(unsigned long address,unsigned char *buf)
{ 
   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++)
	{
		buf[i]=Read_Byte_SPI();
	}
   Read_Byte_SPI();//CRC - Byte 
   Read_Byte_SPI();//CRC - Byte
   reading=0; 
   MMC_PORT|=MMC_CS_PIN;        //关闭SD卡
   return(temp);
}
unsigned int SD_Read_Word(unsigned long address,unsigned int offset)
{
	 uchar temp=0;uint i=0;
	 uchar k=0;
   reading=1;
   uchar buf[2]; 
   temp=SD_Write_Command(17,address<<9);     //读出RESPONSE     
   while (Read_Byte_SPI()!= 0xfe)
   {;}  //直到读取到了数据的开始头0XFE,才继续
	 for(i=0;i<offset;i++)  Read_Byte_SPI();
	 for(;i<offset+2;i++)   buf[k++]=Read_Byte_SPI();	
	 for(;i<512;i++) Read_Byte_SPI();
	 Read_Byte_SPI();
	 Read_Byte_SPI();
	 reading=0; 
   MMC_PORT|=MMC_CS_PIN;        //关闭SD卡
 //  SD_Write_Command(12,0);          
   return (*((unsigned int*) &((char*)buf)[0])) ; 
}

unsigned int SD_Read_Dword(unsigned long address,unsigned int offset)
{
   uchar temp=0;uint i=0;
   uchar k=0;
   reading=1;
   uchar buf[4]; 
   temp=SD_Write_Command(17,address<<9);     //读出RESPONSE     
   while (Read_Byte_SPI()!= 0xfe)
   {;}  //直到读取到了数据的开始头0XFE,才继续
	 for(i=0;i<offset;i++)  Read_Byte_SPI();
	 for(;i<offset+2;i++)   buf[k++]=Read_Byte_SPI();	
	 for(;i<512;i++) Read_Byte_SPI();
	 Read_Byte_SPI();
	 Read_Byte_SPI();
	 reading=0; 
   MMC_PORT|=MMC_CS_PIN;        //关闭SD卡
 //  SD_Write_Command(12,0);          
   return (*((unsigned long*) &((char*)buf)[0])) ; 
}



void ReadSD64Byte(unsigned long address,unsigned char *pbuff)
{   
	unsigned char i;
	i=0;reading = 0;
	SD_Write_Command(16,64);     //设置一次读写BLOCK的长度为32个字节
	reading=1;   
		SD_Write_Command(17,address);      
		while (Read_Byte_SPI()!= 0xfe) ;
		for(i=0; i<64; i++){
			pbuff[i]=Read_Byte_SPI();
		}
		Read_Byte_SPI();//CRC - Byte 
		Read_Byte_SPI();//CRC - Byte
		reading=0;
		MMC_PORT|=MMC_CS_PIN;        //关闭SD卡 
	SD_Write_Command(16,512); 
}


⌨️ 快捷键说明

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