STM32 learning: ① water lamp

1. Water lamp

1.1 Introduction

Development board: ISKBoard
CPU: STM32L431RCT6
Processor: Arm® Cortex®-M4
The left end of the processor is connected to a 3.3V power supply. To make the LED light up, it needs to be connected to the PC9 (red), PC6 (green), and PB2 (blue) pins respectively. If you want to turn off the low level, you need to connect to the high level, so we can control the LED light on and off according to the output level.
insert image description here

1.2 configuration

① Configure and enable CPU external crystal oscillator (RCC)
insert image description here

② Configure the CPU clock tree
From the schematic diagram, we can see that the CPU uses an 8MHz crystal oscillator
insert image description here
insert image description here

③Configure the GPIO pins
to configure the pins of the three-color LED, set the default level to high level (initial state is off), select the mode to push-pull output (Output Push Pull), and name each pin according to the color of the light, easy to use.
insert image description here

1.3 Program modification

The code addition must be between USER CODE BEGIN and USER CODE END, otherwise the added non-standard code will be deleted after the code is regenerated.

1.3.1 Modify gpio.c code

Add the LED control function in gpio.c
① turn_led: The function of controlling the LED on and off, the value of the parameter which is the LED in the enumeration, specify the LED to be controlled, the parameter status is to control the on/off of the LED, and the value is the macro definition ② blink_led
: Control the blinking function of the LED, the parameter which is the same as turn_led, and the parameter interval is the blinking interval time in milliseconds.

/* USER CODE BEGIN 2 */
typedef struct gpio_s
{
    
    
	GPIO_TypeDef	*group;//引脚的类别,如GPIOA、GPIOB
	uint16_t		pin;//引脚号
}gpio_t;

gpio_t leds[LedMax] =
{
    
    
	{
    
    RedLed_GPIO_Port,		RedLed_Pin},
	{
    
    GreenLed_GPIO_Port,	GreenLed_Pin},
	{
    
    BlueLed_GPIO_Port,		BlueLed_Pin},
};

void turn_led(int which,int status)//点亮LED灯
{
    
    
	GPIO_PinState	level;//定义引脚的状态

	if(which >=	LedMax)
		return ;

	level = OFF == status ? GPIO_PIN_SET : GPIO_PIN_RESET;

	HAL_GPIO_WritePin(leds[which].group,leds[which].pin,level);
}

void blink_led(int which,uint32_t interval)//使LED灯闪烁
{
    
    
	turn_led(which,ON);
	HAL_Delay(interval);

	turn_led(which,OFF);
	HAL_Delay(interval);
}

/* USER CODE END 2 */

1.3.2 Modify gpio.h

Declare the functions used in gpio.c, as well as enumerate and define macros.


/* USER CODE BEGIN Prototypes */
enum
{
    
    
	RedLed,
	GreenLed,
	BlueLed,
	LedMax,
};

#define OFF		0
#define ON		1

extern void turn_led(int which,int status);
extern void blink_led(int which,uint32_t interval);
/* USER CODE END Prototypes */

1.3.2 Modify main.c

Call blink_led() directly in main.c to blink the three-color LEDs respectively.

  /* USER CODE BEGIN WHILE */

  while (1)
  {
    
    
	  blink_led(BlueLed, 500);
	  blink_led(RedLed, 500);
	  blink_led(GreenLed, 500);
    /* USER CODE END WHILE */

1.4 Effect Realization

The final effect is that the blue, red, and green LED lights flash in sequence.

Guess you like

Origin blog.csdn.net/xll102500/article/details/131326598