[stm32-1]LED闪烁LED流水灯蜂鸣器

news/2024/10/21 9:35:38/

1、LED闪烁&LED流水灯&蜂鸣器

1.使用RCC开启GPIO时钟

2.使用GPIO_Init函数初始化GPIO

3.使用输入或输出函数控制GPIO口

RCC常用的3个库函数:

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);

void GPIO_DeInit(GPIO_TypeDef* GPIOx);        //所指定的GPIO外设会被复位 

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);        //用结构体的参数来初始化GPIO口

void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);        //结构体变量赋一个默认值

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);              //GPIO读取函数
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);        //GPIO写入函数

GPIO_Mode_AIN         //模拟输入
  GPIO_Mode_IN_FLOATING = 0x04,        //浮空输入
  GPIO_Mode_IPD = 0x28,        //下拉输入
  GPIO_Mode_IPU = 0x48,        //上拉输入
  GPIO_Mode_Out_OD = 0x14,        //开漏输出
  GPIO_Mode_Out_PP = 0x10,        //推挽输出
  GPIO_Mode_AF_OD = 0x1C,        //复用开漏
  GPIO_Mode_AF_PP = 0x18         //复用推挽

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);        //指定端口设置为高电平
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);        //低电平
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); 

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructer;GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructer.GPIO_Pin=GPIO_Pin_0;GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructer);//GPIO_ResetBits(GPIOA,GPIO_Pin_0);//GPIO_SetBits(GPIOA,GPIO_Pin_0);GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);while(1){GPIO_ResetBits(GPIOA,GPIO_Pin_0);Delay_ms(500);GPIO_SetBits(GPIOA,GPIO_Pin_0);Delay_ms(500);
//		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
//		Delay_ms(500);
//		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
//		Delay_ms(500);}
}

 把LED长脚接A0,短脚接-,LED闪烁:推挽输出,高低电平均可驱动。

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructer;GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructer.GPIO_Pin=GPIO_Pin_All;//GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructer);
//	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//	//GPIO_SetBits(GPIOA,GPIO_Pin_0);
//	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);while(1){GPIO_Write(GPIOA,~0x0001);//0000000000000001Delay_ms(500);GPIO_Write(GPIOA,~0x0002);//0000000000000010Delay_ms(500);GPIO_Write(GPIOA,~0x0004);//0000000000000100Delay_ms(500);GPIO_Write(GPIOA,~0x0008);//0000000000001000Delay_ms(500);GPIO_Write(GPIOA,~0x0010);//0000000000010000Delay_ms(500);GPIO_Write(GPIOA,~0x0020);//0000000000100000Delay_ms(500);GPIO_Write(GPIOA,~0x0040);//0000000001000000Delay_ms(500);GPIO_Write(GPIOA,~0x0080);//0000000010000000Delay_ms(500);}
}

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);GPIO_InitTypeDef GPIO_InitStructer;GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructer.GPIO_Pin=GPIO_Pin_12;GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructer);while(1){GPIO_ResetBits(GPIOB,GPIO_Pin_12);Delay_ms(100);GPIO_SetBits(GPIOB,GPIO_Pin_12);Delay_ms(100);GPIO_ResetBits(GPIOB,GPIO_Pin_12);Delay_ms(100);GPIO_SetBits(GPIOB,GPIO_Pin_12);Delay_ms(700);}
}

 


http://www.ppmy.cn/news/1457846.html

相关文章

Ubuntu18.04 安装 anconda

anaconda官网 bash Anaconda3-2021.11-Linux-x86_64.sh一直回车,输入yes 选择安装目录 是否希望更新shell配置文件以自动初始化conda

python 企业微信向员工付款 paywwsptrans2pocket

前提声明 该功能已经不可开通&#xff0c;以往已开通的商户不受影响 否则报错如下 <err_code_des><![CDATA[你的商户号未开通该产品权限&#xff0c;请联系管理员到产品中心开通。开通路径&#xff1a;产品中心-产品大全-企业微信-向员工付款-申请开通]]></err…

Rust:用 Warp 库实现 Restful API 的简单示例

直接上代码&#xff1a; 1、源文件 Cargo.toml [package] name "xcalc" version "0.1.0" edition "2021"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies] warp "…

692. 前K个高频单词 、KY264 单词识别

692. 前K个高频单词 - 力扣&#xff08;LeetCode&#xff09; //#include<set> class Solution { public://仿函数struct compare{bool operator()(const pair<string,int>& kv1,const pair<string,int>& kv2){return kv1.second > kv2.second;}…

关于YOLO8学习(五)安卓部署ncnn模型--视频检测

前文 关于YOLO8学习(一)环境搭建,官方检测模型部署到手机 关于YOLO8学习(二)数据集收集,处理 关于YOLO8学习(三)训练自定义的数据集 关于YOLO8学习(四)模型转换为ncnn 简介 本文将会讲解: (1)使用前文生成的ncnn模型,部署到安卓端,并且实现视频中,人脸的检测…

分享 Kamailio 5.7.x 预处理一例

来自工单&#xff0c;很不错 不翻译了&#xff0c;认真看的话都能看懂 #!define IPADDR 127.0.0.1 #!defexp SIPURI "sip:" IPADDR ":5060" #!defexp QSIPURI "sip: IPADDR :5060" #!defexp V16 1<<4 Another possibility is using…

【unity】(2)GameObject

GameObject类是基本的游戏对象类型&#xff0c;它可以用来代表场景中的任何实体。 基本属性 name 类型&#xff1a;string说明&#xff1a;GameObject的名称。用法&#xff1a; GameObject go new GameObject(); go.name "My GameObject";activeSelf 类型&#xf…

PostgreSQL自带的命令行工具01- pg_archivecleanup

PostgreSQL自带的命令行工具01- pg_archivecleanup 基础信息 OS版本&#xff1a;Red Hat Enterprise Linux Server release 7.9 (Maipo) DB版本&#xff1a;16.2 pg软件目录&#xff1a;/home/pg16/soft pg数据目录&#xff1a;/home/pg16/data 端口&#xff1a;5777pg_archiv…