当前位置: 首页 > news >正文

STM32开关输入控制220V灯泡亮灭源代码(附带PROTEUSd电路图)

 

//main.c文件
/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief  The application entry point.* @retval int*/int main(void)
{HAL_Init();SystemClock_Config();MX_GPIO_Init();while (1){if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==1){HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,0);while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==1);}else{HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,1);while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0);}/* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @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_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;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();}
}/* 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 */
//gpio.c文件
/* USER CODE BEGIN Header */
/********************************************************************************* @file    gpio.c* @brief   This file provides code for the configuration*          of all used GPIO pins.******************************************************************************* @attention** Copyright (c) 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "gpio.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 *//* USER CODE END 1 *//** Configure pins
*/
void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};__HAL_RCC_GPIOB_CLK_ENABLE();	//设置0号脚GPIO_InitStruct.Pin=GPIO_PIN_0;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_PULLDOWN;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);//设置一号脚GPIO_InitStruct.Pin=GPIO_PIN_1;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_PULLDOWN;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,0);}/* USER CODE BEGIN 2 *//* USER CODE END 2 */

http://www.lryc.cn/news/131220.html

相关文章:

  • Spring Boot配置文件
  • 函数(2)
  • Linux笔试题(4)
  • Selenium的使用:WEB功能测试
  • Kubernetes(K8s)从入门到精通系列之十七:minikube启动K8s dashboard
  • C++ 网络编程项目fastDFS分布式文件系统(五)--nginx+fastdfs
  • 开发者本地搭建性能监测工具(Windows)
  • 嵌入式Linux开发实操(八):UART串口开发
  • 公告:微信小程序备案期限官方要求
  • cesium中获取高度的误区
  • 基于Centos:服务器基础环境安装: JDK、Maven、Python、Go、Docker、K8s
  • Elasticsearch的数据删除策略只能触发一次
  • Open3D 最小二乘拟合空间直线(方法一)
  • 解决uniapp 二次登陆 登录页是首页时,登录页闪现问题
  • 如何快速制作一个房地产电子传单?
  • golang云原生项目之:etcd服务注册与发现
  • arm:day4
  • flutter 常见的状态管理器
  • Kotlin的Map
  • STM32 串口复习
  • VScode替换cmd powershell为git bash 终端,并设置为默认
  • 网络基础(一)桥接网络
  • C#程序变量统一管理例子 - 开源研究系列文章
  • 信息熵,信息增益,增益率的理解
  • 二级MySQL(一)——基本概念与方法
  • 13 Web全栈 pnpm
  • 回归预测 | MATLAB实现CSO-BP布谷鸟优化算法优化BP神经网络多输入单输出回归预测(多指标,多图)
  • JavaScript中的事件冒泡和事件捕获机制
  • 秋招面经——结合各方面试经验
  • Python random模块用法整理