STM32F103C8T6串口1的DMA,如何实现上位机串口助手发送指令到单片机 ?需要用到定时中断吗?
时间: 2023-06-25 16:02:38 浏览: 288
串口助手-简易串口助手-带时间戳的串口助手-极简串口助手-V1.5 自动保存配置参数 能显示收发时间方便调试
3星 · 编辑精心推荐
在使用DMA实现串口通信时,定时中断并不是必须的,但是需要注意一些细节:
1. 首先需要配置串口1和DMA的相关寄存器,使它们能够正常工作。具体步骤如下:
- 配置串口1的波特率、数据位、停止位等参数;
- 使能串口1和DMA时钟;
- 配置DMA通道,指定源地址为串口1的数据寄存器(USART_DR),目的地址为接收缓冲区,传输数据长度为1个字节;
- 使能DMA通道。
2. 然后在主程序中,需要开启串口1的接收中断,并在中断服务函数中处理接收到的数据。具体步骤如下:
- 开启串口1的接收中断;
- 在中断服务函数中,判断接收到的数据是否是指令结束符,如果是,则将接收缓冲区的数据拷贝到一个新的缓冲区中,并清空接收缓冲区;
- 在主程序中,根据接收到的指令进行相应的处理。
下面是一个简单的示例代码,供参考:
```c
#include "stm32f10x.h"
#define BUFFER_SIZE 64
uint8_t rxBuffer[BUFFER_SIZE];
uint8_t cmdBuffer[BUFFER_SIZE];
uint8_t rxIndex = 0;
uint8_t cmdIndex = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rxBuffer[rxIndex++] = USART_ReceiveData(USART1);
if (rxBuffer[rxIndex - 1] == '\n' || rxIndex >= BUFFER_SIZE)
{
memcpy(cmdBuffer, rxBuffer, rxIndex);
cmdIndex = rxIndex;
rxIndex = 0;
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
}
}
void DMA1_Channel4_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC4) != RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC4);
DMA_Cmd(DMA1_Channel4, DISABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
}
int main(void)
{
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
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(USART1, &USART_InitStructure);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)rxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
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_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
while (1)
{
// 处理接收到的指令
if (cmdIndex > 0)
{
// do something
cmdIndex = 0;
}
}
}
```
阅读全文