`这次跟大家分享一下串口的使用,正好趁着双十二,在网上买了个串口转USB模块,用的芯片是CH340G,在win10系统上用起来挺稳定的,最高波特率可以达到921600 bps没有乱码,之前51 单片机开发板上带的那个串口转USB跑这么高的波特率会出现乱码。
板子上 电源从串口转USB模块引入,红线是5V,绿色GND,蓝色TXD,黄色RXD。
(注意跳帽接到VIN-5V)
选用芯片的USART6外设,查手册知道可以看到引脚对应关系
之所以选择USART6而不是USART3是因为 USART3通过STlink跟电脑连在了一起,这样的话如果使用串口就没办法再用STlink进行调试,当然,如果代码已经调试好了,可以直接使用USART3,STlink充当了串口转USB模块的角色。
程序的思路很简单,使用DMA把串口接收到的数据保存下来,在DMA传输完成中断里把接收到的数据再通过串口发送出去。
bsp_uart.h
- #ifndef __BSP_USART
- #define __BSP_USART
- #define UART_DR_Address ((uint32_t)0x40011404) //USART6 DR寄存器的地址
- void bsp_InitUart(void);
- void UART_Config(void);
- void UART_DMA_Config(void);
- #endif
复制代码
bsp_uart.c
- #include "bsp.h"
- void UART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
- USART_InitStructure.USART_BaudRate = 921600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- /* When using Parity the word length must be configured to 9 bits */
- 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(USART6, &USART_InitStructure);
- USART_Cmd(USART6, ENABLE);
-
- USART_ClearITPendingBit(USART6, USART_IT_TC);
-
- USART_DMACmd(USART6, USART_DMAReq_Rx, ENABLE);
- USART_DMACmd(USART6, USART_DMAReq_Tx, ENABLE);
-
- USART_ITConfig(USART6, USART_IT_TC, ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void UART_DMA_Config(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
- DMA_InitStructure.DMA_BufferSize = 1;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)UART_DR_Address;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_Channel = DMA_Channel_5;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_DMA_Buf1;
-
- DMA_Init(DMA2_Stream1,&DMA_InitStructure);
-
- DMA_Cmd(DMA2_Stream1, ENABLE);
-
- NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- DMA_ITConfig(DMA2_Stream1,DMA_IT_TC,ENABLE);
-
- }
- void DMA2_Stream1_IRQHandler(void)
- {
- if(DMA_GetFlagStatus(DMA2_Stream1, DMA_FLAG_TCIF1)!=RESET)
- {
- DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
- }
- USART_SendData(USART6, UART_DMA_Buf1[0]);
- while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
- GPIO_ToggleBits(GPIOB, GPIO_Pin_14); //使用板子上得到红色的灯来指示串口工作状态
- }
- void bsp_InitUart(void)
- {
- UART_Config();
- UART_DMA_Config();
- }
- void USART6_IRQHandler(void)
- {
- if(USART_GetFlagStatus(USART6, USART_FLAG_TC) == 1)
- {
- USART_ClearITPendingBit(USART6, USART_IT_TC);
- }
- }
复制代码
上位机选用老牌的putty
先设置波特率,我电脑上串口端号是COM4,数据位数和停止位个数
接下还有一些细节需要设置
比如字符集
如果不选中红色标识的选项,边界会遮挡数据不显示
勾选红色标识选项后,每次按下回车键,光标才能换行。
具体效果如下
主函数里没什么东西就不贴了。
`
0
|
|
|
|
最后一张图排版错啦
点击open才能打开面板
|
|
|
|
|