一个简单的串口程序,用串口助手发送后,又在串口助手上显示出来,用原子哥的板测试着没问题,用自己做的板,串口助手显示一直没收到数,区别就是原子哥板上的是USART1,我的板上是USART3,JTAG调试时,看到DR寄存器值为0,RXNE=0,好像是没进入到接收中断,不知道什么原因
u16 USART_RX_BUF[200]; //接收缓冲
u16 USART_RX_CNT=0; //接收计数
void uart(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3,&USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //NVIC初始化
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //开接收中断
USART_Cmd(USART3,ENABLE); //使能串口
}
void USART3_IRQHandler()
{
u16 Res;
if (USART_GetITStatus(USART3,USART_IT_RXNE)!=0)
Res=USART_ReceiveData(USART3);
if(USART_RX_CNT<200)
{
USART_RX_BUF[USART_RX_CNT]=Res;
USART_RX_CNT++;
}
USART_SendData(USART3,Res);
}
int main()
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart(9600);
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>