365足彩外围
通过另一个串口发送串口收到的数据。
void UART2_Init(u32bound)
GPIO_InitTypeDefGPIO_InitStructure;
USART_InitTypeDefUSART_InitStructure;
NVIC_InitTypeDefNVIC_InitStructure;
// 1,串行时钟使能GPIO时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// 2,串口重启
USART_DeInit(USART2);
// 3,GPIO端口设置
GPIO_InitStructure
GPIO_Pin = GPIO_Pin_2; // PA2TX
GPIO_InitStructure
GPIO_Mode = GPIO_Mode_AF_PP; //多个推挽输出
GPIO_InitStructure
GPIO_Speed ?? = GPIO_Speed_50 MHz。
GPIO_Init(GPIOA,GPIO_InitStructure);
GPIO_InitStructure
GPIO_Pin = GPIO_Pin_3; // PA3RX
GPIO_InitStructure
GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮动输入
GPIO_InitStructure
GPIO_Speed ?? = GPIO_Speed_50 MHz。
GPIO_Init(GPIOA,GPIO_InitStructure);
// 4,串口参数初始化
USART_InitStructure
USART_BaudRate =已链接;
USART_InitStructure
USART_WordLength = USART_WordLength_8b;
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(USART2,USART_InitStructure);
// 5,初始化NVIC
NVIC_InitStructure
NVIC_IRQ通道= USART2_IRQn;
NVIC_InitStructure
NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure
NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure
NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(NVIC_InitStructure);
// 6,打开中断
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
// 7,启用串口
USART_Cmd(USART2,ENABLE);
voidUSART2_IRQHandler(void)
U8res;
是(USART_GetITStatus(USART2,USART_IT_RXNE)!
=重置)
Res = USART_ReceiveData(USART2);
USART_SendData(USART2,res); //发送接收的数据
最简单的串行端口2接收的数据通过串行端口2传输。通过串口1发送时,串口的中断发送功能变为串口1。


Time:2019-11-14 13:14:57  编辑:admin
RETURN