一、利用Altium Designer 18绘制原理图
1.Altium Designer 18的安装
百度网盘:https://pan.baidu.com/s/1X-lJdIQpIJselnmpmU2lmA#list/path=%2F 提取码:ygmr
2.添加元件库
百度网盘:https://pan.baidu.com/s/1Ny24NnI5uJRhaaeNimFUGg 提取码:luha
3.stm32最小系统的电路原理图
双击添加到画板开始作图。所以元器件寻找都是这个过程 电源和地端口: 选中元器件后按TAB键可以进行信息修改,按回车修改完成。接下来就可以开始绘图了。 stm32f103c8t6芯片 指示灯 R:Miscellaneous Devices LC.IntLib 元件库中搜索 0805 1k D:Miscellaneous Devices LC.IntLib 元件库中搜索 0603
去耦电路 C:Miscellaneous Devices LC.IntLib 元件库中搜索 0805 100nf 晶振电路 Y:Miscellaneous Devices.IntLib 元件库中搜索 XTAL C:Miscellaneous Devices LC.IntLib 元件库中搜索 0805 22pf 复位电路 S:Miscellaneous Devices.IntLib 元件库中搜索 SW-PB C:Miscellaneous Devices LC.IntLib 元件库中搜索 0805 1uf 降压电路 AMS1117:stm32f103c8t6 最小系统.SchLib 元件库中
下载方式选择及程序烧录接口 P:Miscellaneous Connectors.IntLib 元件库中搜索 MHDR 输入、输出电源 J:base.SchLib 元件库中搜索 USB micro 排针 P:Miscellaneous Connectors.IntLib 元件库中搜索 MHDR1X16
4.STM32+SD卡 的系统原理图绘制
在之前STM32的原理图上进行绘制。 SD卡原理图 AMS1117:stm32f103c8t6 最小系统.SchLib 元件库中 P:Miscellaneous Connectors.IntLib 元件库中搜索 MHDR C:Miscellaneous Devices LC.IntLib 元件库中搜索 0805 100uf STM32+SD卡总览
二、SD卡的数据读取
1.建立工程
配置FATFS: 配置SYS: GPIO口设置: SPI设置: USART1设置:
时钟设置: 生成工程:
2.代码
main.c
#include "main.h"
#include "fatfs.h"
#include "SDdriver.h"
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART1_UART_Init(void);
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, 0xFFFF);
return ch;
}
uint16_t uart_value[3];
uint8_t aRxBuffer1;
void WritetoSD(BYTE write_buff[],uint8_t bufSize);
char SD_FileName[] = "hello.txt";
uint8_t WriteBuffer[] = "01 窝室嫩叠\r\n";
uint8_t write_cnt =0;
void WritetoSD(BYTE write_buff[],uint8_t bufSize)
{
FATFS fs;
FIL file;
uint8_t res=0;
UINT Bw;
res = SD_init();
if(res == 1)
{
printf("SD卡初始化失败! \r\n");
}
else
{
printf("SD卡初始化成功! \r\n");
}
res=f_mount(&fs,"0:",1);
if(res == FR_NO_FILESYSTEM)
{
printf("没有文件系统! \r\n");
res = f_mkfs("", 0, 0);
if(res == FR_OK)
{
printf("格式化成功! \r\n");
res = f_mount(NULL,"0:",1);
res = f_mount(&fs,"0:",1);
if(res == FR_OK)
{
printf("SD卡已经成功挂载,可以进进行文件写入测试!\r\n");
}
}
else
{
printf("格式化失败! \r\n");
}
}
else if(res == FR_OK)
{
printf("挂载成功! \r\n");
}
else
{
printf("挂载失败! \r\n");
}
res = f_open(&file,SD_FileName,FA_OPEN_ALWAYS |FA_WRITE);
if((res & FR_DENIED) == FR_DENIED)
{
printf("卡存储已满,写入失败!\r\n");
}
f_lseek(&file, f_size(&file));
if(res == FR_OK)
{
printf("打开成功/创建文件成功! \r\n");
res = f_write(&file,write_buff,bufSize,&Bw);
if(res == FR_OK)
{
printf("文件写入成功! \r\n");
}
else
{
printf("文件写入失败! \r\n");
}
}
else
{
printf("打开文件失败!\r\n");
}
f_close(&file);
f_mount(NULL,"0:",1);
}
void Get_SDCard_Capacity(void)
{
FRESULT result;
FATFS FS;
FATFS *fs;
DWORD fre_clust,AvailableSize,UsedSize;
uint16_t TotalSpace;
uint8_t res;
res = SD_init();
if(res == 1)
{
printf("SD卡初始化失败! \r\n");
}
else
{
printf("SD卡初始化成功! \r\n");
}
res=f_mount(&FS,"0:",1);
if (res != FR_OK)
{
printf("FileSystem Mounted Failed (%d)\r\n", result);
}
res = f_getfree("0:", &fre_clust, &fs);
if ( res == FR_OK )
{
TotalSpace=(uint16_t)(((fs->n_fatent - 2) * fs->csize ) / 2 /1024);
AvailableSize=(uint16_t)((fre_clust * fs->csize) / 2 /1024);
UsedSize=TotalSpace-AvailableSize;
printf("\r\n%d MB total drive space.\r\n""%d MB available.\r\n""%d MB used.\r\n",TotalSpace, AvailableSize,UsedSize);
}
else
{
printf("Get SDCard Capacity Failed (%d)\r\n", result);
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_FATFS_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1,&aRxBuffer1,1);
printf(" main \r\n");
Get_SDCard_Capacity();
while (1)
{
WritetoSD(WriteBuffer,sizeof(WriteBuffer));
WriteBuffer[0] = WriteBuffer[0] +0;
WriteBuffer[1] = WriteBuffer[1] +1;
write_cnt ++;
while(write_cnt > 5)
{
printf(" while \r\n");
HAL_Delay(500);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = SD_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(SD_CS_GPIO_Port, &GPIO_InitStruct);
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
三、烧录运行
四、总结
奇怪的知识又增加了
五、参考资料
https://www.bilibili.com/video/BV17E411x7dR?p=2 https://www.bilibili.com/video/BV1Ab411K7yp/?spm_id_from=333.788.videocard.0
|