STM32_HAL库(一):点亮LED

STM32_HAL库(一):点亮LED

  • LED.h
#ifndef __LED_H

#define __LED_H

#include "stm32f4xx.h"

#define LED1_PORT		GPIOF
#define LED1_PIN		GPIO_PIN_9
#define LED2_PORT		GPIOF
#define LED2_PIN		GPIO_PIN_10

void LED_Init(void);
void LED_Open(uint16_t GPIO_PIN);

#endif
  • LED.c
#include "led.h"

void LED_Init()
{
	GPIO_InitTypeDef GPIO_InitStructure;		//define structure

	__HAL_RCC_GPIOF_CLK_ENABLE();						//ENABLE GPIOF RCC (LED1, LED2)
	
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
	GPIO_InitStructure.Pull = GPIO_PULLUP;
	
	/* init LED1 & LED2 */
	GPIO_InitStructure.Pin = LED1_PIN | LED2_PIN;
	HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
	HAL_GPIO_WritePin(GPIOF, LED1_PIN | LED2_PIN, GPIO_PIN_SET);
}

void LED_Open(uint16_t GPIO_PIN)
{
	HAL_GPIO_WritePin(GPIOF, GPIO_PIN, GPIO_PIN_RESET);
}

  • main.c
/* USER CODE BEGIN Includes */
#include "LED.h"		//1
/* USER CODE END Includes */

/* USER CODE BEGIN Init */
LED_Init();				//2
 /* USER CODE END Init */
 
/* USER CODE BEGIN WHILE */
while (1)
{
	LED_Open(LED1_PIN | LED2_PIN);		//3
/* USER CODE END WHILE */

你可能感兴趣的:(STM32)