新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 单片机读写SD卡最简单最基本的程序

单片机读写SD卡最简单最基本的程序

作者: 时间:2016-11-25 来源:网络 收藏

  

  void read_data(U8 *buffer)

  {

  U32 i;

  U8 rsp = 0;

  while(!(rsp == 0xfe)) //答应字节的最低为0则代表起始位

  rsp = spi_read_byte();

  for(i = 0;i < BLOCK_LEN; i++) //读一个block的内容,一般为512字节

  buffer = spi_read_byte();

  for(i = 0; i < 2; i++) //读两个CRC校正码

  send_clk();

  send_clk(); //读结束字节

  }

  

  U8 write_data(U8 *buffer)

  {

  U16 rsp = 0, tmp = 0, busy = 0, i = 6;

  spi_rt_mode();

  spi_write_byte(0xfe); //起始位

  for(i = 0; i < 512; i++) //发送512个字节

  spi_write_byte(buffer);

  for(i = 0; i < 2; i++) //发送16位的CRC校正

  spi_write_byte(0xff);

  spi_ro_mode(); //等待答应

  while(!(rsp == 0x1))

  {

  rsp =(U16)spi_read_byte();

  tmp = rsp;

  rsp &= 0x11;

  }

  while(!(busy == 0xff)) //判忙

  {

  busy = spi_read_byte();

  }

  tmp &= 0xe;

  if (tmp == 4)

  return NO_ERR;

  else

  {

  Uart_Printf("writing error!!!");

  return WR_SGL_BLK_ERR;

  }

  }

  

  U8 read_register(U8 len, U8 *buffer)

  {

  U8 rsp = 0xff, i = 0;

  spi_ro_mode();

  while((rsp == 0xff) && (i < 100))

  {

  rsp=spi_read_byte();

  }

  if (i > 99)

  {

  Uart_Printf("ERR in readding register!!!");

  return rsp;

  }

  if (rsp != 0xfe)

  {

  buffer[0] = rsp;

  i = 1;

  }

  else

  i = 0;

  for( ; i < len; i++)

  buffer = spi_read_byte();

  for(i = 0; i < 2; i++ )

  send_clk();

  send_clk();

  return NO_ERR;

  }

  

  

  void send_clk()

  {

  rSIOCON |= (1 << 3); //使能SPI

  while (!(rINTPND & BIT_SIO)); //等待发送完毕

  rI_ISPC|=BIT_SIO; //清除中断标志

  }

  

  void spi_write_byte(U8 dat)

  {

  rSIODAT = dat;

  send_clk(); //SPI发送

  }

  

  U8 spi_read_byte(void)

  {

  send_clk(); //SPI发送

  return rSIODAT;

  }

  

  void spi_port_init()

  {

  rIVTCNT = 0;

  rPCONF = (rPCONF & 0xe3ff) | 0x1B0C00; //除了CLK,茶叶MISO,MOSI外,不改变其他位

  rPUPF |= 0x160; //使能MISO的上拉电阻

  }

  

  #ifndef _SD_CONG

  #define _SD_CONG

  #define BLOCK_LEN (512) //一个block的长度

  #define CMD0 0

  #define CMD1 1 // 读OCR寄存器

  #define CMD9 9 // 读CSD寄存器

  #define CMD10 10 // 读CID寄存器

  #define CMD12 12 // 停止读多块时的数据传输

  #define CMD13 13 // 读 Card_Status 寄存器

  #define CMD16 16 // 设置块的长度

  #define CMD17 17 // 读单块

  #define CMD18 18 // 读多块,直至主机发送CMD12

  #define CMD24 24 // 写单块

  #define CMD25 25 // 写多块

  #define CMD27 27 // 写CSD寄存器

  #define CMD28 28 // Set the write protection bit of the addressed group

  #define CMD29 29 // Clear the write protection bit of the addressed group

  #define CMD30 30 // Ask the card for the status of the write protection bits

  #define CMD32 32 // 设置擦除块的起始地址

  #define CMD33 33 // 设置擦除块的终止地址

  #define CMD38 38 //擦除所选择的块

  #define CMD42 42 // 设置/复位密码或上锁/解锁卡

  #define CMD55 55 // 禁止下一个命令为应用命令

  #define CMD56 56 // 应用命令的通用I/O

  #define CMD58 58 // 读OCR寄存器

  #define CMD59 59 // 使能或禁止

  //错误返回

  #define INIT_FAIL 0

  #define NO_ERR 1

  #define WR_SGL_BLK_ERR 2

  #define GET_INFO_ERR 3

  #define R1 1 //SD卡答应类型,表示一个字节

  #define R2 2 //SD卡答应类型,表示两个字节

  //一下是移植时需修改的内容

  #define SD_desel() rPDATE=0x20; //使能SD卡

  #define SD_sel() rPDATE=0x00; //放开SD卡

  #define spi_high_speed() rSBRDR = 5; //spi高速模式

  #define spi_low_speed() rSBRDR = 99; //spi低速模式

  #define spi_ro_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x0 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //只读模式

  #define spi_rt_mode() rSIOCON = (0x0 << 7) | (0x0 << 6) | (0x1 << 5) | (0x0 << 4) | (0x0 << 3) | (0x0 << 2) | 0x1 //读写模式

  #endif

上一页 1 2 下一页

关键词: 单片机读写SD

评论


技术专区

关闭