新手入门
- 积分
- 5
- 金钱
- 5
- 注册时间
- 2020-12-1
- 在线时间
- 2 小时
|
楼主 |
发表于 2024-2-22 11:39:44
|
显示全部楼层
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;// GPIO_Mode_IPU;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART1, &USART_InitStructure); //初始化串口1
//Usart1 NVIC 配置//
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//在FreeRTOS不可更改,全为抢占式优先级,共0-15级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级STM32中断优先级数值越小,优先级越大,FreeRTOS任务优先级是数值越大,优先级越高。需要和中断优先级区分开来。
//0-4级不受RTOS控制
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //在FreeRTOS不可更改子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //由RTOS系统开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
uint8_t Rx1Data;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//&&(RX3_FLAG==0))
{
Rx1Data=USART_ReceiveData(USART1); //读出数据后自动清除字节接收标志位
USART1_RX_BUF[RX1_Cnt]=Rx1Data;
RX1_Cnt=RX1_Cnt+1;
if(RX1_Cnt >50){RX1_Cnt =0;}
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
|
|