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

📄 sd.h

📁 PIC16F689驱动SD卡程序,可以实现对SD的ID读取和内存读取,并且可以通过串口调试精灵看到相应信息
💻 H
字号:

#include"function.h"


//------------------------------------------------------------
// Error define
//-------------------------------------------------------------
#define INIT_CMD0_ERROR     0x01
#define INIT_CMD1_ERROR		0x02
#define WRITE_BLOCK_ERROR	0x03
#define READ_BLOCK_ERROR   	0x04 
bank1 unsigned char get[64];
void int_to_char(unsigned int date,unsigned char *result)
{
char i=0,j=0;
char temp=0;
//temp=date/10000;
//result[0]=temp+0x30;
temp=(date/1000)%10;
result[0]=temp+0x30;
temp=(date/100)%10;
result[1]=temp+0x30;
temp=(date/10)%10;
result[2]=temp+0x30;
temp=(date)%10;
result[3]=temp+0x30;
result[4]='\0';

}

typedef struct MMC_VOLUME_INFO
{ //MMC/SD Card info
  unsigned int  size_MB;
  unsigned char sector_multiply;
  unsigned int  sector_count;
  unsigned char name[6];
} VOLUME_INFO_TYPE; 
typedef struct STORE 
{ 
 unsigned char dat[20]; 
} BUFFER_TYPE; //256 bytes, 128 words

BUFFER_TYPE sectorBuffer; //512 bytes for sector buffer

//--------------------------------------------------------------
bank1 unsigned int  readPos=0;
bank1 unsigned char sectorPos=0;
unsigned char LBA_Opened=0; //Set to 1 when a sector is opened.
unsigned char Init_Flag;    //Set it to 1 when Init is processing.
//---------------------------------------------------------------
void delay1(unsigned int time)
{
 while(time--);
}

