嵌入式使用stm32f10x.h,写程序,自己确定功能中断(NVIC,DMA)通过按键启动传输,LED表示传输状态功能分析,流程图,代码
时间: 2024-03-22 15:42:44 浏览: 108
以下是使用STM32 Standard Peripheral Library写的一个示例程序,该程序使用了STM32F10x系列的头文件,实现了通过按键启动传输,LED指示传输状态的功能。
程序的主要思路是使用中断来控制按键输入和DMA传输,通过LED指示传输状态。具体的流程如下:
1. 初始化NVIC和DMA
2. 初始化按键输入和LED输出端口
3. 等待按键按下
4. 当按键按下时,启动DMA传输
5. 等待DMA传输完成
6. 将LED指示为传输完成的状态
7. 等待按键释放
8. 返回步骤3
以下是参考代码:
```c
#include "stm32f10x.h"
#define BUFFER_SIZE 256
uint8_t buffer[BUFFER_SIZE];
uint8_t data_ready = 0;
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
// 按键按下,启动DMA传输
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4, BUFFER_SIZE);
DMA_Cmd(DMA1_Channel4, ENABLE);
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void DMA1_Channel4_IRQHandler(void) {
if (DMA_GetITStatus(DMA1_IT_TC4) != RESET) {
// DMA传输完成
GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET);
data_ready = 1;
DMA_ClearITPendingBit(DMA1_IT_TC4);
}
}
int main() {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// 初始化按键输入和LED输出端口
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// 初始化NVIC和DMA
NVIC_InitTypeDef NVIC_InitStruct = {0};
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
DMA_InitTypeDef DMA_InitStruct = {0};
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&GPIOC->ODR;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)buffer;
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_Channel4, &DMA_InitStruct);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
// 开启中断
EXTI_InitTypeDef EXTI_InitStruct = {0};
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
while (1) {
// 等待按键按下
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) != Bit_RESET) {}
// 等待DMA传输完成
while (!data_ready) {}
// 将LED指示为传输完成的状态
GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET);
data_ready = 0;
// 等待按键释放
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) != Bit_SET) {}
}
}
```
需要注意的是,这只是一个参考代码,您需要根据实际需求进行修改和完善。同时,由于不同的STM32型号可能存在差异,部分代码可能需要根据实际情况进行修改。
阅读全文