初中生
最后登录1970-1-1
在线时间 小时
注册时间2018-12-7
|
本帖最后由 PeguinWang 于 2018-12-9 20:32 编辑
最近项目要用到RS485通讯,我用的是挑战者F429配一个USB转485转接头。但是这个程序一直无法正常工作,向PC发送数据转接头的数据指示灯不亮,串口调试助手没收到任何东西;PC向板子发送数据能进接收中断,接受引脚上用示波器也测到了正确波形,但是收到的数据全是0,而且DEBUG的时候发现一接收数据LBD位就会被置位,同时溢出错误ORE位和帧错误FE位也被置位,请问有没有人知道这是怎么回事?
附上关键源代码:
USART配置,PC用485向板子发送数据,USART1向PC回传接受到的数据:
void GPIO_Config(void)
{
GPIO_InitTypeDef LEDG_GPIO;
GPIO_InitTypeDef USART1_GPIOInit;
GPIO_InitTypeDef USART2_GPIOInit;
/*LED测试引脚配置*/
LEDG_GPIO.GPIO_Mode = GPIO_Mode_OUT;
LEDG_GPIO.GPIO_OType = GPIO_OType_PP;
LEDG_GPIO.GPIO_PuPd = GPIO_PuPd_UP;
LEDG_GPIO.GPIO_Speed = GPIO_High_Speed;
LEDG_GPIO.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_Init(GPIOH,&LEDG_GPIO);
/*USART2 RS485配置*/
USART2_GPIOInit.GPIO_Mode = GPIO_Mode_AF;
USART2_GPIOInit.GPIO_OType = GPIO_OType_PP;
USART2_GPIOInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
USART2_GPIOInit.GPIO_Speed = GPIO_High_Speed;
USART2_GPIOInit.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD,&USART2_GPIOInit);
USART2_GPIOInit.GPIO_Mode = GPIO_Mode_AF;
USART2_GPIOInit.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOD,&USART2_GPIOInit);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
USART2_GPIOInit.GPIO_Mode = GPIO_Mode_OUT;
USART2_GPIOInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
USART2_GPIOInit.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOD,&USART2_GPIOInit);
/*USART1配置*/
USART1_GPIOInit.GPIO_Mode = GPIO_Mode_AF;
USART1_GPIOInit.GPIO_OType = GPIO_OType_PP;
USART1_GPIOInit.GPIO_PuPd = GPIO_PuPd_UP;
USART1_GPIOInit.GPIO_Speed = GPIO_Medium_Speed;
USART1_GPIOInit.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA,&USART1_GPIOInit);
USART1_GPIOInit.GPIO_Mode = GPIO_Mode_AF;
USART1_GPIOInit.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA,&USART1_GPIOInit);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
}
void USART_Config(void)
{
USART_InitTypeDef RS485_Init;
USART_InitTypeDef USART1_Init;
RS485_Init.USART_BaudRate = 115200;
RS485_Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
RS485_Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
RS485_Init.USART_Parity = USART_Parity_No;
RS485_Init.USART_StopBits = USART_StopBits_1;
RS485_Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2,&RS485_Init);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
GPIO_ResetBits(GPIOD,GPIO_Pin_11);
USART1_Init.USART_BaudRate = 115200;
USART1_Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART1_Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART1_Init.USART_Parity = USART_Parity_No;
USART1_Init.USART_StopBits = USART_StopBits_1;
USART1_Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART1_Init);
USART_Cmd(USART1, ENABLE);
}
/*****************发送一个字符**********************/
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
delay_ms(10);
GPIO_SetBits(GPIOD,GPIO_Pin_11);
delay_ms(10);
USART_SendData(pUSARTx,ch);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET){};
GPIO_ResetBits(GPIOD,GPIO_Pin_11);
delay_ms(10);
}
#include "main.h"
void RCC_Config(void);
uint8_t i = 0;
int main(void)
{
RCC_Config();
NVIC_Config();
GPIO_Config();
USART_Config();
delay_init(180);
LEDR_OFF();
LEDG_OFF();
while(1);
}
void RCC_Config(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
RCC_PLLCmd(DISABLE);
RCC_PLLConfig(RCC_PLLSource_HSE,25,360,2,7);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div2);
}
else
{while (1);}
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
uint8_t ucTemp = 1;
void RS485_IRQHandler(void)
{
ucTemp = USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
LEDR_Toggle();
USART_SendData(USART1,ucTemp);
if (USART_GetFlagStatus(USART2,USART_FLAG_ORE) != RESET)
{
uint8_t ERR_CLEAR = ERR_CLEAR;
ERR_CLEAR = USART2->SR;
ERR_CLEAR = USART2->DR;
}
}
|
|