接收端串口设置
我们定义的是串口函数初始化,我们使用的是串口二
void uart2_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef UartGpioInitStructer;
USART_InitTypeDef UartinitStructer;
//在配置过程中,为防止TX RX不再同一个端口上,增强可移植性,固分开配置
//初始化串口TX RX 引脚
RCC_APB2PeriphClockCmd( UART2_TX_GPIO_CLK | UART2_RX_GPIO_CLK, ENABLE ); //打开TX RX 端口时钟
UartGpioInitStructer.GPIO_Mode = GPIO_Mode_AF_PP;
UartGpioInitStructer.GPIO_Speed = GPIO_Speed_2MHz;
//TX
UartGpioInitStructer.GPIO_Pin = UART2_TX_GPIO_PIN;
GPIO_Init( UART2_TX_GPIO_PORT, &UartGpioInitStructer ); //初始化TX引脚 配置为复用功能
//RX
UartGpioInitStructer.GPIO_Mode = GPIO_Mode_IN_FLOATING;
UartGpioInitStructer.GPIO_Pin = UART2_RX_GPIO_PIN;
GPIO_Init( UART2_RX_GPIO_PORT, &UartGpioInitStructer ); //初始化RX引脚 配置为输入
//配置USART外设
USART_DeInit( UART2_PORT ); //外设复位
if( USART1 == UART2_PORT ) //使能外设时钟
{
RCC_APB2PeriphClockCmd( UART2_PORT_CLK, ENABLE );
} //不同的USART外设可能在不同的APB时钟上
else //STM32F103单片机只有USART1在APB2上,如平台有差异做相应改变即可
{
RCC_APB1PeriphClockCmd( UART2_PORT_CLK, ENABLE );
}
UartinitStructer.USART_BaudRate = bound; //设置波特率
UartinitStructer.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不使用流控制
UartinitStructer.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送和接收
UartinitStructer.USART_Parity = USART_Parity_No; //不带校验
UartinitStructer.USART_StopBits = USART_StopBits_1; //一个停止位
UartinitStructer.USART_WordLength = USART_WordLength_8b; //8个数据位
USART_Cmd( UART2_PORT, DISABLE ); //失能外设
USART_Init( UART2_PORT, &UartinitStructer ); //初始化外设
USART_Cmd( UART2_PORT, ENABLE ); //使能外设
}
串口接收函数设置
uint8_t drv_uart2_rx_bytes( uint8_t* RxBuffer )
{
uint8_t l_RxLength = 0;
uint16_t l_UartRxTimOut = 0x7FFF;
while( l_UartRxTimOut-- ) //等待查询串口数据
{
if( RESET != USART_GetFlagStatus( UART2_PORT, USART_FLAG_RXNE ))
{
*RxBuffer = (uint8_t)UART2_PORT->DR;
RxBuffer++;
l_RxLength++;
l_UartRxTimOut = 0x7FFF; //接收到一个字符,回复等待时间
}
if( 100 == l_RxLength )
{
break; //不能超过100个字节
}
}
return l_RxLength; //等待超时,数据接收完成
}
串口头文件配置
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
void uart2_init(u32 bound);
#define UART2_TX_GPIO_PORT GPIOA
#define UART2_TX_GPIO_CLK RCC_APB2Periph_GPIOA
#define UART2_TX_GPIO_PIN GPIO_Pin_2
#define UART2_RX_GPIO_PORT GPIOA
#define UART2_RX_GPIO_CLK RCC_APB2Periph_GPIOA
#define UART2_RX_GPIO_PIN GPIO_Pin_3
uint8_t drv_uart2_rx_bytes( uint8_t* RxBuffer );
#endif
定时中断初始化,进行循环接收
void TIM2_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
}
定制中断函数编写
uint8_t g_As62_rx_buffer[ 100 ] = { 0 }; //也就是检测出来的数值不能超过100个数值
uint8_t g_As62_rx_buffer2[ 10 ] = { 0 }; //检测出来的数值不能超过10个数值
uint8_t g_RxLength = 0; //接收到的字符长度
char Make ='n';
int TIM2_IRQHandler(void)
{
u8 temp;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
if(delay_flag==1)
{
if(++delay_50==5) delay_50=0,delay_flag=0; //给主函数提供50ms的精准延时
}
g_RxLength = drv_uart_rx_bytes( g_As62_rx_buffer );//接收串口字符串信息 g_RxLength为获得字符串个数
drv_uart2_rx_bytes( g_As62_rx_buffer2 );
Make=g_As62_rx_buffer2[0];
Control_remote(Make); //处理串口接收到的数据
memset(g_As62_rx_buffer, 0, sizeof(g_As62_rx_buffer));
}
return 0;
}
主函数里面串口初始化,然后配置定时器二,设置成10ms中断。
接收端串口设置
我们定义的是串口函数初始化,我们使用的是串口二
void uart2_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef UartGpioInitStructer;
USART_InitTypeDef UartinitStructer;
//在配置过程中,为防止TX RX不再同一个端口上,增强可移植性,固分开配置
//初始化串口TX RX 引脚
RCC_APB2PeriphClockCmd( UART2_TX_GPIO_CLK | UART2_RX_GPIO_CLK, ENABLE ); //打开TX RX 端口时钟
UartGpioInitStructer.GPIO_Mode = GPIO_Mode_AF_PP;
UartGpioInitStructer.GPIO_Speed = GPIO_Speed_2MHz;
//TX
UartGpioInitStructer.GPIO_Pin = UART2_TX_GPIO_PIN;
GPIO_Init( UART2_TX_GPIO_PORT, &UartGpioInitStructer ); //初始化TX引脚 配置为复用功能
//RX
UartGpioInitStructer.GPIO_Mode = GPIO_Mode_IN_FLOATING;
UartGpioInitStructer.GPIO_Pin = UART2_RX_GPIO_PIN;
GPIO_Init( UART2_RX_GPIO_PORT, &UartGpioInitStructer ); //初始化RX引脚 配置为输入
//配置USART外设
USART_DeInit( UART2_PORT ); //外设复位
if( USART1 == UART2_PORT ) //使能外设时钟
{
RCC_APB2PeriphClockCmd( UART2_PORT_CLK, ENABLE );
} //不同的USART外设可能在不同的APB时钟上
else //STM32F103单片机只有USART1在APB2上,如平台有差异做相应改变即可
{
RCC_APB1PeriphClockCmd( UART2_PORT_CLK, ENABLE );
}
UartinitStructer.USART_BaudRate = bound; //设置波特率
UartinitStructer.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不使用流控制
UartinitStructer.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送和接收
UartinitStructer.USART_Parity = USART_Parity_No; //不带校验
UartinitStructer.USART_StopBits = USART_StopBits_1; //一个停止位
UartinitStructer.USART_WordLength = USART_WordLength_8b; //8个数据位
USART_Cmd( UART2_PORT, DISABLE ); //失能外设
USART_Init( UART2_PORT, &UartinitStructer ); //初始化外设
USART_Cmd( UART2_PORT, ENABLE ); //使能外设
}
串口接收函数设置
uint8_t drv_uart2_rx_bytes( uint8_t* RxBuffer )
{
uint8_t l_RxLength = 0;
uint16_t l_UartRxTimOut = 0x7FFF;
while( l_UartRxTimOut-- ) //等待查询串口数据
{
if( RESET != USART_GetFlagStatus( UART2_PORT, USART_FLAG_RXNE ))
{
*RxBuffer = (uint8_t)UART2_PORT->DR;
RxBuffer++;
l_RxLength++;
l_UartRxTimOut = 0x7FFF; //接收到一个字符,回复等待时间
}
if( 100 == l_RxLength )
{
break; //不能超过100个字节
}
}
return l_RxLength; //等待超时,数据接收完成
}
串口头文件配置
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
void uart2_init(u32 bound);
#define UART2_TX_GPIO_PORT GPIOA
#define UART2_TX_GPIO_CLK RCC_APB2Periph_GPIOA
#define UART2_TX_GPIO_PIN GPIO_Pin_2
#define UART2_RX_GPIO_PORT GPIOA
#define UART2_RX_GPIO_CLK RCC_APB2Periph_GPIOA
#define UART2_RX_GPIO_PIN GPIO_Pin_3
uint8_t drv_uart2_rx_bytes( uint8_t* RxBuffer );
#endif
定时中断初始化,进行循环接收
void TIM2_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
}
定制中断函数编写
uint8_t g_As62_rx_buffer[ 100 ] = { 0 }; //也就是检测出来的数值不能超过100个数值
uint8_t g_As62_rx_buffer2[ 10 ] = { 0 }; //检测出来的数值不能超过10个数值
uint8_t g_RxLength = 0; //接收到的字符长度
char Make ='n';
int TIM2_IRQHandler(void)
{
u8 temp;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
if(delay_flag==1)
{
if(++delay_50==5) delay_50=0,delay_flag=0; //给主函数提供50ms的精准延时
}
g_RxLength = drv_uart_rx_bytes( g_As62_rx_buffer );//接收串口字符串信息 g_RxLength为获得字符串个数
drv_uart2_rx_bytes( g_As62_rx_buffer2 );
Make=g_As62_rx_buffer2[0];
Control_remote(Make); //处理串口接收到的数据
memset(g_As62_rx_buffer, 0, sizeof(g_As62_rx_buffer));
}
return 0;
}
主函数里面串口初始化,然后配置定时器二,设置成10ms中断。
举报