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)
根据BitVal(Bit_SET或Bit_RESET)设置单个引脚电平。
GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
直接写入整个GPIO端口的值。
输入读取函数
GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
读取指定引脚的电平状态(返回Bit_SET或Bit_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)
启用或禁用指定引脚的中断功能。