找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8017|回复: 8
收起左侧

stm32读取SD卡信息程序,通过SPI的方式

  [复制链接]
ID:225248 发表于 2017-8-6 19:00 | 显示全部楼层 |阅读模式
这是一个读取SD卡信息的程序
所有资料51hei提供下载:
SD程序.rar (2.53 MB, 下载次数: 223)


stm32单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file    USART/Printf/main.c
  4.   * @author  MCD Application Team
  5.   * @version V3.3.0
  6.   * @date    04/16/10
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @copy
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
  19.   */

  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x.h"
  22. #include "delay.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>

  25. #include "sd.h"
  26. #include "usart.h"

  27. /* Private macro -------------------------------------------------------------*/
  28. /* Private variables ---------------------------------------------------------*/
  29. USART_InitTypeDef USART_InitStructure;
  30. GPIO_InitTypeDef GPIO_InitStructure;

  31. /* Private function prototypes -----------------------------------------------*/

  32. #ifdef __GNUC__
  33.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  34.      set to 'Yes') calls __io_putchar() */
  35.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  36. #else
  37.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  38. #endif /* __GNUC__ */
  39.   

  40. /**
  41.   * @brief  Main program
  42.   * @param  None
  43.   * @retval None
  44.   */
  45. u8 SD_Buffer[512];//SD卡数据缓存区
  46. int main(void)
  47. {
  48.   u8 tmp;
  49.   u16 mm,nn;
  50.   u32 nummber,nummber_bak;

  51.   //设置优先级分组:抢占优先级和响应优先级各2位
  52.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  53.   //设置向量表的位置和偏移:在FLASH中偏移为0
  54.   NVIC_SetVectorTable(NVIC_VectTab_FLASH,0);

  55.   //USART1初始化
  56.   USART1_Init();


  57.   //检测当前系统时钟SystemCoreClock
  58.   SystemCoreClockUpdate ();



  59.   //检测SD卡,失败则2秒后继续检测
  60.   while(SD_Init()!=0)
  61.   {
  62.      printf("\r\n未检测到SD卡!");
  63.      delay_ms(SystemCoreClock,1000);
  64.      delay_ms(SystemCoreClock,1000);
  65.   }

  66.   printf("\r\n初始化SD卡成功!\r\n");

  67.   //逻辑0扇区的物理扇区号
  68.   nummber_bak=SD_GetLogic0();

  69.   while(1)
  70.   {
  71.     RxCounter1=0;
  72.     printf("\r\n请发送要读取的逻辑扇区号\r\n");

  73.         do
  74.         {  //等待接收停止100mS
  75.           tmp = RxCounter1;
  76.           delay_ms(SystemCoreClock,100);
  77.     }while(RxCounter1==0 || tmp!=RxCounter1);

  78.         
  79.         //将接收到的字符串转换成数值
  80.     RxBuffer1[RxCounter1]='\0';
  81.     nummber=atol((const char *)RxBuffer1);

  82.     if(SD_ReadBlock(SD_Buffer,nummber_bak+nummber,512)==0)//读指定扇区
  83.                
  84.     printf("\r\n第%d逻辑扇区数据:",nummber);

  85.     for(mm=0;mm<32;mm++)
  86.     {
  87.       printf("\r\n%03xH  ",mm<<4);
  88.                
  89.       for(nn=0;nn<16;nn++)
  90.             printf("%02x ",SD_Buffer[(mm<<3)+nn]);
  91.         }
  92.     printf("\r\n");
  93.   }





  94. //  while (1)
  95. //  {
  96. //          delay_ms(SystemCoreClock,1000);
  97. //        printf("\r\n串口1测试程序");           
  98. //  }
  99. }

  100. /**
  101.   * @brief  Retargets the C library printf function to the USART.
  102.   * @param  None
  103.   * @retval None
  104.   */
  105. PUTCHAR_PROTOTYPE
  106. {
  107.   /* Place your implementation of fputc here */
  108.   /* e.g. write a character to the USART */
  109.   USART_SendData(USART1, (uint8_t) ch); /*发送一个字符函数*/

  110.   /* Loop until the end of transmission */
  111.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)/*等待发送完成*/
  112.   {
  113.   
  114.   }
  115.   return ch;
  116. }

  117. #ifdef  USE_FULL_ASSERT

  118. /**
  119.   * @brief  Reports the name of the source file and the source line number
  120.   *         where the assert_param error has occurred.
  121.   * @param  file: pointer to the source file name
  122.   * @param  line: assert_param error line source number
  123.   * @retval None
  124.   */
  125. void assert_failed(uint8_t* file, uint32_t line)
  126. {
  127.   /* User can add his own implementation to report the file name and line number,
  128.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  129.   /* Infinite loop */
  130.   while (1)
  131.   {
  132.   }
  133. }
  134. #endif

  135. /**
  136.   * @}
  137.   */

  138. /**
  139.   * @}
  140.   */

  141. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
复制代码




回复

使用道具 举报

ID:290380 发表于 2018-5-28 21:41 | 显示全部楼层
不错哦,学习一下
回复

使用道具 举报

ID:122107 发表于 2018-7-3 13:16 | 显示全部楼层
不错哦,学习一下
回复

使用道具 举报

ID:138247 发表于 2018-7-24 15:31 | 显示全部楼层

不错哦,学习一下
回复

使用道具 举报

ID:221379 发表于 2019-3-26 11:14 | 显示全部楼层
好东西,学习一下
回复

使用道具 举报

ID:318452 发表于 2019-7-23 21:51 | 显示全部楼层
看下学习下
回复

使用道具 举报

ID:349782 发表于 2019-9-25 10:50 | 显示全部楼层

好东西,学习一下
回复

使用道具 举报

ID:739683 发表于 2020-5-11 18:00 | 显示全部楼层
不错,有参考价值
回复

使用道具 举报

ID:285416 发表于 2020-7-18 05:51 | 显示全部楼层
LZ,给个原图嘛,
  //使能PA时钟,PC时钟,SPI1时钟和APB2复用功能时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE);

  //关闭SPI所有相关器件:SD_CS(SD卡),W25_CS(W25X16),T_CS(触摸屏),N_CS(RF24L01无线模块)
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_SetBits(GPIOC,GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12);
       
  //使能SPI1引脚(SCK和MOMI)推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
  //使能SPI1引脚(MIMO)上拉输入
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

虽然有说明,还是不确定-----.C12 是CS,上面的,C9-10-11,是关闭一些外设的,这个必有的嘛
还有,就是,CLK,和MOMI,对应是,A5和7嘛
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表