当前位置:   article > 正文

stm32f103c8t6用spi协议操作sd卡并给电脑串口发送消息_c8t6+spi sd+fatfs

c8t6+spi sd+fatfs

stm32cubemx设计图如下:

接线如下:

SD_CARDstm32f103核心板
CSA4
SCKA5
MOSIA7
MISOA6

main.c代码如下:

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "fatfs.h"
  22. #include "spi.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "stdio.h"
  28. #include "string.h"
  29. //SPI_HandleTypeDef hspi1;
  30. FATFS fs;
  31. FATFS *pfs;
  32. FIL fil;
  33. FRESULT fres;
  34. DWORD fre_clust;
  35. DIR dir;
  36. static FILINFO fno;
  37. uint32_t total, freez;
  38. char buffer[100];
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN PV */
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. /* USER CODE BEGIN PFP */
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. /* USER CODE END 0 */
  59. /**
  60. * @brief The application entry point.
  61. * @retval int
  62. */
  63. int main(void)
  64. {
  65. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /* MCU Configuration--------------------------------------------------------*/
  68. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  69. HAL_Init();
  70. /* USER CODE BEGIN Init */
  71. /* USER CODE END Init */
  72. /* Configure the system clock */
  73. SystemClock_Config();
  74. /* USER CODE BEGIN SysInit */
  75. /* USER CODE END SysInit */
  76. /* Initialize all configured peripherals */
  77. MX_GPIO_Init();
  78. MX_SPI1_Init();
  79. MX_FATFS_Init();
  80. MX_USART1_UART_Init();
  81. /* USER CODE BEGIN 2 */
  82. /* Mount SD Card */
  83. if(f_mount(&fs, "", 0) != FR_OK)
  84. printf("挂载失败 \r\n");
  85. /* Open file to write */
  86. if(f_open(&fil, "test1.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) != FR_OK)
  87. printf("打开文件失败 \r\n");
  88. /* Check free space */
  89. if(f_getfree("", &fre_clust, &pfs) != FR_OK)
  90. printf("计算空间失败 \r\n");
  91. total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
  92. freez = (uint32_t)(fre_clust * pfs->csize * 0.5);
  93. /* Free space is less than 1kb */
  94. if(freez < 1)
  95. printf("SD满了 \r\n");
  96. /* Writing text */
  97. f_puts("hello world \n", &fil);
  98. f_puts("SD卡获取根目录文件成功!!!", &fil);
  99. /* Close file */
  100. if(f_close(&fil) != FR_OK)
  101. printf("关闭文件失败 \r\n");
  102. /* Open file to read */
  103. if(f_open(&fil, "test1.txt", FA_READ) != FR_OK)
  104. printf("打开文件失败 \r\n");
  105. while(f_gets(buffer, sizeof(buffer), &fil))
  106. {
  107. printf("%s \r\n", buffer);
  108. }
  109. /* Close file */
  110. if(f_close(&fil) != FR_OK)
  111. printf("关闭文件失败 \r\n");
  112. /* Get tree list */
  113. FRESULT res;
  114. if(f_opendir(&dir,"") != FR_OK){
  115. printf("打开根目录失败 \r\n");
  116. }else{
  117. printf("打开根目录成功 \r\n");
  118. for(;;){
  119. res = f_readdir(&dir, &fno); //读取目录,返回状态 和 文件信息的指针
  120. if(res != FR_OK || fno.fname[0] == 0)
  121. break; //若打开失败 或 到结尾,则退出
  122. if(fno.fattrib & AM_DIR) //是目录
  123. {
  124. printf("是目录%s \r\n",fno.fname);
  125. if(res != FR_OK) break; //打开失败则退出
  126. }else
  127. {
  128. printf("是文件:%s \r\n",fno.fname); //是文件
  129. }
  130. }
  131. }
  132. /* Unmount SDCARD */
  133. if(f_mount(NULL, "", 1) != FR_OK)
  134. printf("解除挂载文件失败 \r\n");
  135. /* USER CODE END 2 */
  136. /* Infinite loop */
  137. /* USER CODE BEGIN WHILE */
  138. while (1)
  139. {
  140. /* USER CODE END WHILE */
  141. /* USER CODE BEGIN 3 */
  142. printf("test123123 \r\n");
  143. HAL_Delay(2000);
  144. }
  145. /* USER CODE END 3 */
  146. }
  147. /**
  148. * @brief System Clock Configuration
  149. * @retval None
  150. */
  151. void SystemClock_Config(void)
  152. {
  153. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  154. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  155. /** Initializes the RCC Oscillators according to the specified parameters
  156. * in the RCC_OscInitTypeDef structure.
  157. */
  158. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  159. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  160. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  161. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  162. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  163. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  164. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  165. {
  166. Error_Handler();
  167. }
  168. /** Initializes the CPU, AHB and APB buses clocks
  169. */
  170. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  171. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  172. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  173. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  174. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  175. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  176. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  177. {
  178. Error_Handler();
  179. }
  180. }
  181. /* USER CODE BEGIN 4 */
  182. /* USER CODE END 4 */
  183. /**
  184. * @brief This function is executed in case of error occurrence.
  185. * @retval None
  186. */
  187. void Error_Handler(void)
  188. {
  189. /* USER CODE BEGIN Error_Handler_Debug */
  190. /* User can add his own implementation to report the HAL error return state */
  191. __disable_irq();
  192. while (1)
  193. {
  194. }
  195. /* USER CODE END Error_Handler_Debug */
  196. }
  197. #ifdef USE_FULL_ASSERT
  198. /**
  199. * @brief Reports the name of the source file and the source line number
  200. * where the assert_param error has occurred.
  201. * @param file: pointer to the source file name
  202. * @param line: assert_param error line source number
  203. * @retval None
  204. */
  205. void assert_failed(uint8_t *file, uint32_t line)
  206. {
  207. /* USER CODE BEGIN 6 */
  208. /* User can add his own implementation to report the file name and line number,
  209. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  210. /* USER CODE END 6 */
  211. }
  212. #endif /* USE_FULL_ASSERT */

下载地址:【免费】stm32f103c8t6用spi协议操作sd卡并给电脑串口发送消息资源-CSDN文库

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/659626
推荐阅读
相关标签
  

闽ICP备14008679号