【求助】stm32 rs485 无法发送数据,代码如下。stm32f030f4px


#include "stm32f0xx.h"
#include <stdio.h>

#define YK_RS485RD 		GPIO_Pin_1 		//PA1

#define YKRS485RX_Enable  	GPIO_ResetBits(GPIOA,YK_RS485RD) //0
#define YKRS485TX_Enable  	GPIO_SetBits(GPIOA,YK_RS485RD)   //1


uint8_t tid;

void Delay_us(uint16_t data)
{
	uint16_t i,x;

	for(i=0;i<data;i++)
	{
		for(x=0;x<3;x++)
		{
			;
		}	
	}
}

void Delay_ms(uint16_t data)
{
  uint16_t i;

	for(i=0;i<data;i++)	
	{
		Delay_us(100);
	}
}



void USART1_Send(uint8_t byte)  //串口发送一个字节
{
  YKRS485TX_Enable; Delay_ms(1);
	
  USART_SendData(USART1,byte);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
	
  Delay_ms(2);YKRS485RX_Enable; 

}


void	Hardware_Init()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

	//GPIO_Configuration();	
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	
	//YK_GPIO初始化
	GPIO_InitStructure.GPIO_Pin 	= YK_RS485RD;   //YK_RS485RD 485收发控制。
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;	
	GPIO_Init(GPIOA, &GPIO_InitStructure);		
	YKRS485RX_Enable;
	
}

/****************************************************************************
* 函数名: 485初始化
* 功  能: 配置UART1硬件参数
* 输  入: 无
* 返  回: 无
*****************************************************************************/
static void Uart1Hard_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	/* 第1步:打开GPIO和USART部件的时钟 */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  /* 第2步:将USART Tx的GPIO配置为推挽复用模式 */
  //  GPIO_DeInit(GPIOA); 
  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);    
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);	

  /* Configure pins as AF pushpull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	YKRS485RX_Enable;
	
  /* 第4步:设置串口硬件参数 */	
	USART_DeInit(USART1);
	USART_InitStructure.USART_BaudRate = 4800;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	/* 使能接收中断 */
  
	/* 
		USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
		注意: 不要在此处打开发送中断
		发送中断使能在SendUart()函数打开
	*/

	USART_Cmd(USART1, ENABLE);		/* 使能串口 */ 


	/*方法一:清发送完成标志*/
	/* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
		如下语句解决第1个字节无法正确发送出去的问题 */
  //	USART_ClearFlag(USART1, USART_FLAG_TC);     /* 清发送完成标志,Transmission Complete flag */
	
	/*方法二:获取串口1状态标志位*/
	USART_GetITStatus(USART1,USART_FLAG_TC);
		
}

/****************************************************************************
* 函数名: ConfigUartNVIC
* 功  能: 配置硬件中断.
* 输  入: 无
* 输  出: 无
* 返  回: 无
*****************************************************************************/
static void UartNVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

int main(void)
{
//	UartNVIC_Configuration();

	Hardware_Init();
	Uart1Hard_Init();

	USART1_Send(0x93);
	
	while(1)
	{
		
  }
}


void USART1_IRQHandler(void)
{
	uint8_t tep;
	if (USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)//RX
	{ 	
		tep = USART_ReceiveData(USART1);//接收数据 	
	  YKRS485TX_Enable;
		
		while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET){};
  	USART_SendData(USART1, tep);	
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){};

		YKRS485RX_Enable;
	}

}




 

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^