编写程序:利用串口采用DMA方式发送数据到主存,统计发送数据中包含的字符数
时间: 2024-03-02 08:51:50 浏览: 96
以下是一个基于STM32的例子程序,可以实现串口采用DMA方式发送数据到主存并统计发送数据中包含的字符数:
```c
#include "stm32f4xx.h"
#define BUFFER_SIZE 100
uint8_t buffer[BUFFER_SIZE];
uint32_t count = 0;
void DMA2_Stream7_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream7, DMA_IT_TCIF7) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7);
count = 0;
for (int i = 0; i < BUFFER_SIZE; i++)
{
if (buffer[i] != 0)
count++;
else
break;
}
}
}
int main(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = DMA2_Stream7_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
DMA_InitTypeDef DMA_InitStruct;
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;
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_VeryHigh;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream7, &DMA_InitStruct);
DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE);
while (1)
{
USART_SendData(USART2, 'a');
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
DMA_Cmd(DMA2_Stream7, ENABLE);
while (DMA_GetFlagStatus(DMA2_Stream7, DMA_FLAG_TCIF7) == RESET);
DMA_Cmd(DMA2_Stream7, DISABLE);
}
}
```
程序的基本思路是:在主循环中,通过USART_SendData函数向串口发送数据,然后启动DMA传输,将接收到的数据存储到buffer数组中,当DMA传输完成时,遍历buffer数组,统计其中不为0的元素个数,即为发送数据中包含的字符数。注意,由于使用了DMA传输,因此在中断服务函数中需要清除DMA_IT_TCIF7中断标志位,并在主循环中使用DMA_GetFlagStatus函数检查DMA传输是否完成。
需要注意的是,具体的代码实现可能会因为不同的芯片和开发环境而有所不同。此外,在实际应用中,还需要考虑数据传输的正确性和可靠性,以及串口和DMA的配置和使用方法等问题。
阅读全文