首页 > 其他分享 >点亮LED灯_STM32第一课

点亮LED灯_STM32第一课

时间:2023-03-20 11:03:48浏览次数:30  
标签:__ LED Pin void STM32 第一课 GPIO HAL RCC

基本原理

初始化Hal库

HAL_Init();

 

系统时钟

SystemClock_Config();
 

GPIOB初始化:GPIOB模式为推挽输出,GPIO引脚为Pin_5、0、1代表红绿蓝LED,既不上拉也不下拉电阻,低速

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

控制GPIO输出:引脚GPIOB,5号0号1号,低电平点亮

HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, LED_ON);

以下是详细文件

main.c

#include "main.h"
#include "gpio.h"

void SystemClock_Config(void);

int main(void) {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    
    while (1) {
        //设置GPIOB中红绿蓝Led的亮灭
        HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, LED_ON);
        HAL_Delay(1000);
        HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, LED_OFF);

        HAL_GPIO_WritePin(GREEN_GPIO_Port, GREEN_Pin, LED_ON);
        HAL_Delay(1000);
        HAL_GPIO_WritePin(GREEN_GPIO_Port, GREEN_Pin, LED_OFF);

        HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, LED_ON);
        HAL_Delay(1000);
        HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, LED_OFF);
    }
}

/**
  * @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_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    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_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
        Error_Handler();
    }
}


/**
  * @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 */

  main.h

#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f1xx_hal.h"

#define GREEN_Pin       GPIO_PIN_0
#define GREEN_GPIO_Port GPIOB

#define BLUE_Pin        GPIO_PIN_1
#define BLUE_GPIO_Port  GPIOB

#define RED_Pin         GPIO_PIN_5
#define RED_GPIO_Port   GPIOB

#define LED_ON          GPIO_PIN_RESET
#define LED_OFF         GPIO_PIN_SET

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

 

gpio.c

#include "gpio.h"

void MX_GPIO_Init(void) {

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pins : PBPin PBPin PBPin */
    GPIO_InitStruct.Pin = GREEN_Pin | BLUE_Pin | RED_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

  

gpio.h

#ifndef __GPIO_H__
#define __GPIO_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "main.h"
void MX_GPIO_Init(void);

#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */

  

配置过程

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:__,LED,Pin,void,STM32,第一课,GPIO,HAL,RCC
From: https://www.cnblogs.com/Kaelthas/p/17235546.html

相关文章