STM32F407通过按键触发外部中断,用DMA方式从串口1发送一个100字节的信息,按一次发一次
时间: 2023-05-12 11:06:42 浏览: 88
可以使用STM32F407的DMA和串口1模块来实现这个功能。首先,需要配置外部中断和DMA通道,以便在按键按下时触发DMA传输。然后,需要编写一个发送函数,将100字节的信息发送到串口1。最后,将发送函数与DMA通道关联,以便在DMA传输完成后自动调用发送函数。
以下是一个可能的实现:
```c
#include "stm32f4xx.h"
#define BUFFER_SIZE 100
uint8_t buffer[BUFFER_SIZE];
void send_data(void) {
// 发送数据到串口1
for (int i = 0; i < BUFFER_SIZE; i++) {
USART_SendData(USART1, buffer[i]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
int main(void) {
// 初始化GPIO和外部中断
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 初始化DMA和串口1
DMA_InitTypeDef DMA_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream7, &DMA_InitStruct);
USART_InitStruct.USART_BaudRate = 115200;
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_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
// 启动DMA和串口1
DMA_Cmd(DMA2_Stream7, ENABLE);
USART_Cmd(USART1, ENABLE);
while (1) {
// 等待外部中断触发
}
}
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
// 外部中断触发,启动DMA传输
DMA_Cmd(DMA2_Stream7, DISABLE);
DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7);
DMA_SetCurrDataCounter(DMA2_Stream7, BUFFER_SIZE);
DMA_Cmd(DMA2_Stream7, ENABLE);
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
```
请注意,此代码仅供参考,可能需要根据具体硬件和应用程序进行修改。
阅读全文