一、实验内容和任务
- 用stm32F103核心板的GPIOA端一管脚接一个LED,GPIOB端口一引脚接一个开关(用杜邦线模拟代替)。采用中断模式编程,当开关接高电平时,LED亮灯;接低电平时,LED灭灯。2. 采用串口中断方式重做上周的串口通信作业,分别实现:1)当stm32接收到字符“s”时,停止持续发送“hello windows!”; 当接收到字符“t”时,持续发送“hello windows!”(提示:采用一个全局标量做信号灯);2)当stm32接收到字符“stop stm32!”时,停止持续发送“hello windows!”; 当接收到字符“go stm32!”时,持续发送“hello windows!”(提示:要将接收到的连续字符保存到一个字符数组里,进行判别匹配。写一个接收字符串的函数。
二、实验要求
1. 分组要求:每个学生独立完成,即1人1组。
2. 程序及报告文档要求:具有较好的可读性,如叙述准确、标注明确、截图清晰等。
3.项目代码上传github,同时把项目完整打包为zip文件,与实验报告(Markdown源码及PDF文件)、作业博客地址一起提交到学习通。
三. 实验过程介绍 (此处可以填博客内容)
stm32外部中断模式控制灯亮灭
一、在STM32CubeMx软件中配置项目
1.引脚配置
A4输出控制灯的亮灭,设置为GPIO_Output
A1持续输出高电平,设置同上
A7持续输出低电平,设置同上
B5模拟开关,设置为GPIO_EXTI5
2.配置EXIT
3.配置SYS
4.配置GPIO
把A1配置为高电平、A7配置为低电平
把B5中断配置为上升沿和下降沿都触发
User Label可以为当前引脚设置一个别名,方便后续自己调用。
5.勾上,并完成项目名设置等
二、通过KEil配置代码
1.打开生成的项目,找到stm32f1xx_it.c
2.找到EXTI9_5_IRQHandler这个函数,选中 HAL_GPIO_EXTI_IRQHandler这个语句按F12跳到该函数
无法跳到该函数的话编译一下就行。
3.往下找到HAL_GPIO_EXTI_Callback这个函数
该函数是给用户自己重写的,可以在这里根据不同的中断来执行不同的处理。在这里我们需要根据B5的不同中断来实现A4的亮灭。
函数代码
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_A4_GPIO_Port,LED_A4_Pin,GPIO_PIN_RESET);//把A4变为低电位
//高电位
else
HAL_GPIO_WritePin(LED_A4_GPIO_Port,LED_A4_Pin,GPIO_PIN_SET);//把A4变为高电位
}
}
4.编译生成hex文件
5.通过MCUISP进行烧录
三、效果
说明:
因为抖动的原因,当B5什么也不接入的时候,LED灯一直在频繁闪,看起来是亮的,但是不是很亮。
当B5接入高电平后LED明显变亮。
当B5接低电平后LED直接熄灭。
HAL库中断方式进行串口通信
一、通过CubeMX配置项目
1.设置RCC
2.设置SYS
3.设置USART
4.设置NVIC
5.创建项目
二、在keil配置代码
1.打开通过CubeMX生成的项目
2.在main函数前定义全局变量和添加头文件
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"
char myBuffer[] = "I have gotten your message: "; //用户提示信息
char Enter[] = "\r\n"; //回车换行
char getBuffer[100]; //用户自定义的缓冲区
char value;
char str1[] = "go stm32!";
char str2[] = "stop stm32!";
char c;//指令 0:停止 1:开始
char message[]="hello Windows\n";//输出信息
char tips[]="CommandError\n";//提示1
char tips1[]="Start.....\n";//提示2
char tips2[]="Stop......\n";//提示3
int flag=1;//标志 0:停止发送 1.开始发送
int countofGetBuffer=0;
3.修改main.c主函数中while循环并在循环前设置接收中断
HAL_UART_Receive_IT(&huart1, (uint8_t *)&value, 1);
while (1)
{
if(flag==1){
//发送信息
HAL_UART_Transmit(&huart1, (uint8_t *)&message,COUNTOF(message),0xFFFF);
//延时
HAL_Delay(1000);
}else if(flag==0){
HAL_Delay(1000);
}
}
4.在main.c函数下部重写中断回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart1){
HAL_UART_IRQHandler(huart1);
getBuffer[countofGetBuffer++]=value;
if(strcmp(getBuffer,str1)==0){
flag = 1;
HAL_UART_Transmit(huart1, (uint8_t *)&tips1, COUNTOF(tips1),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}else if(strcmp(getBuffer,str2)==0){
flag = 0;
HAL_UART_Transmit(huart1, (uint8_t *)&tips2, COUNTOF(tips2),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}
HAL_UART_Receive_IT(huart1, (uint8_t *)&value,1);
}
5.main.c函数全部代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 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 "string.h"
char myBuffer[] = "I have gotten your message: "; //用户提示信息
char Enter[] = "\r\n"; //回车换行
char getBuffer[100]; //用户自定义的缓冲区
char value;
char str1[] = "go stm32!";
char str2[] = "stop stm32!";
char c;//指令 0:停止 1:开始
char message[]="hello Windows\n";//输出信息
char tips[]="CommandError\n";//提示1
char tips1[]="Start.....\n";//提示2
char tips2[]="Stop......\n";//提示3
int flag=1;//标志 0:停止发送 1.开始发送
int countofGetBuffer=0;
/* 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 */
/* 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 */
/**ep
* @brief The application entry point.
* @retval int
*/
#define COUNTOF(a) (sizeof(a)/sizeof(*(a)))
//jkl
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();
HAL_UART_Receive_IT(&huart1, (uint8_t *)&value, 1);
//HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 10);
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(flag==1){
//发送信息
HAL_UART_Transmit(&huart1, (uint8_t *)&message,COUNTOF(message),0xFFFF);
//延时
HAL_Delay(1000);
}else if(flag==0){
HAL_Delay(1000);
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart1){
HAL_UART_IRQHandler(huart1);
getBuffer[countofGetBuffer++]=value;
if(strcmp(getBuffer,str1)==0){
flag = 1;
HAL_UART_Transmit(huart1, (uint8_t *)&tips1, COUNTOF(tips1),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}else if(strcmp(getBuffer,str2)==0){
flag = 0;
HAL_UART_Transmit(huart1, (uint8_t *)&tips2, COUNTOF(tips2),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}
HAL_UART_Receive_IT(huart1, (uint8_t *)&value,1);
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1); //该函数会清空中断标志,取消中断使能,并间接调用回调函数
HAL_UART_Receive_IT(&huart1, (uint8_t *)getBuffer,10); //添加的一行代码
}
/**
* @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 */
}
#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 */
6.编译并烧录
7.效果
DMA (串口DMA发送接收)
一、通过CubeMX配置项目
1设置RCC
2设置串口
1点击USATR1
2设置MODE为异步通信(Asynchronous)
3基础参数:波特率为115200 Bits/s。传输数据长度为8 Bit。奇偶检验无,停止位1 接收和发送都使能
4GPIO引脚自动设置 USART1_RX/USART_TX
5 NVIC Settings 一栏使能接收中断
3 DMA设置
-
点击DMASettings 点击 Add 添加通道
-
选择USART_RX USART_TX 传输速率设置为中速
-
DMA传输模式为正常模式
-
DMA内存地址自增,每次增加一个Byte(字节)
-
1DMA基础设置
右侧点击System Core 点击DMA
时钟源设置
4项目文件设置
配置下载工具
新建的工程所有配置都是默认的 我们需要自行选择下载模式,勾选上下载后复位运行
在main.C中添加:
/* USER CODE BEGIN Init */
uint8_t Senbuff[] = "\r\n**** Serial Output Message by DMA ***\r\n UART DMA Test \r\n Zxiaoxuan"; //定义数据发送数组
/* USER CODE END Init */
while循环:
while (1)
{
/* USER CODE END WHILE */
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)Senbuff, sizeof(Senbuff));
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
串口助手测试正常:
五、参考
https://blog.csdn.net/qq_47281915/article/details/121024427
https://blog.csdn.net/as480133937/article/details/104827639/