STM32单片机学习篇3

1.LED灯闪烁

复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"						//使用延时函数

int main(void)
{
	
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	//定义变量
	GPIO_InitTypeDef GPIO_InitStructure;
	//推挽输出
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

	//设置流水灯的引脚,只需用按位或
	//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 ;
	//选中所有引脚
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	//输出速率
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
    //初始化指定的GPIO端口的引脚配置,参数&GPIO_InitStructure指向一个包含初始化配置的结构体
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	

	while(1)
	{	
		//C语言不支持直接写二进制,支持十六进制
		GPIO_Write(GPIOA,0x0001);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0002);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0004);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0008);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0010);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0020);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0040);
		Delay_ms(500);
		GPIO_Write(GPIOA,0x0080);
		Delay_ms(500);
		
	}
}

2.蜂鸣器程序

PA15,PB3,PB4连线时不要选,默认是JTAG的调试端口,用作普通端口,需要重新配置

使用的三根公对母的杜邦线,蜂鸣器,S-Tlink驱动

复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"						//使用延时函数

int main(void)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	//定义变量
	GPIO_InitTypeDef GPIO_InitStructure;
	//推挽输出
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	//选择引脚/12
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	
	//输出速率
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//把指定端口设置为高电平
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
	//把指定端口设置为低电平
	//GPIO_SetBits(GPIOA,GPIO_Pin_0);
	//
	//GPIO_Write(GPIOA,GPIO_Pin_0,Bit_RESET);
	while(1)
	{
		//第一种方法,从上至下依次执行
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(200);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(200);
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		
		
	
	}
}

3.补充:库函数的使用方法

打开.h文件,看一下有哪些库函数->右键跳转定义,查看函数和参数的使用方法->英文可以借助软件来进行翻译

Keil GPIO标准库函数及简介

Keil MDK通常与STM32标准外设库或HAL库配合使用。以下是常用的GPIO相关函数及其功能简介:

初始化与配置函数

GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

用于初始化GPIO引脚,配置引脚模式(输入/输出/复用功能)、输出类型(推挽/开漏)、速度及上拉/下拉电阻。

参数GPIO_InitStruct包含具体配置信息。

GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)

将GPIO初始化结构体的成员设置为默认值(通常为输入浮空模式)。

输出控制函数

GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

将指定引脚设置为高电平(置1)。

GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

将指定引脚设置为低电平(置0)。

GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)

根据BitValBit_SETBit_RESET)设置单个引脚电平。

GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)

直接写入整个GPIO端口的值。

输入读取函数

GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

读取指定引脚的电平状态(返回Bit_SETBit_RESET)。

GPIO_ReadInputData(GPIO_TypeDef* GPIOx)

读取整个GPIO端口的输入值(返回16位数据)。

GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

读取输出寄存器的引脚状态(非实际引脚电平)。

复用功能与锁定

GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)

配置引脚的复用功能(如USART、SPI等),需结合具体外设使用。

GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

锁定引脚配置,防止意外修改。

中断相关函数

GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)

配置外部中断线对应的GPIO引脚(需配合EXTI库使用)。

GPIO_ITConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, FunctionalState NewState)

启用或禁用指定引脚的中断功能。

相关推荐
Slow菜鸟2 小时前
AI学习篇(五) | awesome-design-md 使用说明
人工智能·学习
狐狐生风3 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
狐狐生风3 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
LCG元5 小时前
STM32项目实战:基于STM32F103的智能农业监控系统
stm32·单片机·嵌入式硬件
努力努力再努力FFF6 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
sakiko_7 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Truffle7电子7 小时前
STM32CubeIDE/Programmer/Touch GFX 应用
stm32·单片机·嵌入式硬件
Alice-YUE8 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
constant_LDX8 小时前
步进电机开发(一、硬件设计)
单片机·嵌入式硬件
北山有鸟9 小时前
修改源码法和插件法
嵌入式硬件·学习