#include "usart.h"
extern void Error_Handler(void);
UART_HandleTypeDef huart2;
UART_HandleTypeDef husart_debug;
UART_HandleTypeDef UartHandle;
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/**USART2 GPIO Configura
tion
PA0-CK_IN ------> USART2_RX
PA9 ------> USART2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspDeInit 0 */
/* USER CODE END USART2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration
PA0-CK_IN ------> USART2_RX
PA9 ------> USART2_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_9);
}
}
void MX_USART2_UART_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
//huart2.Init.OverSampling = UART_OVERSAMPLING_16;
//HAL_UART_Init(&huart2);
if (HAL_UART_Init(&UartHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
}
/**
* º¯Êý¹¦ÄÜ: Öض¨Ïòc¿âº¯Êýprintfµ½DEBUG_USARTx
* ÊäÈë²ÎÊý: ÎÞ
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷£ºÎÞ
*/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&husart_debug, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
/**
* º¯Êý¹¦ÄÜ: Öض¨Ïòc¿âº¯Êýgetchar,scanfµ½DEBUG_USARTx
* ÊäÈë²ÎÊý: ÎÞ
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷£ºÎÞ
*/
int fgetc(FILE * f)
{
uint8_t ch = 0;
HAL_UART_Receive(&husart_debug,&ch, 1, 0xffff);
return ch;
}
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
//MX_GPIO_Init();
//MX_ADC_Init();
MX_USART2_UART_Init();
while (1)
{
// I2C_Write(0x0000,1);
printf("s");
HAL_Delay(500);
}
}
型号是
STM32L011什么波形都没有,哪位大神帮忙看下谢谢!
有通信,串口线就会产生方波不是?
一周热门 更多>