配置CubeMX
初始状态使led灯全灭,高电平为无效状态
时钟树配置
配置project
2.代码部分
向里面添加.c文件,怎么自动生成.h文件?
先在inc文件中创建.h文件,然后把代码粘贴好,编译,led.c展开就有.h文件了
led模块
led.c
void LED_Ctrl(uint16_t LED, uint8_t LED_Status)
{
if(LED_Status == ON){ //亮灯
HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_RESET);//控制传过来的led亮
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//PD2控制
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
else{ //灭灯
HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
}
lcd.h
#include "stm32g4xx_hal.h"
#define ON 1
#define OFF 0
#define LED1 GPIO_PIN_8
#define LED2 GPIO_PIN_9
#define LED3 GPIO_PIN_10
#define LED4 GPIO_PIN_11
#define LED5 GPIO_PIN_12
#define LED6 GPIO_PIN_13
#define LED7 GPIO_PIN_14
#define LED8 GPIO_PIN_15
#define LEDALL GPIO_PIN_All
void LED_Ctrl(uint16_t LED, uint8_t LED_Status);
key模块
key.c
#include "key.h"
uint8_t Key_Scan(void)
{
char key_value = 0;
if(Press(KB1) == 0) { //key1
HAL_Delay(10); //去抖
if(Press(KB1) == 0){
key_value = 1;
while(Press(KB1) == 0); //按键松开效果 ,直到松开按键为止
}
}else if(Press(KB2) == 0) { //key2
HAL_Delay(10);
if(Press(KB2) == 0){
key_value = 2;
while(Press(KB2) == 0);
}
}
else if(Press(KB3) == 0) { //key3
HAL_Delay(10);
if(Press(KB3) == 0){
key_value = 3;
while(Press(KB3) == 0);
}
}
else if(Press(KB4) == 0) { //key4
HAL_Delay(10);
if(Press(KB4) == 0){
key_value = 4;
while(Press(KB4) == 0);
}
}
return key_value;
}
key.h
#include "stm32g4xx_hal.h"
#define Press(x) (HAL_GPIO_ReadPin(x))
//返回按下的键
#define KB1 GPIOB, GPIO_PIN_0
#define KB2 GPIOB, GPIO_PIN_1
#define KB3 GPIOB, GPIO_PIN_2
#define KB4 GPIOA, GPIO_PIN_0
uint8_t Key_Scan(void);
main.c
//头文件部分
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "led.h"
#include "key.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
//变量
//当前停车状况,两类车分别停多少量,空闲车位剩几个
int CNBR_Size = 0;
int VNBR_Size = 0;
int IDEL_Size = 8;
//两类车的停车单价
float CNBR_Price = 3.50f;
float VNBR_Price = 2.00f;
//为了在lcd屏幕上显示而定义的字符串数组
char Show_CNBR[20];
char Show_VNBR[20];
char Show_IDLE[20];
char Show_CNBR_Price[20];
char Show_VNBR_Price[20];
//Flag
char Show_Flag = 1; //LCD界面切换标志
//函数声明
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Var_Update(void);
void LCD_Proc(void);
void Key_Proc(void);
void Led_Proc(void);
/* USER CODE END PFP */
main函数主程序
初始化
/* USER CODE BEGIN 2 */
//lcd初始化
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
//问题:LED和LCD相互干扰
//解决方案:
//1.LED初始化应在LCD初始化之后
LED_Ctrl(LEDALL, OFF); //可能在CubeMX上初始化后上电灯还是全亮
/* USER CODE END 2 */
//while函数里的函数调用
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
Var_Update();
LCD_Proc();
Key_Proc();
Led_Proc();
/* USER CODE BEGIN 3 */
}
/* USER CODE BEGIN 4 */
//Update Variables
void Var_Update(void)
{
sprintf(Show_CNBR," CNBR:%d ", CNBR_Size);//把后面的字符串全部赋给Show_,,,
sprintf(Show_VNBR," VNBR:%d ", VNBR_Size);
sprintf(Show_IDLE," IDLE:%d ", IDEL_Size);
//这里换过来顺序就不行了,先写整型部分,再写浮点型
sprintf(Show_CNBR_Price," CNBR:%.2f ", CNBR_Price);
sprintf(Show_VNBR_Price," VNBR:%.2f ", VNBR_Price);
}
//LCD processing function
void LCD_Proc(void)
{
if(Show_Flag == 1){
LCD_DisplayStringLine(Line1, (uint8_t *)" Data");
LCD_DisplayStringLine(Line3, (uint8_t *)Show_CNBR);
LCD_DisplayStringLine(Line5, (uint8_t *)Show_VNBR);
LCD_DisplayStringLine(Line7, (uint8_t *)Show_IDLE);
}else if(Show_Flag == 2){
LCD_DisplayStringLine(Line1, (uint8_t *)" Para");
LCD_DisplayStringLine(Line3, (uint8_t *)Show_CNBR_Price);
LCD_DisplayStringLine(Line5, (uint8_t *)Show_VNBR_Price);
LCD_DisplayStringLine(Line7, (uint8_t *)" ");
}
}
//Key processing function
void Key_Proc(void)
{
uint8_t cur_key = 0;
cur_key = Key_Scan();
switch(cur_key)
{
case 1:
Show_Flag++;
if(Show_Flag == 3)
Show_Flag = 1;
break;
case 2:
if(Show_Flag == 2)
{
CNBR_Price += 0.5f;
VNBR_Price += 0.5f;
}
break;
case 3:
if(Show_Flag == 2)
{
CNBR_Price -= 0.5f;
VNBR_Price -= 0.5f;
if(CNBR_Price < 0)
CNBR_Price = 0.0f;
if(VNBR_Price < 0)
VNBR_Price = 0.0f;
}
break;
case 4:
break;
default:
;
}
}
//根据题目要求,还有空车位的时候LED1亮
void Led_Proc(void)
{
if(IDEL_Size > 0)
LED_Ctrl(LED1, ON);
else
LED_Ctrl(LED1, OFF);
}
/* USER CODE END 4 */