赞
踩
实验目的:掌握LED5的GPIO口设置及STM32CumeMX的配置
实验内容:在实验版上点亮LED5
实验现象:实验A版上点亮LED5或者LED闪烁
配置过程:
将PC15配置为GPIO_Output,默认高电平(高电平不亮),上拉,标签改为LD5
修改完这几处后直接点击GENERATE CODE
代码:自己创建一个代码,将后续所用到的都写入其中
//名称:Final_LD5
//作用:控制LD5的熄灭,点亮和翻转
//参数:0->熄灭 1->点亮 2->翻转
//返回值:无
//备注:参数只能是0,1和2
#include "Final.h"
#include "gpio.h"
void Final_LD5(unsigned char state)
{
switch(state)
{
case 0:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);//写函数
break;
case 1:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);//写函数
break;
case 2:
HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);//翻转函数
break;
}
}
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Final.h"//此处为Final.c头文件,只能在BEGIN和END之间添加才不会被覆盖
/* USER CODE END Includes */
/* USER CODE BEGIN WHILE */
while (1)
{
Final_LD5(2);//LD5每500ms翻转一次
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
此时LD6绿灯亮代表的是选择A开发板(LD8绿灯亮),代码下载成果后A板的LD5如下图1是亮的
图1
实验目的:掌握K1,K2的GPIO口设置及STM32CumeMX的配置
实验内容:在实验版上点亮K1,K2
实验现象:实验A版上点亮K1,K2或者K1,K2闪烁
配置过程:下载器的配置和生成代码时的修改后续省略
将PA11和PA12配置为GPIO_Output,默认低电平(低电平不亮),不上拉不下拉,标签改为K1,K2
修改完这几处后直接点击GENERATE CODE
代码:
#include "Final.h"
#include "gpio.h"
//名称:Final_Relay
//作用:控制继电器的不工作,工作和翻转
//参数:number:继电器编号,1代表K1;2代表K2
//参数:state:工作状态,0代表不工作;1代表工作;2代表翻转
//返回值:无
//备注:
void Final_Relay(unsigned char number,unsigned char state)
{
switch(number)
{
case 1:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K1_GPIO_Port, K1_Pin);
break;
}
break;
case 2:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K2_GPIO_Port, K2_Pin);
break;
}
break;
}
}
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Final.h"//此处为Final.c头文件,只能在BEGIN和END之间添加才不会被覆盖
/* USER CODE END Includes */
/* USER CODE BEGIN WHILE */
while (1)
{
Final_Relay(1,1);
Final_Relay(2,1);
HAL_Delay(500);
/* USER CODE END WHILE */
如main函数中,K1,K2均为1(亮)
实验目的:掌握K1,K2的GPIO口设置及STM32CumeMX的配置
实验内容:在实验版上点亮K1,K2
实验现象:实验A版上点亮K1,K2或者K1,K2闪烁
配置过程:下载器的配置和生成代码时的修改后续省略
将PC14配置为GPIO_Intput,上拉,标签改为KEY
#include "Final.h"
#include "gpio.h"
void Final_LD5(unsigned char state)
{
switch(state)
{
case 0:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
break;
case 1:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
break;
case 2:
HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);
break;
}
}
void Final_Relay(unsigned char number,unsigned char state)
{
switch(number)
{
case 1:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K1_GPIO_Port, K1_Pin);
break;
}
break;
case 2:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K2_GPIO_Port, K2_Pin);
break;
}
break;
}
}
//名称:Final_Key
//作用:读取独立按键的键值
//参数:无
//返回值:0代表没有按下;1代表被按下
//备注:按下按键立刻有效
unsigned char Final_Key(void)
{
unsigned char value=0;//
if(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin)==RESET)//判断是否被按下
{
HAL_Delay(20);//消抖
if(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin)==RESET)//再次确定
{
value=1;
while(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin)==RESET);//释放按键
}
}
return value;
}
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Final.h"//此处为Final.c头文件,只能在BEGIN和END之间添加才不会被覆盖
/* USER CODE END Includes */
/* USER CODE BEGIN WHILE */
while (1)
{
if(Final_Key())
{
Final_LD5(2);
Final_Relay(1,2);
Final_Relay(2,2);
}
/* USER CODE END WHILE */
有main.c中函数中当按键按下时LD5和K1,K2进行一次翻转
将按键配置成GPIO_EXTI14,Falling(下降沿)触发,上拉,标签为KEY
将NVIC优先级配置成2,并且使能它
#include "Final.h"
#include "gpio.h"
void Final_LD5(unsigned char state)
{
switch(state)
{
case 0:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
break;
case 1:
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
break;
case 2:
HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);
break;
}
}
void Final_Relay(unsigned char number,unsigned char state)
{
switch(number)
{
case 1:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K1_GPIO_Port, K1_Pin);
break;
}
break;
case 2:
switch(state)
{
case 0:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_TogglePin(K2_GPIO_Port, K2_Pin);
break;
}
break;
}
}
//名称:HAL_GPIO_EXTI_Callback
//作用:用户的GPIO的中断的回调函数
//参数:系统参数 无需关系
//返回值:无
//备注:按下按键立刻有效
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin==KEY_Pin)
Final_LD5(2);
}
当按下user,LD5点亮
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。