首页->【AVR从入门到精通】

1494 29

8665276777984.rar   MEGA16读取SD卡程序  下面是程序中的主程序

void main(void)
{
  uint i;
  ulong dir,sector,clust;
  ulong next_clust;
  init_devices();  //设备初始化
  delay(10);
  usart_send_b(0x85);
  usart_send_b(0xea);
 
  SD_init();  //SD卡初始化
  delay(200);
  FAT_init();  //FAT文件系统初始化
  delay(200);
  dir = FAT_find_item(first_dir_clust,FAT_s);//根目录区存放文件夹信息,根据文件夹名字寻找该文件夹的起始簇号
  if(dir == 1)                               //若查找失败,发送0x11,0x12
  {
 usart_send_b(0x11);
 usart_send_b(0x12);
 while(1);
  }
  else                                       //若查找成功,返回文件夹起始簇号
  {
    usart_send_b(0x71);
 usart_send_b(0x72);
 usart_send_b(dir>>24);       //把簇号发送给串口
 usart_send_b(dir>>16);
 usart_send_b(dir>>8);
 usart_send_b(dir);
  }
  usart_send_b(0x31);
  clust = FAT_find_item(dir,s_name1);        //在该文件夹下查找文件名称为name_a的文件起始簇号
  if(clust == 1)                             //查找失败
  {
 usart_send_b(0x15);
 usart_send_b(0x16);
 while(1);
  }
  else                                       //查找成功,返回文件起始簇号,计算第一扇区地址,并返回文件大小
  {
 usart_send_b(clust>>24);       //发送该文件所在的簇号
 usart_send_b(clust>>16);
 usart_send_b(clust>>8);
 usart_send_b(clust);
 usart_send_b(0x55);
 usart_send_b(0x66);
 usart_send_b(file_size>>24);
 usart_send_b(file_size>>16);
 usart_send_b(file_size>>8);
 usart_send_b(file_size);
 usart_send_b(0x77);
 usart_send_b(0x88);
 sector = first_data_sector + (clust-2)*((ulong)sectors_per_clust&0x000000ff);//第一个数据扇区加上偏移扇区
 usart_send_b(sector>>24);
 usart_send_b(sector>>16);
 usart_send_b(sector>>8);
 usart_send_b(sector);
 SD_read_single_block(sector,&buffer[0]);
 for(i=0;i<512;i++)
 {
   usart_send_b(buffer[i]);
 }
 next_clust=FAT_next_clust(clust);    //在FAT1表中找出该文件数据的下一簇
 usart_send_b(next_clust>>24);        //把下一簇号发送给串口
 usart_send_b(next_clust>>16);
 usart_send_b(next_clust>>8);
 usart_send_b(next_clust);
 sector = first_data_sector + (next_clust-2)*((ulong)sectors_per_clust&0x000000ff); //计算扇区的地址
 SD_read_single_block(sector,&buffer[0]);//读出该扇区的数据
 for(i=0;i<512;i++)
 {
   usart_send_b(buffer[i]);  //把数据发送给串口
 }
 for(i=0;i<512;i++)
 {
   buffer[i]=i;  //给buffer赋数据
 }
 SD_write_single_block(sector,buffer);//往刚刚算出的扇区写数据
  }
  usart_send_b(0x32);
  clust = FAT_find_item(dir,s_name2);      // 查找name_b
  if(clust == 1)
  {
 usart_send_b(0x17);
 usart_send_b(0x18);
 while(1);
  }
  else
  {
 usart_send_b(clust>>24);
 usart_send_b(clust>>16);
 usart_send_b(clust>>8);
 usart_send_b(clust);
    usart_send_b(0x55);
 usart_send_b(0x66);
 usart_send_b(file_size>>24);
 usart_send_b(file_size>>16);
 usart_send_b(file_size>>8);
 usart_send_b(file_size);
 usart_send_b(0x77);
 usart_send_b(0x88);
 sector = first_data_sector + (clust-2)*((ulong)sectors_per_clust&0x000000ff);
 usart_send_b(sector>>24);
 usart_send_b(sector>>16);
 usart_send_b(sector>>8);
 usart_send_b(sector);
  }
  while(1)
  {
 NOP();
  }
}

楼主可见

  1. mdc012170 29#
    回复:bangzhu05

    好东西

    看看 

  2. greenhome 28#

    正在找寻相关的资料,顶!

  3. zhangxinbing 27#

    有用么?????????

  4. chgx8888 26#

    顶!不错,先学了!!

  5. chenyear 25#

    学习学习

  6. tingting1954 24#

    看看,谢谢!

  7. wuxiaoping 23#

    henhao 很好

  8. nongguanhai 22#

    :(好东西,学习。。。。。

  9. a7737576 21#

    很好的

  10. happycat125 20#

    谢谢!

  11. zlkasai 19#

    谢谢,正要做这个

  12. brightsky110 18#

    学习了

  13. arrot 17#

    这么抽象

  14. qq278053096 16#

    不错

  15. linsacn 15#

    12333333