|
UART4 使用 PC10 as UART4_Tx , PC11 as UART4_Rx ; 8N1 , 4800bps 发送数据,线路上有数据传送, 但 数据不正确!
硬件环境: 正点原子 的 MiniARM V3.6 。
测试软件:自己编写的----- 不成功。 又下载使用 正点原子的例程工程-----同样现象(收到的数据不同于发送数据)!!!
大侠有遇到过这个情况?毛病在哪?如何解决的? 谢谢!
下面是 相关的 code:(其他就是正常的常规代码,就不再占篇幅了)
//初始化IO 串口4 (from Uart2)
//pclk1 CLK1时钟频率(Mhz)
//bound:波特率
void UART4_Init(u32 bound) // uart4 tx --- PC10 , rx --- PC11
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // GPIOC时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); // UART4 CLK enable
USART_DeInit(UART4); //复位串口4
//UART4_TX 初始化 PC10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PC.10---Tx4
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC10
//UART4_RX 初始化 PC11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PC.11---Rx4
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化 PC11
USART_InitStructure.USART_BaudRate = bound; //一般设置为9600;
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(UART4, &USART_InitStructure); //初始化 Uart4
#ifdef UART4_RX_EN //如果使能了接收
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//使能接收中断
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //开启 Uart4 中断
#endif
USART_Cmd(UART4, ENABLE); //使能串口
}
|
|