我想用485通信程序这样写#include "usart.h"
#include "delay.h"
/*485测试程序*/
void usart_init(u32 bound)
{
/*打开RCC复位时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //打开USART2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO ,ENABLE); //开启GPIOA的时钟
/*串口配置*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //485使能控制引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ; //复用推挽输出
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //上拉输入
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*中断嵌套配置*/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
/*串口配置*/
USART_DeInit(USART2); //进行串口初始化
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = bound ; //波特率设置为输入一般为9600
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None; //无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx; //使能为接收
USART_InitStructure.USART_Parity = USART_Parity_No ; //奇偶失能
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位为1位
USART_InitStructure.USART_WordLength= USART_WordLength_8b; //8位数据位
USART_Init(USART2,&USART_InitStructure);
/*打开串口中断*/
USART_Cmd(USART2, ENABLE); //使能串口2
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //使能接收中断
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清空接收中断标志位
USART_ClearITPendingBit(USART2,USART_IT_TXE); //清空发送中断标志位
USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //失能发送中断
/*打开485的接收端口*/
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //(低电平使能接收)
//GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
void USART2_IRQHandler()
{
u16 rev_data;
if(USART_GetFlagStatus(USART2,USART_IT_RXNE)!=RESET)
{
rev_data=USART_ReceiveData(USART2);
// GPIO_SetBits(GPIOA,GPIO_Pin_4);
// delay_ms(1);
// USART_SendData(USART2,rev_data);
// while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
// delay_ms(2);
// GPIO_ResetBits(GPIOA,GPIO_Pin_4);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
if(rev_data==0x00)
{
GPIO_SetBits(GPIOB, GPIO_Pin_5);
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
}
}
这样写的话,从串口给数据0X00板子没有任何反应
|