开发环境安装
1.点击mdk_xxx.exe,安装MDK软件; 2.点击Keil.STM32FXxx_DFP.x.x.x.pack,安装芯片支持包
仿真器连接
连线方式 主控板程序烧写接口如下图: ST-Link-V2各引脚定义:
SWCLK--------------------------->SWCLK
SWDIO--------------------------->SWDIO
VCC --------------------------->VCC
GND --------------------------->GND
Keil配置 USB通讯错误解决
时钟配置
STM32F4时钟树 时钟初始化代码
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
GPIO配置
端口结构 施密特触发器 GPIO各种模式 此处部分gpio主要用作状态指示 gpio.h
#include "sys.h"
#define vbat_led1_GPIO_Port GPIOE
#define vbat_led2_GPIO_Port GPIOE
#define vbat_led3_GPIO_Port GPIOE
#define running_status_led_GPIO_Port GPIOF
#define vbat_led1_Pin GPIO_PIN_13
#define vbat_led2_Pin GPIO_PIN_14
#define vbat_led3_Pin GPIO_PIN_15
#define running_status_led0_Pin GPIO_PIN_9
#define LED0 PFout(9)
void GPIO_Init(void);
gpio.c
#include "gpio.h"
void GPIO_Init()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
HAL_GPIO_WritePin(vbat_led1_GPIO_Port,vbat_led1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(vbat_led2_GPIO_Port,vbat_led2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(vbat_led3_GPIO_Port,vbat_led3_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = vbat_led1_Pin|vbat_led2_Pin|vbat_led3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.Pin = running_status_led0_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOF,running_status_led0_Pin,GPIO_PIN_SET);
}
UART串口配置
UART_HandleTypeDef UART1_Handler;
void uart1Init(u32 bound)
{
UART1_Handler.Instance=USART1;
UART1_Handler.Init.BaudRate=bound;
UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B;
UART1_Handler.Init.StopBits=UART_STOPBITS_1;
UART1_Handler.Init.Parity=UART_PARITY_NONE;
UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
UART1_Handler.Init.Mode=UART_MODE_TX_RX;
UART1_Handler.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UART1_Handler.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UART1_Handler) != HAL_OK)
Error_Handler();
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_Initure;
if(huart->Instance==USART1)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_9 | GPIO_PIN_10;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_FAST;
GPIO_Initure.Alternate=GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
#if EN_USART1_RX
HAL_NVIC_EnableIRQ(USART1_IRQn);
HAL_NVIC_SetPriority(USART1_IRQn,2,0);
#endif
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==UART4)
{
__HAL_RCC_UART4_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
HAL_NVIC_DisableIRQ(UART4_IRQn);
}
}
串口发送较为简洁:
HAL_UART_Transmit(&UART1_Handler,(uint8_t*)screenData,44,1000);
串口接收要用中断来实现,注意将EN_USART1_RX置1!!!
uint8_t SCREEN_RX_BUF[44];
uint8_t screenPoint = 0;
uint8_t screenData[44];
void USART1_IRQHandler(void)
{
uint32_t tmp1 = 0U, tmp2 = 0U;
tmp1 = __HAL_UART_GET_FLAG(&UART1_Handler, UART_FLAG_RXNE);
tmp2 = __HAL_UART_GET_IT_SOURCE(&UART1_Handler, UART_IT_RXNE);
if((tmp1 != RESET) && (tmp2 != RESET))
{
SCREEN_RX_BUF[screenPoint] = (uint8_t)(UART1_Handler.Instance -> DR & (uint8_t) 0x00FFU);
screenPoint ++;
if(SCREEN_RX_BUF[4] == 0x0d)
{
memset(screenData, 0, 44);
memcpy(screenData, SCREEN_RX_BUF, 44);
memset(SCREEN_RX_BUF, 0, 44);
}
}
LED0=!LED0;
HAL_UART_IRQHandler(&UART1_Handler);
}
SPI配置
SPI接口结构 主从机通信连接
SPI_HandleTypeDef SPI2_Handler;
void SPI2_Init(void)
{
SPI2_Handler.Instance=SPI2;
SPI2_Handler.Init.Mode=SPI_MODE_MASTER;
SPI2_Handler.Init.Direction=SPI_DIRECTION_2LINES;
SPI2_Handler.Init.DataSize=SPI_DATASIZE_8BIT;
SPI2_Handler.Init.CLKPolarity=SPI_POLARITY_HIGH;
SPI2_Handler.Init.CLKPhase=SPI_PHASE_2EDGE;
SPI2_Handler.Init.NSS=SPI_NSS_SOFT;
SPI2_Handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_256;
SPI2_Handler.Init.FirstBit=SPI_FIRSTBIT_MSB;
SPI2_Handler.Init.TIMode=SPI_TIMODE_DISABLE;
SPI2_Handler.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE;
SPI2_Handler.Init.CRCPolynomial=10;
if(HAL_SPI_Init(&SPI2_Handler) != HAL_OK)
Error_Handler();
}
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_Initure;
if(hspi -> Instance == SPI2)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_FAST;
GPIO_Initure.Alternate=GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{
if(hspi->Instance==SPI2)
{
__HAL_RCC_SPI2_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
}
}
|