#include "USART.h"
u8 USART3_RX_BUF[14]; //接收缓冲u8 CH4ZeroSetFlag=0;
u16 USART3_RX_STA=0; //接收状态标记
/********************************************************************
改硬件后,将串口1--USART1改为串口--3USART3
********************************************************************/
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //USART1挂在RCC_APB2Periph
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //数据输出端
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //数据输入端
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SEND_485; //控制MAX487芯片
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600; //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位
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(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART3, USART_IT_TC, ENABLE); //打开发送完成中断
USART_Cmd(USART3,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); //配置串口中断
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// USART_ClearFlag(USART2, USART_FLAG_TC);//清发送完成标志位
GPIOA->BRR = SEND_485; //默认为接收
}
void USART3_IRQHandler(void) //串口3中断服务程序
{
u8 Res;
OSIntEnter();
if(USART_GetITStatus(USART3, USART_IT_RXNE)!= RESET) //接收中断
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE); //清除中断标志
Res =USART_ReceiveData(USART3); //读取接收到的数据
USART3_RX_BUF[USART3_RX_STA]=Res;
if(USART3_RX_STA>14)
{
USART3_RX_STA=0;
}
else
{
USART3_RX_STA++;
}
}
OSIntExit();
OSIntEnter();
if(USART_GetITStatus(USART3, USART_IT_TC) != RESET)
{
USART_ITConfig(USART3, USART_IT_TC, DISABLE); //关闭发送完成中断
USART_ClearITPendingBit(USART3, USART_IT_TC); //清除中断标志
}
OSIntExit();
}
void USART3_SendByte(uint8_t byte)
{
SEND_485_TX; //控制RS485收发芯片MAX487:发送模式
OSTimeDlyHMSM(0,0,0,2);
USART_SendData(USART3,byte);
while (USART_GetFlagStatus(USART3,USART_FLAG_TC) !=SET);
OSTimeDlyHMSM(0,0,0,2);
SEND_485_RX; //接收模式
}
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
// USART3->SR;
USART_SendData(USART3, (unsigned char) ch);
while (!(USART3->SR & USART_FLAG_TXE));
return (ch);
}
现在的问题是,可以正确发送数据到串口助手,但是从串口助手发送过来的数据,可以进入接收中断,但是读取不了数据。
求各位大神指导。。。 |