现使用战舰开发板和GPRS-DTU做一个采集数据加通讯的实验
DTU的例程Slave中是使用的 UART3来做串口的数据接收和发送的,我使用例程并改了下main.c的程序可以将 战舰开发板采集到的温湿度数据通过DTU发送到原子云上,但是由于后面要加上MPU6050占用了UART3的引脚,就打算使用UART2来做通讯,但是将例程中串口的所有配置都改为UART2后,原子云接收不到数据,查了一天了也没弄通,把简单的代码发一下,各位大神朋友能帮忙指点一下么?
是不是UART2有特殊的配置之类的?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "sys.h"
#include "uart2.h"
#include "ringbuffer.h"
RingBuffer *pUart2RxBuf;
void uart2_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.2
//USART2_RX GPIOA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.3
//Usart2 NVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;
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); //初始化串口2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启串口接受中断
USART_Cmd(USART2, ENABLE); //使能串口2
}
void USART2_IRQHandler(void) //串口2中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
Res = USART_ReceiveData(USART2); //读取接收到的数据
RingBuffer_In(pUart2RxBuf, &Res, 1); //放入缓存
}
}
void USART2_Send_Data(u8 *data, u32 size)
{
for(u32 i = 0; i < size; i++)
{
while((USART2->SR & 0X40) == 0);
USART2->DR = data[i];
}
}
一周热门 更多>