之前是用232通信已经可以通了,现在加了一个485的控制端,结果无法进入接收中断,也不能接收数据。可以发送数据。而且485的硬件没问题,用其他的程序调试可以正常发送和接收。下面是我的代码,不知道为什么进不了中断,请大家帮忙看看是什么问题- /*************485宏定义****************/
- #define RS485TX() GPIO_SetBits(GPIOB,GPIO_Pin_8)
- #define RS485RX() GPIO_ResetBits(GPIOB,GPIO_Pin_8)
- /************** 串口中断接收数据 ****************/
- void USART1_IRQHandler(void)
- {
- unsigned int dat;
- if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
- {
- USART_ClearITPendingBit(USART1,USART_IT_RXNE);
- dat=USART1_GetByte();
- USART1_SendByte(dat);
- }
- }
- void RS485_GpioInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //485使能端配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void USART1_SendByte(unsigned int SendData)
- {
- RS485TX(); //打开发送控制端
- Delay(0xff);
- USART_SendData(USART1,SendData); //发送数据
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //等待数据发送完成
- Delay(0xff);
- RS485RX(); //关闭发送控制端
- }
- unsigned int USART1_GetByte(void)
- {
- unsigned int MidData=0;
- RS485RX(); //打开接收控制端
- Delay(0xff);
- MidData = USART_ReceiveData(USART1); //接收数据
- Delay(0xff);
- RS485TX(); //关闭接收控制端
- return MidData;
- }
- /**************串口的IO口配置****************/
- void USART_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /**************开串口(波特率、停止位、接收发送中断可调)****************/
- void OpenCom(unsigned int BaudRate,unsigned int StopBits,unsigned int Mode)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- USART_GPIO_Init();
- USART_InitStructure.USART_BaudRate = BaudRate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- switch(StopBits)
- {
- case 0: USART_InitStructure.USART_StopBits = USART_StopBits_1;break;
- case 1: USART_InitStructure.USART_StopBits = USART_StopBits_2;break;
- default: break;
- }
-
- 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); /* USART configuration */
- switch(Mode)
- {
- case 0: USART_ITConfig(USART1,USART_IT_TXE,ENABLE);break;
- case 1: USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);break;
- }
-
- USART_Cmd(USART1, ENABLE);
- USART_ClearFlag(USART1, USART_FLAG_TC);
- RS485RX();
-
- NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=4;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
|