STM32 是一款基于ARM Cortex-M3内核的32位MCU,主频最高可达72M。最近因为要在车机上集成TPMS功能, 便开始着手STM32的开发工作,STM32F10x系列共有5个串口(USART1~USART5),支持DMA方式通信,DMA方式由于不需要CPU的参与,而是直接由DMA控制器完成串口数据的读写,因而可以很大程度的提高CPU的利用率。在使用STM32串口之前需要做一系列的初始化工作:
1.RCC(复位和时钟控制寄存器)初始化,启用GPIO、DMA、USART时钟。
2.NVIC(嵌套向量中断控制寄存器)初始化,完成各个硬件中断的配置。
3.USART初始话,配置串口,设置DMA通道等。
4.DMA初始化,完成DMA的配置。
源代码:
配置:
//---------------------串口功能配置---------------------
//打开串口对应的外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
//启动DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//DMA发送中断设置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA1通道4配置
DMA_DeInit(DMA1_Channel4);
//外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1-》DR);
//内存地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;
//dma传输方向单向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//设置DMA在传输时缓冲区的长度
DMA_InitStructure.DMA_BufferSize = 100;
//设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//设置DMA的内存递增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据字长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//内存数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//设置DMA的传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//设置DMA的优先级别
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//设置DMA的2个memory中的变量互相访问
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
//使能通道4
//DMA_Cmd(DMA1_Channel4, ENABLE);
//初始化参数
//USART_InitStructure.USART_BaudRate = DEFAULT_BAUD;
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_InitStructure.USART_BaudRate = DEFAULT_BAUD;
//初始化串口
USART_Init(USART1,&USART_InitStructure);
//TXE发送中断,TC传输完成中断,RXNE接收中断,PE奇偶错误中断,可以是多个
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置UART1中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
//采用DMA方式发送
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
//启动串口
USART_Cmd(USART1, ENABLE);
//设置IO口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//串口1的管脚初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
//设置IO口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管脚9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
中断函数:
//串口1DMA方式发送中断
void DMA1_Channel4_IRQHandler(void)
{
//清除标志位
DMA_ClearFlag(DMA1_FLAG_TC4);
//DMA_ClearITPendingBit(DMA1_FLAG_TC4);
//DMA1-》IFCR |= DMA1_FLAG_TC4;
//关闭DMA
DMA_Cmd(DMA1_Channel4,DISABLE);
//DMA1_Channel4-》CCR &= ~(1《《0);
//允许再次发送
Flag_Uart_Send = 0;
}
发送测试:
//串口DMA发送测试
Uart_Send_Buffer[0] = 1;
Uart_Send_Buffer[1] = 2;
Uart_Send_Buffer[2] = 3;
Uart_Send_Buffer[3] = 4;
Uart_Send_Buffer[4] = 5;
i = 1;
while (1)
{
//检查串口是否可以发送
while (Flag_Uart_Send);
Flag_Uart_Send = 1;
//设置传输数据长度
DMA_SetCurrDataCounter(DMA1_Channel4,i);
//打开DMA
DMA_Cmd(DMA1_Channel4,ENABLE);
i++;
if (i 》 5)
{
i = 1;
}
}
评论
查看更多