新手上路
- 积分
- 34
- 金钱
- 34
- 注册时间
- 2019-9-26
- 在线时间
- 5 小时
|
5金钱
用STM32F030c8t6与电脑通过RS485通信,RS485只能发送给电脑数据,无法正确接收电脑发送过来的数据,只能接收第一个电脑发送的数据,其它接收到的数据已接收的第一个数据相同,不知道什么原因?程序为
void rs485_init(uint32_t bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE); //使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART1时钟
//串口1对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1); //GPIOA9复用为USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1); //GPIOA10复用为USART1
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3
//PG8推挽输出,485模式控制
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //PB13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化PB13
//USART1 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_2;//一个停止位
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(USART2, &USART_InitStructure); //初始化串口1
USART_Cmd(USART2, ENABLE); //使能串口1
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPriority =1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
GPIO_ResetBits(GPIOB,GPIO_Pin_13); //默认接收模式
}
void Usart485_Send( uint8_t len, uint8_t *data)
{
uint8_t i;
uint8_t num=len;
GPIO_SetBits(GPIOB,GPIO_Pin_13);//发送模式
delay_ms(2);
for(i=0;i<num;i++)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); //等待发送结束
USART_SendData(USART2, data[i]); //向串口1发送数据
}
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); //等待发送结束
RS485_RX_CNT=0;
GPIO_ResetBits(GPIOB,GPIO_Pin_13); //接收模式
delay_ms(2);
}
void USART2_IRQHandler(void) //串口2中断服务程序
{
uint8_t Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
Res =USART_ReceiveData(USART2);//(USART1->DR); //读取接收到的数据
USART485_RX_BUF[USART485_RX_COUNTS++]=Res;//保存数据
Usart485_Send( 4, USART485_RX_BUF);
}
}
|
|