使用stm32f10x的固件库 编写 stm32f103RCT6 串口2 DMA中断通讯 配置 通信代码
时间: 2023-08-14 21:04:37 浏览: 168
STM32F103RCT6核心的库函数压缩包.zip
好的,我来解答你的问题。
首先,你需要使用STM32CubeMX来配置你的串口。在配置中,选择串口2,并启用DMA和中断。你需要将USART2的时钟使能,设置波特率和数据位、校验位、停止位等参数。在DMA设置中,选择通道和传输方向以及DMA传输的缓存区大小等。在中断设置中,选择接收中断和错误中断,并编写相应的中断处理程序。
接下来,我为你提供一个简单的串口通信代码的示例,你可以参考一下:
```c
#include "stm32f10x.h"
#define BUFFER_SIZE 256
uint8_t RxBuffer[BUFFER_SIZE];
uint8_t TxBuffer[BUFFER_SIZE];
int main(void)
{
USART_InitTypeDef USART_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
/* Enable clocks for USART2 and DMA1 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Configure USART2 */
USART_StructInit(&USART_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
/* Configure DMA for USART2 Rx */
DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStruct);
DMA_Cmd(DMA1_Channel6, ENABLE);
/* Configure DMA for USART2 Tx */
DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel7, &DMA_InitStruct);
DMA_Cmd(DMA1_Channel7, ENABLE);
/* Configure NVIC for DMA1_Channel6 */
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel6_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/* Configure NVIC for DMA1_Channel7 */
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel7_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/* Enable USART2 DMA Rx and Tx */
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
/* Enable USART2 DMA interrupts */
DMA_ITConfig(DMA1_Channel6, DMA_IT_TC, ENABLE);
DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE);
while (1)
{
/* Main loop */
}
return 0;
}
void DMA1_Channel6_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC6))
{
DMA_ClearITPendingBit(DMA1_IT_TC6);
/* Rx DMA transfer complete */
}
}
void DMA1_Channel7_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC7))
{
DMA_ClearITPendingBit(DMA1_IT_TC7);
/* Tx DMA transfer complete */
}
}
```
这是一个基本的串口通信代码,你可以根据自己的需求进行修改和完善。
阅读全文