返回该小组首页 回复主题
zhlycn

zhlycn

小组等级:     E币:287  (E币换礼)

#1楼主:AVR的SD卡接口程序

文章发表于:2008-07-02 01:57

SD卡接口程序/************************************************************/
/*           SD Code for   M32L             */
/*       By   pasyong        */
/*       2006-4         */
/*       Base ICC6.31A             */
/************************************************************/
#include <iom32v.h>
#include <macros.h>
#include "1011.h"
#define uchar unsigned char
#define uint unsigned int
#define MMC_CS_PIN     BIT(4)    //PORTB.4
#define MMC_PORT       PORTB   

 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<n; 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);
}

  (转自http://www.fly2web.cn/blog/article.php?type=blog&cid=1&itemid=5

haha0900

   小组等级:    E币:61  (E币换礼)

#2

文章发表于:2008-07-03 21:24

这个是SPI模式的吧?有没有SD模式的?想学习。

l0p0c

   小组等级:    E币:2961  (E币换礼)

#3

文章发表于:2008-07-07 11:39

SD模式首先要看你的处理器支持否?

这样参数有接口和频率

zhlycn

   小组等级:    E币:287  (E币换礼)

#4

文章发表于:2008-07-08 09:22

现在的设计,大都兼容SD模式和SPI模式。SD模式近期将和大家共享,希望各位大侠也把手中的好东东拿来共享。

my2003sky

   小组等级:    E币:79  (E币换礼)

#5

文章发表于:2008-09-11 15:17

SPI的模式下不能进行多口传输吧

czdo2008

   小组等级:    E币:968  (E币换礼)

#6

文章发表于:2008-09-25 08:54

恩,不错我们的产品上用到sd

 

总共 , 当前 /

快速回复主题--如果想加入编辑器功能,建议使用 [高级回复]

您目前还不是小组成员,请先加入

回复贴子区

用户名:    您没有注册?

密码:    忘记了密码?

内容:

  • DesignDesign
  • HTMLHTML

浏览该小组的用户还看过...

设计资源与分销

 

 

We have detected that you do not have the Flash 7 player installed.
Please Click Here to download the Flash plugin.