用串口助手通过SP485发送数据,但在USART3的数据寄存器中得到的数据不是发送的数据
#include <stm32f10x.h>
#include "usart1.h"
#include <stdarg.h>
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //ÍÆÍìÊä³ö-TX
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë-RX
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//RTX
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART3 ¹¤×÷ģʽÅäÖà */
USART_InitStructure.USART_BaudRate = 9600; //²¨ÌØÂÊÉèÖãº9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Êý¾ÝλÊýÉèÖãº8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1; //ֹͣλÉèÖãº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_ClearFlag(USART3,USART_FLAG_OR);
USART_Init(USART3, &USART_InitStructure); //³õʼ»¯USART3
USART_Cmd(USART3, ENABLE);// USART3ʹÄÜ
}
/*·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý*/
void UART3SendByte(unsigned char SendData)
{
GPIO_SetBits(GPIOB,GPIO_Pin_14);
USART_SendData(USART3,SendData);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
/*½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý*/
unsigned char UART3GetByte(unsigned char* GetData)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
{ return 0;//ûÓÐÊÕµ½Êý¾Ý
}
*GetData = USART_ReceiveData(USART3);
return 1;//ÊÕµ½Êý¾Ý
}
/*½ÓÊÕÒ»¸öÊý¾Ý£¬ÂíÉÏ·µ»Ø½ÓÊÕµ½µÄÕâ¸öÊý¾Ý*/
void UART3Test(void)
{
unsigned char i = 0;
while(1)
{
while(UART3GetByte(&i))
{
UART3SendByte(i);
}
}
}
|