实验报告
一、实验目的
- 了解串口通信方式、UART设备&标准、RS232等标准规范、COM(Communication Port)
- 了解UART通信协议和USART与UART的区别,学会使用USART端口向串口发送数据
- 了解DMA(直接内存访问)技术,学会使用DMA配置串口发送数据
二、实验过程
1.调用 bsp_usart_dma库中的USART_Config函数。初始化USART。USART_Config 函数进行的操作有:打开串口GPIO的时钟、打开串口外设的时钟、将USART Tx的GPIO配置为推挽复用模式、将USART Rx的GPIO配置为浮空输入模式、配置波特率、配置针数据字长、配置停止位、配置校验位、配置硬件流控制、配置工作模式、使能串口。
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//打开串口GPIO的时钟
DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
//打开串口外设的时钟
DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
//将USART Tx的GPIO配置为推挽复用模式
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
//将USART Rx的GPIO配置为浮空输入模式
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
//配置串口的工作参数
//配置波特率
USART_InitStructure.USART_BaudRate = DEBUG_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(DEBUG_USARTx, &USART_InitStructure);
//使能串口
USART_Cmd(DEBUG_USARTx, ENABLE);
}
2.进行第一个实验,用直接配置串口的方式,向PC循环发送0-9十个数字。调用bsp_usart_dma库中的Usart_SendByte函数,使用for循环语句,向串口发送数字。使用串口调试助手查看发送过来的数据。
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
/*发送一个字节数据到USART*/
USART_SendData(pUSARTx,ch);
/*等待发送数据寄存器为空*/
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
3.进行第二个实验,在FLASH中存储一句话,并用DMA配置串口向PC传输。调用bsp_usart_dma库中的USARTx_DMA_Config函数,配置使用DMA模式。引用字符串变量SendBuff,调用string库中的strcpy函数将要发送的话存储在SendBuff中,此时SendBuff存储在FLASH中。调用stm32f10x_usart库中的USART_DMACmd函数,令USART1向DMA发出Tx请求,向串口发送存储在FLASH中的数据。使用串口调试助手查看发送过来的数据。
void USARTx_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
//开启DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//设置DMA源地址:串口数据寄存器地址*/
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_ADDRESS;
//内存地址(要传输的变量的指针)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
//方向:从内存到外设
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//传输大小
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;
//外设地址不增
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//外设地址自增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据单位
DMA_InitStructure.DMA_PeripheralDataSize =
DMA_PeripheralDataSize_Byte;
//内存数据单位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//DMA模式,一次或者循环模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//优先级:中
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
//禁止内存到内存的传输
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//配置DMA通道
DMA_Init(USART_TX_DMA_CHANNEL, &DMA_InitStructure);
//使能DMA
DMA_Cmd (USART_TX_DMA_CHANNEL,ENABLE);
}
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DMAREQ(USART_DMAReq));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the DMA transfer for selected requests by setting the DMAT and/or
DMAR bits in the USART CR3 register */
USARTx->CR3 |= USART_DMAReq;
}
else
{
/* Disable the DMA transfer for selected requests by clearing the DMAT and/or
DMAR bits in the USART CR3 register */
USARTx->CR3 &= (uint16_t)~USART_DMAReq;
}
}
三、附录
#include "stm32f10x.h"
#include "bsp_usart_dma.h"
#include "string.h"
extern uint8_t SendBuff[SENDBUFF_SIZE];
int main(void)
{
int i;
/* 初始化USART */
USART_Config();
printf("*********\n1120211738 张诺 第6次作业 实验1:\n用直接配置串口的方式,向PC循环发送0-9十个数字\n**********\n");
for(i=0;i<10;i++){
Usart_SendByte(DEBUG_USARTx,'0'+i);
Usart_SendByte(DEBUG_USARTx,'\n');
}
printf("**********\n1120211738 张诺 第6次作业 实验2:\n在FLASH中存储一句话,并用DMA配置串口向PC传输\n**********");
/* 配置使用DMA模式 */
USARTx_DMA_Config();
strcpy(SendBuff,"**********\n这是一句存储在FLASH中,且将会被使用DMA配置串口向PC的话\n**********");
/* USART1 向 DMA发出TX请求 */
USART_DMACmd(DEBUG_USARTx, USART_DMAReq_Tx, ENABLE);
}