2019-07-21 03:39发布
zc123 发表于 2019-4-1 14:19 串口进不了中断问题一般就几个 1.串口配置出错,如时钟未使能,波特率,数据位,停止位配置不一致 2.串口 ...
最多设置5个标签!
这几步都试过了,没问题
#include "usart.h"
#include "PM2.5_Sensor.h"
#include "NTCtp.h"
#include "stdio.h"
#include "CO2_Sensor.h"
extern uint8_t data_cnt;
extern uint8_t counter;
void usart_config(void)
{
//1:打开GPIO ,USART3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
//2:初始化相应的串口引脚
usart_release_gpio_int();
//3:配置串口模式
usart_para_config();
//4:配置串口中断
usart_nvic_config();
}
void usart_release_gpio_int(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_1); //TX
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_1); //RX
}
void usart_para_config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; /*波特率*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*流控*/
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*数据位,8*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*校验位,n*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*停止位,1*/
USART_Init(USART3,&USART_InitStructure);
USART_ClearFlag(USART3,USART_FLAG_TC); //清除发送完成标志位
USART_Cmd(USART3,ENABLE); //使能串口3
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
}
void usart_nvic_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int fputc(int ch, FILE *f) //IAR重定向printf函数
{
//while (USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET )
while (USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET )
{
}
USART_SendData(USART3,(uint8_t) ch);
return ch;
}
void USART3_6_IRQHandler(void)
{
uint8_t receive_data;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //判断读寄存器是否有数据
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE); //清除发送完成标志位
receive_data = USART_ReceiveData(USART3); //接收USART3的数据
data_buf[counter] = receive_data;
counter++;
if(counter >11)
{
counter = 0;
}
}
}
这是串口部分的配置代码, PC端测试的话我上周六刚试过,没问题
一周热门 更多>