//****************************************************************************
// Port Init
void MMC_Port_Init()
//****************************************************************************
{
   SPI_SCL=1;
   SPI_DO =1;
   SPI_CS=1;
   send_s("Port Init!");
/*
   //Config ports 
   SPI_DI=1;          //Set Pin MMC_DI as Input
   SPI_SCL=1;         //Set Pin MMC_Clock as Output
   SPI_DO=1;          //Set Pin MMC_DO as Output
   SPI_CS=1;          //Set MMC_Chip_Select to High,MMC/SD Invalid.
   //busy led port init
   //SPI_BY=1;        //Set spi busy led port output
   //MMC_BUSY_LED=1;                      //busy led off     
   */               
}
void Write_Byte_MMC(unsigned char value)
//****************************************************************************
{ 
   unsigned char i; 
   
   SPI_BY=0; 
   //Software SPI
   for (i=0;i<8;i++) 
   {  //write a byte
      if (((value>>(7-i))&0x01)==0x01) 
	   SPI_DI=1; //Send bit by bit(MSB First)
      else SPI_DI=0;
       SPI_SCL=0; //set Clock Impuls low
      if(Init_Flag) 
	   delay(8); 
      SPI_SCL=1; //set Clock Impuls High
      if(Init_Flag) 
	   delay(8);     
   }//write a byte
   //MMC_BUSY_LED=1;
}
unsigned char Read_Byte_MMC()
//****************************************************************************
{ 
   unsigned char temp=0;
   unsigned char i;

   SPI_BY=0;
   //Software SPI
   for (i=0;i<8;i++) //MSB First
   {
      SPI_DO=1;
      SPI_SCL=0; //Clock Impuls (Low)
      if(Init_Flag)
	   delay(8);
      temp=(temp<<1)+(unsigned char)SPI_DO; //read mmc data out pin 
      SPI_SCL=1; //set Clock Impuls High
      if(Init_Flag) 
	   delay(8);	
   }
   SPI_BY=1;
   return (temp);
}
unsigned char Write_Command_MMC(unsigned char *CMD)
//****************************************************************************
{
   unsigned char tmp;
   unsigned char retry=0;
   unsigned char i;

   //set MMC_Chip_Select to high (MMC/SD-Card disable) 
   SPI_CS=1;
   //send 8 Clock Impulse
   Write_Byte_MMC(0xFF);
   //set MMC_Chip_Select to low (MMC/SD-Card active)
   SPI_CS=0;

   //send 6 Byte Command to MMC/SD-Card
   for (i=0;i<0x06;i++) 
   { 
      Write_Byte_MMC(*CMD++);
   }
   
   //get 16 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_MMC();
      retry++;
   }
   while((tmp==0xff)&&(retry<100)); 
   return(tmp);
}
unsigned char MMC_Init()
//****************************************************************************
{  
   unsigned char retry,temp;
   unsigned char i;
   unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
   send_s("SD CARD Init!");
   MMC_Port_Init(); //Init SPI port  

   delay(200);
   
   Init_Flag=1; //Set the init flag

   for (i=0;i<0x0f;i++) 
   {
      Write_Byte_MMC(0xff); //send 74 clock at least!!!
   }
	
   //Send Command CMD0 to MMC/SD Card
   send_s("Send Command CMD0 to MMC/SD Card");
   retry=0;
   do
   { //retry 200 times to send CMD0 command 
     temp=Write_Command_MMC(CMD);
     retry++;
     if(retry==200) 
     { //time out
       return(INIT_CMD0_ERROR);//CMD0 Error!
     }
   } 
   while(temp!=1);
   
   //Send Command CMD1 to MMC/SD-Card
   send_s("Send Command CMD1 to MMC/SD-Card");
   CMD[0] = 0x41; //Command 1
   CMD[5] = 0xFF;
   retry=0;
   do
   { //retry 100 times to send CMD1 command 
     temp=Write_Command_MMC(CMD);
     retry++;
     if(retry==100) 
     { //time out
       return(INIT_CMD1_ERROR);//CMD1 Error!
     }
   } 
   while(temp!=0);
   
   Init_Flag=0; //Init is completed,clear the flag 
   
   SPI_CS=1;  //set MMC_Chip_Select to high 
   send_s("SD CARD Init Suc!!");
   return(0x55); //All commands have been taken.
} 
unsigned char MMC_Read_Block(unsigned char *CMD,unsigned char *Buffer,unsigned int Bytes)
//****************************************************************************
{  
   unsigned int i; 
   unsigned retry,temp;
    
   //Send Command CMD to MMC/SD-Card
   retry=0;
   do
   {  //Retry 100 times to send command.
      temp=Write_Command_MMC(CMD);
      retry++;
      if(retry==100) 
      {
        return(READ_BLOCK_ERROR); //block write Error!
      }
   }
   while(temp!=0); 
   			
   //Read Start Byte form MMC/SD-Card (FEh/Start Byte)
   while (Read_Byte_MMC()!=0xfe);
	
   //Write blocks(normal 512Bytes) to MMC/SD-Card
   for (i=0;i<Bytes;i++)
   {
      *Buffer++ = Read_Byte_MMC();
   }
   
   //CRC-Byte
   Read_Byte_MMC();//CRC - Byte 
   Read_Byte_MMC();//CRC - Byte
	
   //set MMC_Chip_Select to high (MMC/SD-Card invalid)
   SPI_CS=1;
   return(0);
}
unsigned char Read_CSD_MMC(unsigned char *Buffer)
//***************************************************************************
{	
   //Command for reading CSD Registers
   unsigned char CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
   unsigned char temp;
   temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes

   return(temp);
}
unsigned char Read_CID_MMC(unsigned char *Buffer)
//***************************************************************************
{
   //Command for reading CID Registers
   unsigned char CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF}; 
   unsigned char temp;
   temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes

   return(temp);
}
void MMC_get_volume_info(void)
//****************************************************************************
{   unsigned char i;
    unsigned char c_temp[5];
   VOLUME_INFO_TYPE *vinf,MMC_volume_Info;
    send_s("SD CARD Information Read!!");
   vinf=&MMC_volume_Info; //Init the pointoer;
    Read_CSD_MMC(sectorBuffer.dat);
	vinf->sector_count = sectorBuffer.dat[6] & 0x03;
	vinf->sector_count <<= 8;
	vinf->sector_count += sectorBuffer.dat[7];
	vinf->sector_count <<= 2;
	vinf->sector_count += (sectorBuffer.dat[8] & 0xc0) >> 6;
	vinf->sector_multiply = sectorBuffer.dat[9] & 0x03;
	vinf->sector_multiply <<= 1;
	vinf->sector_multiply += (sectorBuffer.dat[10] & 0x80) >> 7;
	vinf->size_MB = vinf->sector_count >> (9-vinf->sector_multiply);

	Read_CID_MMC(sectorBuffer.dat);

	vinf->name[0]=sectorBuffer.dat[3];
	vinf->name[1] = sectorBuffer.dat[4];
	vinf->name[2] = sectorBuffer.dat[5];
	vinf->name[3] = sectorBuffer.dat[6];
	vinf->name[4] = sectorBuffer.dat[7];
	vinf->name[5] = 0x00; //end flag
    //Print Product name on lcd
    i=0;
    send_s("Product Name:");
	send_s(vinf->name);
    send_s("Tot:"); 
    int_to_char(vinf->size_MB,c_temp);
    //send_s(ftoa(vinf->size_MB,c_temp,0)); 
     send_s(c_temp);
	send_s("MB ");
	
}

⌨️ 快捷键说明

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