1.串口初始化
2.串口发送
3.串口接收
//串口初始化
USART_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1时钟
//连接GPIO引脚到串口
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//GPIO 初始化
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//配置 Tx 引脚为复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置 Rx 引脚为复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 2500;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长(数据位+校验位):8
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位: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 模式控制:同时使能接收和发送
USART_Init(USART1, &USART_InitStructure);//完成 USART 初始化配置
//中断配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置 USART1 为中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢断优先级为 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//子优先级为 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能串口接收中断
USART_Cmd(USART1, ENABLE);//使能串口
}
//串口发送
void SendData(void)
{
USART_SendData(USART1,ADC_value);//发送16位的数据
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}
//串口接收
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
data = USART_ReceiveData( USART1 );
}
}
1.串口初始化
2.串口发送
3.串口接收
//串口初始化
USART_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1时钟
//连接GPIO引脚到串口
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//GPIO 初始化
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//配置 Tx 引脚为复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置 Rx 引脚为复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 2500;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长(数据位+校验位):8
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位: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 模式控制:同时使能接收和发送
USART_Init(USART1, &USART_InitStructure);//完成 USART 初始化配置
//中断配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置 USART1 为中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢断优先级为 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//子优先级为 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能串口接收中断
USART_Cmd(USART1, ENABLE);//使能串口
}
//串口发送
void SendData(void)
{
USART_SendData(USART1,ADC_value);//发送16位的数据
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}
//串口接收
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
data = USART_ReceiveData( USART1 );
}
}
举报