STM32F4串口USART发送为00的解决方案

  1. 检查接线是否正确
  2. 检查TX是否为复用推挽输出

   3.检查是否将TX和RX引脚重映射为USART功能

 

  • 在STM32中,每个GPIO引脚可以配置为不同的复用功能,例如UART、SPI、I2C等。
  • 具体来说,GPIO_PinAFConfig函数用于配置GPIO引脚的复用功能。它的参数包括GPIOx(x代表GPIO端口,例如GPIOA、GPIOB等)、GPIO_PinSource(指定引脚的编号,例如9代表引脚9)、GPIO_AF(指定要配置的复用功能,例如USART1)。
  • GPIOA的引脚9和引脚10配置为USART1的复用功能,这意味着这两个引脚可以用于USART1通信,而不再是普通的GPIO引脚。通过这种配置,可以将USART1与其他设备进行串口通信。

串口发送接收的初始化代码如下

/**
 ****************************************************************************************************
 * @author      Archie_IT
 * @version     V1.0
 * @date        2023-11-21
 * @brief       串口1 驱动代码
 * @CSDN				https://blog.csdn.net/m0_61712829?type=blog
 ****************************************************************************************************
 * @attention
 *
 * 主控:stm32f429
 * 引脚:ch340的TXD----PA10(mcu接收)、ch340的RXD----PA9(mcu发送)
 *
 *
 * 修改说明:
 * 
 *
 ****************************************************************************************************
 */
 

#include "stm32f4xx.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>						//用于包含可变数量参数的标准头文件。


uint8_t Serial_RxData;
uint8_t Serial_RxFlag;


void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);	
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructure);

	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
}

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}


void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
	uint16_t i;
	for(i=0;i<Length;i++)
	{
	Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)
{
	uint8_t i;
	for(i = 0;String[i] != '\0';i++)
	{
		Serial_SendByte(String[i]);
	}

}


//发送数字
//x的y次方
uint32_t Serial_POW(uint32_t x,uint32_t y)
{
	uint32_t Result = 1;
	while(y--)
	{
		Result = Result * x;
	}
	return Result;
}
//        Number/10的(个十百千万)位次方%10  ->求出个十百千万的每个数
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
	uint8_t i;
	for(i=0;i<Length;i++)
	{
		Serial_SendByte(Number / Serial_POW(10,Length - 1 - i) % 10 + 0x30);
	}

}

//fputc函数重定向到串口,printf输出到串口;方式1
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

//可变参数;printf方式3
void Serial_Printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);
}





//中断接收和变量的封装函数
uint8_t Serial_GetRxFlag(void)//读后自动清除标志位
{
	if(Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}

uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;
}




//重写中断函数
void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)
	{
		Serial_RxData = USART_ReceiveData(USART1);
		Serial_RxFlag = 1;
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
	
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 13:42:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 13:42:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 13:42:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 13:42:01       20 阅读

热门阅读

  1. Docker tag 命令

    2023-12-06 13:42:01       42 阅读
  2. 首例CSDN_AI文章-- K-均值聚类算法

    2023-12-06 13:42:01       37 阅读
  3. 蓝桥杯ACwing习题

    2023-12-06 13:42:01       32 阅读
  4. 基于python实现人脸识别登录系统

    2023-12-06 13:42:01       32 阅读
  5. MySQL四 | 约束

    2023-12-06 13:42:01       40 阅读
  6. 【PyTorch】优化分析

    2023-12-06 13:42:01       32 阅读
  7. PHP常用的正则表达式

    2023-12-06 13:42:01       39 阅读
  8. go语言http协议post方法样例调用

    2023-12-06 13:42:01       39 阅读