stm32红绿灯源代码示例(附带Proteus电路图)

本代码不能直接用于红路灯,只是提供一个思路

cpp 复制代码
#include "main.h"
#include "gpio.h"
void SystemClock_Config(void);
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();   //我要使用PB     全部的PB都激活了	
  /*Configure GPIO pin : PB0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  while (1)
  {
		int i=0;
		GPIOB->ODR=0X0;
		GPIOB->ODR=GPIO_PIN_2;
		HAL_Delay(5000);
		for(i=0;i<5;i++)
		{
			GPIOB->ODR=GPIO_PIN_1;
			HAL_Delay(500);
			GPIOB->ODR=0;
			HAL_Delay(500);
		}
		GPIOB->ODR=GPIO_PIN_0;
		HAL_Delay(5000);

				GPIOB->ODR=0X0;
		GPIOB->ODR=GPIO_PIN_3;
		HAL_Delay(5000);
		for(i=0;i<5;i++)
		{
			GPIOB->ODR=GPIO_PIN_4;
			HAL_Delay(500);
			GPIOB->ODR=0;
			HAL_Delay(500);
		}
		GPIOB->ODR=GPIO_PIN_5;
		HAL_Delay(5000);
		
  }
  /* USER CODE END 3 */
}
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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

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

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

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

可以看到如果只是输入输出,已经跟51单片机差不多了,就是操作寄存器。而且提供了一个很方便的东西就是GPIO_PIN_X这样的宏,已经不用我自己来写了。不过我写的时候并不知道这个东西

相关推荐
吉哥机顶盒刷机2 小时前
晶晨芯片机顶盒与海思芯片机顶盒刷机区别详解
经验分享·嵌入式硬件·刷机
森利威尔电子-2 小时前
SL4011:2.7V-12V输入,12.8V输出高效率升压型DC-DC转换器
单片机·嵌入式硬件·集成电路·芯片·电源芯片
想搞嵌入式的小白2 小时前
LDO和DC-DC的异同
单片机·嵌入式硬件
EVERSPIN3 小时前
RGB显示驱动MCU单片机—CH643键盘应用方案
单片机·嵌入式硬件·mcu·计算机外设·mcu单片机
电气_空空4 小时前
基于 Labview/Matlab 的人脸识别系统设计与实现
单片机·嵌入式硬件·毕业设计·labview
学嵌入式的小杨同学5 小时前
STM32 进阶封神之路(十一):串口通信底层原理全解析 ——UART/USART 区别 + 电平标准 + 协议规范(面试重点)
stm32·单片机·嵌入式硬件·mcu·硬件架构·硬件工程·智能硬件
wuyikeer5 小时前
SocketTool、串口调试助手、MQTT中间件基础
单片机·嵌入式硬件·中间件
阿祖_in_coding5 小时前
面试之嵌入式基础知识:uart、i2c、spi
嵌入式硬件
济6175 小时前
STM32 I2C 总线通信实战|从原理到 OLED 屏数据收发(超详细)---STM32 HAL库专栏
stm32·嵌入式·stm32hal库编程
Hello_Embed6 小时前
LVGL 入门(四):大小坐标与盒子模型
前端·笔记·stm32·单片机·嵌入式