找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 8029|回復(fù): 8
收起左側(cè)

stm32讀取SD卡信息程序,通過(guò)SPI的方式

  [復(fù)制鏈接]
ID:225248 發(fā)表于 2017-8-6 19:00 | 顯示全部樓層 |閱讀模式
這是一個(gè)讀取SD卡信息的程序
所有資料51hei提供下載:
SD程序.rar (2.53 MB, 下載次數(shù): 223)


stm32單片機(jī)源程序如下:
  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卡數(shù)據(jù)緩存區(qū)
  46. int main(void)
  47. {
  48.   u8 tmp;
  49.   u16 mm,nn;
  50.   u32 nummber,nummber_bak;

  51.   //設(shè)置優(yōu)先級(jí)分組:搶占優(yōu)先級(jí)和響應(yīng)優(yōu)先級(jí)各2位
  52.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  53.   //設(shè)置向量表的位置和偏移:在FLASH中偏移為0
  54.   NVIC_SetVectorTable(NVIC_VectTab_FLASH,0);

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


  57.   //檢測(cè)當(dāng)前系統(tǒng)時(shí)鐘SystemCoreClock
  58.   SystemCoreClockUpdate ();



  59.   //檢測(cè)SD卡,失敗則2秒后繼續(xù)檢測(cè)
  60.   while(SD_Init()!=0)
  61.   {
  62.      printf("\r\n未檢測(cè)到SD卡!");
  63.      delay_ms(SystemCoreClock,1000);
  64.      delay_ms(SystemCoreClock,1000);
  65.   }

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

  67.   //邏輯0扇區(qū)的物理扇區(qū)號(hào)
  68.   nummber_bak=SD_GetLogic0();

  69.   while(1)
  70.   {
  71.     RxCounter1=0;
  72.     printf("\r\n請(qǐng)發(fā)送要讀取的邏輯扇區(qū)號(hào)\r\n");

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

  78.         
  79.         //將接收到的字符串轉(zhuǎn)換成數(shù)值
  80.     RxBuffer1[RxCounter1]='\0';
  81.     nummber=atol((const char *)RxBuffer1);

  82.     if(SD_ReadBlock(SD_Buffer,nummber_bak+nummber,512)==0)//讀指定扇區(qū)
  83.                
  84.     printf("\r\n第%d邏輯扇區(qū)數(shù)據(jù):",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測(cè)試程序");           
  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); /*發(fā)送一個(gè)字符函數(shù)*/

  110.   /* Loop until the end of transmission */
  111.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)/*等待發(fā)送完成*/
  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****/
復(fù)制代碼




回復(fù)

使用道具 舉報(bào)

ID:290380 發(fā)表于 2018-5-28 21:41 | 顯示全部樓層
不錯(cuò)哦,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

ID:122107 發(fā)表于 2018-7-3 13:16 | 顯示全部樓層
不錯(cuò)哦,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

ID:138247 發(fā)表于 2018-7-24 15:31 | 顯示全部樓層

不錯(cuò)哦,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

ID:221379 發(fā)表于 2019-3-26 11:14 | 顯示全部樓層
好東西,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

ID:318452 發(fā)表于 2019-7-23 21:51 | 顯示全部樓層
看下學(xué)習(xí)下
回復(fù)

使用道具 舉報(bào)

ID:349782 發(fā)表于 2019-9-25 10:50 | 顯示全部樓層

好東西,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

ID:739683 發(fā)表于 2020-5-11 18:00 | 顯示全部樓層
不錯(cuò),有參考價(jià)值
回復(fù)

使用道具 舉報(bào)

ID:285416 發(fā)表于 2020-7-18 05:51 | 顯示全部樓層
LZ,給個(gè)原圖嘛,
  //使能PA時(shí)鐘,PC時(shí)鐘,SPI1時(shí)鐘和APB2復(fù)用功能時(shí)鐘
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE);

  //關(guān)閉SPI所有相關(guān)器件:SD_CS(SD卡),W25_CS(W25X16),T_CS(觸摸屏),N_CS(RF24L01無(wú)線模塊)
  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);

雖然有說(shuō)明,還是不確定-----.C12 是CS,上面的,C9-10-11,是關(guān)閉一些外設(shè)的,這個(gè)必有的嘛
還有,就是,CLK,和MOMI,對(duì)應(yīng)是,A5和7嘛
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表