尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
2.1GPIO输入
按键控制LED灯亮灭我这里直接用A0,A1,A4,A6口了省得麻烦。主函数#include stm32f10x.h // Device header void led_initial(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_ModeGPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_PinGPIO_Pin_0|GPIO_Pin_1; GPIO_InitStruct.GPIO_SpeedGPIO_Speed_50MHz; GPIO_Init(GPIOA,GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1); } void control_led(uint16_t GPIO_Pin_x,uint8_t key) { if(key0) { GPIO_SetBits(GPIOA,GPIO_Pin_x); } else { GPIO_ResetBits(GPIOA,GPIO_Pin_x); } }key.c文件#include stm32f10x.h // Device header #include Delay.h void key_initial(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_ModeGPIO_Mode_IPU; GPIO_InitStruct.GPIO_PinGPIO_Pin_4|GPIO_Pin_6; GPIO_InitStruct.GPIO_SpeedGPIO_Speed_50MHz; GPIO_Init(GPIOA,GPIO_InitStruct); } uint8_t presskey(GPIO_TypeDef*GPIOx,uint16_t GPIO_Pin) { uint8_t num0; if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)0) { Delay_ms(20); while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)0); Delay_ms(20); num1; } return num; }led.c文件#include stm32f10x.h // Device header void led_initial(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_ModeGPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_PinGPIO_Pin_0|GPIO_Pin_1; GPIO_InitStruct.GPIO_SpeedGPIO_Speed_50MHz; GPIO_Init(GPIOA,GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1); } void control_led(uint16_t GPIO_Pin_x,uint8_t key) { if(key0) { GPIO_SetBits(GPIOA,GPIO_Pin_x); } else { GPIO_ResetBits(GPIOA,GPIO_Pin_x); } }光敏传感器控制控制有源蜂鸣器有光响无光不响主函数#include stm32f10x.h // Device header #include Delay.h #include LightSensor.h #include Buzzer.h int main(void) { LightSensorInit(); BuzzerInit(); while(1) { BuzzerControl(GetLight()); } }Buzzer驱动函数#include stm32f10x.h // Device header void BuzzerInit(void)//低电平触发 { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_ModeGPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_PinGPIO_Pin_0; GPIO_InitStruct.GPIO_SpeedGPIO_Speed_50MHz; GPIO_Init(GPIOA,GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_0); } void BuzzerControl(uint8_t num) { if(num1) { GPIO_ResetBits(GPIOA,GPIO_Pin_0); } else { GPIO_SetBits(GPIOA,GPIO_Pin_0); } }光敏传感器驱动函数#include stm32f10x.h // Device header void LightSensorInit(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_ModeGPIO_Mode_IPD; GPIO_InitStruct.GPIO_PinGPIO_Pin_2; GPIO_InitStruct.GPIO_SpeedGPIO_Speed_50MHz; GPIO_Init(GPIOA,GPIO_InitStruct); } uint8_t GetLight(void) { if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)0)//有光光敏传感器输出0 return 1; else return 0; }HAL库按键控制板载LED亮灭while (1) { /* USER CODE END WHILE */ if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)1) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET); else HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET); /* USER CODE BEGIN 3 */ }
RELATED

相关推荐

Locus协作页深度解析:为Unity项目打造的图形化Git工作区

Locus协作页深度解析:为Unity项目打造的图形化Git工作区

【免费下载链接】Locus The open source Unity Dev Agent 项目地址: https://gitcode.com/gh_mirrors/locus5/Locus 点击查看 免费下载 Locus 是一款开源的 Unity Dev Agent(AI 开发助手),而它的「协作」页面就是内置的图形化 Gi…

📅 2026/10/30 6:30:01
从安装到插件开发:Dalamud 三大场景带你走通 FFXIV 插件框架

从安装到插件开发:Dalamud 三大场景带你走通 FFXIV 插件框架

从安装到插件开发:Dalamud 三大场景带你走通 FFXIV 插件框架 【免费下载链接】Dalamud FFXIV plugin framework and API 项目地址: https://gitcode.com/GitHub_Trending/da/Dalamud 开荒团本里,团长要在角色面板、团队列表、职业资源之间来回切&…

📅 2026/10/30 6:30:01
Pipecat 实时语音助手快速入门:3 条命令搭出会说话的 AI 机器人

Pipecat 实时语音助手快速入门:3 条命令搭出会说话的 AI 机器人

Pipecat 实时语音助手快速入门:3 条命令搭出会说话的 AI 机器人 【免费下载链接】pipecat Open Source framework for voice agents, multimodal apps, and realtime AI. Maintained by Daily and the community. 项目地址: https://gitcode.com/GitHub_Trending/…

📅 2026/10/30 6:30:01
MORE NEWS

更多资讯

📰

Nginx前后端服务配置实战与优化指南

1. Nginx 配置前端后端服务实战指南作为现代Web应用的标准部署方案,Nginx因其高性能和灵活的配置能力成为前后端分离架构的核心组件。我在实际项目中经历过数十次Nginx配置迭代,发现90%的部署问题都源于对配置原理理解不透彻。本文将带你深入Nginx配置的…

📰

多体系统故障诊断技术与工程实践

1. 多体系统故障诊断概述多体系统故障诊断是工程领域的重要研究方向,主要针对由多个相互作用的子系统组成的复杂系统进行状态监测和异常识别。这类系统在航空航天、电力网络、智能制造等领域广泛应用,其可靠性直接影响整个系统的运行安全。典型的工业多体…

📰

HTML核心概念与语义化开发实践指南

1. HTML基础概念与核心价值HTML(HyperText Markup Language)作为构建互联网的基石语言,其核心价值在于定义网页内容的结构与语义。不同于CSS负责表现、JavaScript处理行为,HTML专注于内容的组织与关联。这种分工明确的体系使得Web…

📰

商汤LightX2V:实时视频生成框架的技术解析与应用

1. LightX2V:商汤开源的实时视频生成推理框架解析当我在GitHub上第一次看到LightX2V这个项目时,就被它惊人的性能数据吸引了——在H100显卡上实现1.9倍于主流框架的推理速度,这完全颠覆了我对视频生成效率的认知。作为一个长期关注生成式AI的…

📰

AI自动调试技术:原理、实现与工程实践

1. AI自动调试技术概述在软件开发领域,调试(Debugging)一直是耗时且需要丰富经验的工作环节。传统调试流程通常包括:复现问题→定位错误→分析原因→修复验证,这个过程往往占据开发周期的30%-50%时间。AI自动调试技术的…

📰

OneTrans-V2:统一召回与排序的生成式检索架构落地实践

1. 从两个模块到一条链路:OneTrans-V2 到底想解决什么问题推荐系统这个行当,干了几年之后你会发现一个特别拧巴的现实:召回和排序明明是同一个目标的两段路,却硬生生被拆成了两套模型、两套特征、两套工程链路。召回阶段用双塔&am…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