现在我测试一块新的板子 想测试上面的485电路是否能用, 使用的是串口1 PORB15来控制485的收发使能 但是用调试助手发送给单片机 接收不到数据 也进不到接收中断,,下面贴出代码 能否帮忙看一下代码有没有问题。
#include "Manage.h"
#define WAIT_FOR_RECEIVE_END 1
#define MANAGE_RECEIVE_END 2
#define WAIT_FOR_SEND_END 3
#define MANAGE_COMMAND_LENGTH 50
extern u8 ManageReceiveCommand[MANAGE_COMMAND_LENGTH]; // 接收缓冲区
extern u8 ManageSendCommand[MANAGE_COMMAND_LENGTH]; //发送缓冲区
extern u8 ManageCommandIndex ;
extern u8 gManageState;
void ManageUartInit(u32 BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO配置结构体
USART_InitTypeDef USART_InitStructure;//串口配置结构体
NVIC_InitTypeDef NVIC_InitStructure;
/*使能使用的外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
/*GPIOA_Pin_9----USART1_Tx发送端*/
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);
/*GPIOA_Pin_10----USART1_Rx接收端*/
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);
/*GPIOB_Pin_15----收发使能端,0接收,1发送*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//修改
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_15);//使其初始化为接收允许
//Usart1 NVIC 配置 添加
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);// 设置中断优先级分组4
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寄存器
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = BaudRate;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
void USART1_IRQHandler(void)
{
u8 ch;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
ch = USART_ReceiveData(USART1);
ManageReceiveCommand[ManageCommandIndex++] = ch;
if (ch == '
')
{
gManageState = MANAGE_RECEIVE_END;
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
主函数:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "Manage.h"
#define MANAGE_COMMAND_LENGTH 50
#define WAIT_FOR_RECEIVE_END 1
#define MANAGE_RECEIVE_END 2
#define WAIT_FOR_SEND_END 3
u8 gManageState;
u8 ManageReceiveCommand[MANAGE_COMMAND_LENGTH];//接收缓冲区
u8 SendBuf[MANAGE_COMMAND_LENGTH];//发送缓冲区
u8 ManageCommandIndex ;//接收数目
int main(void)
{
u8 t;
ManageCommandIndex = 0;
gManageState = 1;
delay_init(); //延时函数初始化
ManageUartInit(9600); //串口初始化为9600
while(1)
{
if(gManageState == MANAGE_RECEIVE_END)
{
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);//失能串口1接收中断
GPIO_SetBits(GPIOB,GPIO_Pin_15);//485发送使能,停止接收
for(t=0;t<ManageCommandIndex;t++)
{
USART_SendData(USART1,ManageReceiveCommand[t]);
while(!(USART1->SR & USART_FLAG_TXE));//等待发送完成
}
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能串口1接收中断
GPIO_ResetBits(GPIOB,GPIO_Pin_15);//485接收使能,停止发送
gManageState = 1;
ManageCommandIndex = 0;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你可以参考一下原子哥例程里面有个判断接受的协议。
我刚接触这块 初始化我是看正点给的串口实验的代码 好像就是这样的啊 能具体说一下怎么配置复用功能吗 谢谢
//USART_DeInit(USART1); //复位串口 1 ②
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //PA9 复用为 USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //PA10复用为 USART1
一周热门 更多>