- (简答题)
学习stm32中断、DMA通信原理和编程方法。
一.高低电平控制灯灭
1.创建过程
打开 STM32CubeMX,点击 ACCESS TO MCU SELECTOR
选择对应的芯片,这里我用到的是 STM32F103C8T6,在搜索框输入 STM32F103C8,双击搜索到的芯片
进行配置。
PB0为开关,选择GPIO_EXIT0
PA1为灯,设置为output,并将PA1的GPIO output level设为high
3.在keli加入以下代码
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == SWITCH_Pin){
//获取B5的电位
GPIO_PinState pinState = HAL_GPIO_ReadPin(SWITCH_GPIO_Port,SWITCH_Pin);
//低电位
if(pinState==GPIO_PIN_RESET)
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET);//把A5变为低电位,灯灭
//高电位
else
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);//把A5变为高电位,灯亮
}
}
4.生成hex文件。
5.找到hex文件进行烧录。boot0 置1
烧录成功后,断电。boot0置0,然后再继续。
二.采用串口中断方式重做上周的串口通信作业
1.创建工程
选择对应的芯片,这里我用到的是 STM32F103C8T6,在搜索框输入 STM32F103C8,双击搜索到的芯片
设置 RCC
设置SYS,将 Debug 改为 Serial Wire
设置USART1,设置MODE为异步通信,波特率为115200 Bits/s,传输数据长度为8 Bit,奇偶检验无,停止位1
设置NVIC,点击 NVIC Settings 一栏使能接收中断
修改项目名称、存储位置和编译器版本,创建项目
打开项目,点击 Open Project
在main函数前定义全局变量:
char c;//指令 s:停止 t:开始
char hello[]=“hello windows!\n”;//输出信息
char tips[]=“CommandError\n”;//提示1,命令错误
char tips1[]=“Start…\n”;//提示2,开始传输
char tips2[]=“Stop…\n”;//提示3,停止传输
int flag=0;//标志 s:停止发送 t:开始发送
在main函数中设置接收中断:
函数说明:
函数原型
HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
main函数中的while循环里面添加传输代码:
if(flag==1){
//发送信息
HAL_UART_Transmit(&huart1, (uint8_t *)&hello, strlen(hello),0xFFFF);
//延时
HAL_Delay(1000);
}
这里用到了 strlen() 函数,需要在 main.c 前面加上一句 #include “string.h”,来声明包含 strlen() 函数的头文件
在main函数下面重写中断处理函数:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
//当输入的指令为s时,发送提示并改变flag
if(c=='s'){
flag=0;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips2, strlen(tips2),0xFFFF);
}
//当输入的指令为t时,发送提示并改变flag
else if(c=='t'){
flag=1;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips1, strlen(tips1),0xFFFF);
}
//当输入不存在指令时,发送提示并改变flag
else {
flag=0;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips, strlen(tips),0xFFFF);
}
//重新设置中断
HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);
}
** main.c 修改部分代码:**
#include “main.h”
#include “usart.h”
#include “gpio.h”
#include <string.h>
void SystemClock_Config(void);
char c;//指令 s:停止 t:开始
char hello[]=“hello windows!\n”;//输出信息
char tips[]=“CommandError\n”;//提示1,命令错误
char tips1[]=“Start…\n”;//提示2,开始传输
char tips2[]=“Stop…\n”;//提示3,停止传输
int flag=0;//标志 s:停止发送 t:开始发送
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
//设置接受中断
HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);
//当flag为1时,每秒发送一次信息
//当flag为0时,停止
while (1)
{
if(flag==1){
//发送信息
HAL_UART_Transmit(&huart1, (uint8_t *)&hello, strlen(hello),0xFFFF);
//延时
HAL_Delay(1000);
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
//当输入的指令为s时,发送提示并改变flag
if(c=='s'){
flag=0;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips2, strlen(tips2),0xFFFF);
}
//当输入的指令为t时,发送提示并改变flag
else if(c=='t'){
flag=1;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips1, strlen(tips1),0xFFFF);
}
//当输入不存在指令时,发送提示并改变flag
else {
flag=0;
HAL_UART_Transmit(&huart1, (uint8_t *)&tips, strlen(tips),0xFFFF);
}
//重新设置中断
HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);
}
/* USER CODE END 4 /
/*
- @brief System Clock Configuration
- @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
- in the RCC_OscInitTypeDef structure.
/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
- @brief This function is executed in case of error occurrence.
- @retval None
/
void Error_Handler(void)
{
/ USER CODE BEGIN Error_Handler_Debug /
/ User can add his own implementation to report the HAL error return state /
__disable_irq();
while (1)
{
}
/ USER CODE END Error_Handler_Debug */
}
烧录
实验结果
三、采用串口中断方式发送“Hello windows!”
1.新建工程
在 mian.c 中写入以下代码
/* USER CODE BEGIN Header /
/*
- @file : main.c
- @brief : Main program body
- @attention
- Copyright © 2022 STMicroelectronics.
- All rights reserved.
- This software is licensed under terms that can be found in the LICENSE file
- in the root directory of this software component.
- If no LICENSE file comes with this software, it is provided AS-IS.
/
/ USER CODE END Header /
/ Includes ------------------------------------------------------------------*/
#include “main.h”
#include “usart.h”
#include “gpio.h”
#include “stdio.h”
#include “string.h”
/* Private includes ----------------------------------------------------------/
/ USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------/
/ USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------/
/ USER CODE BEGIN PD */
uint8_t aRxBuffer;
uint8_t Uart1_RxBuff[256];
uint8_t str1[20] = “stop stm32”;
uint8_t str2[20] = “go stm32”;
uint8_t Uart1_Rx_Cnt = 0;
uint8_t cAlmStr[] = “Êý¾ÝÒç³ö(´óÓÚ256)\r\n”;
unsigned int flag = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------/
/ USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------/
void SystemClock_Config(void);
/ USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------/
/ USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
- @brief The application entry point.
- @retval int
/
int main(void)
{
/ USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals /
MX_GPIO_Init();
MX_USART1_UART_Init();
/ USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t )&aRxBuffer, 1);
/ USER CODE END 2 */
/* Infinite loop /
/ USER CODE BEGIN WHILE /
while (1)
{
/ USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(flag == 1)
{
printf("ppm Hello windows!\r\n");
}
else
{
//printf("stop stm32 NO!\r\n");
}
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
- @brief System Clock Configuration
- @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
- in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 /
/ USER CODE BEGIN 4 /
/*
-
@brief Rx Transfer completed callbacks.
-
@param huart pointer to a UART_HandleTypeDef structure that contains
-
the configuration information for the specified UART module.
-
@retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef huart)
{
/ Prevent unused argument(s) compilation warning /
UNUSED(huart);
/ NOTE: This function Should not be modified, when the callback is needed,
the HAL_UART_TxCpltCallback could be implemented in the user file
*/
if (strcmp(Uart1_RxBuff, str1) == 0) flag = 0;
if (strcmp(Uart1_RxBuff, str2) == 0) flag = 1;//if(Uart1_RxBuff[0]‘g’) flag = 1;
//if(Uart1_RxBuff[0]‘s’) flag = 0;if(Uart1_Rx_Cnt >= 255) //Òç³öÅжÏ
{
Uart1_Rx_Cnt = 0;
memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
HAL_UART_Transmit(&huart1, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);
}
else
{
Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer; //½ÓÊÕÊý¾Ýת´æif((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //ÅжϽáÊøλ { HAL_UART_Transmit(&huart1, (uint8_t *)&Uart1_RxBuff, Uart1_Rx_Cnt,0xFFFF); //½«ÊÕµ½µÄÐÅÏ¢·¢ËͳöÈ¥ Uart1_Rx_Cnt = 0; memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff)); //Çå¿ÕÊý×é }
}
HAL_UART_Receive_IT(&huart1, (uint8_t )&aRxBuffer, 1); //ÔÙ¿ªÆô½ÓÊÕÖжÏ
}
/ USER CODE END 4 */
/* USER CODE END 4 */
/**
- @brief This function is executed in case of error occurrence.
- @retval None
/
void Error_Handler(void)
{
/ USER CODE BEGIN Error_Handler_Debug /
/ User can add his own implementation to report the HAL error return state /
__disable_irq();
while (1)
{
}
/ USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
- @brief Reports the name of the source file and the source line number
-
where the assert_param error has occurred.
- @param file: pointer to the source file name
- @param line: assert_param error line source number
- @retval None
*/
void assert_failed(uint8_t file, uint32_t line)
{
/ USER CODE BEGIN 6 /
/ User can add his own implementation to report the file name and line number,
ex: printf(“Wrong parameters value: file %s on line %d\r\n”, file, line) /
/ USER CODE END 6 /
}
#endif / USE_FULL_ASSERT */
编译烧录
实验效果
四、串口DMA接收发数据
创建工程
下面我们在CubeMx中配置工程,系统、时钟等跳过
打开USART1及DMA模式
选择USART1_RX一栏,配置DMA参数
选择USART1_TX一栏,配置DMA参数
在Pinout&Configuration–Connectivity–USART1–NVIC Settings中打开中断
在while循环中添加
实机演示
五、总结
本次实验通过中断到串口DMA通信,我们认识到DMA的出现就是为了解决批量数据的输入/输出问题。DMA是指外部设备不通过CPU而直接与系统内存交换数据的接口技术。这样数据的传送速度就取决于存储器和外设的工作速度。