初级会员

- 积分
- 88
- 金钱
- 88
- 注册时间
- 2020-6-11
- 在线时间
- 26 小时
|
10金钱
今天调试485通信,简单测试程序,但是在调试时,使用串口助手收到的数据与发的数据不一致,求大神帮忙看看什么原因(ps:硬件电路可确认没问题)
#define RS485_RX_EN GPIO_ResetBits(GPIOA , GPIO_Pin_11) //接收使能
#define RS485_TX_EN GPIO_SetBits(GPIOA , GPIO_Pin_11) //发送使能
void RS485_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
//收发方向控制引脚配置 供MAX485所用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART1脚位配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1配置
USART_InitStructure.USART_BaudRate =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_Parity_Even;//
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_Cmd(USART1, ENABLE); //起动串口1
RS485_RX_EN;
}
void USART_Send1(unsigned char data)
{
while((USART1->SR&0X40)==0);
USART1->DR = data;
while((USART1->SR&0X40)==0);//检查发送标志
}
//485 tx data
void COM1_Send(unsigned char *send_buff,unsigned int length)
{
unsigned int i = 0;
RS485_TX_EN;//使能发送
delay_ms(2);
for(i = 0;i < length; i++)
USART_Send1( send_buff[i]);
delay_ms(1);
RS485_RX_EN;//使能接收
}
//485 tx string
void UART1_SendString(unsigned char* s)
{
while(*s != '\0')//检测字符串结束符
{
USART_SendData(USART1 ,*s++);//发送当前字符
delay_us(1);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
delay_us(1);
}
}
int main(void)
{
u8 data[8]={1,2,3,4,5,6,7,8};
delay_init(); //延时函数初始化
LED_Init();
RS485_Configuration(); //初始化RS485
while(1)
{
COM1_Send(&data[0],8);
delay_ms(100);
}
}
|
|