4.LED和蜂鸣器的示例程序

_

LED闪烁

面包板接线图如下。

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	//开启GPIOA外设时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//配置使能GPIOA RCC时钟
	GPIO_InitTypeDef GPIOInitStructure;//配置GPIO结构体
	GPIOInitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//通用推挽输出
	GPIOInitStructure.GPIO_Pin=GPIO_Pin_0;//配置PA0
	GPIOInitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置速度为50Mhz
	GPIO_Init(GPIOA,&GPIOInitStructure);//初始化GPIO配置
	//GPIO_SetBits(GPIOA,GPIO_Pin_0);//设置PA0为高电平
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);//设置PA0为低电平
	//GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);//设置PA0的电平状态
	while(1)
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_0);//设置PA0为低电平
		Delay_ms(1000);
		GPIO_SetBits(GPIOA,GPIO_Pin_0);//设置PA0为高电平
		Delay_ms(1000);
	}
}

LED流水灯

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启时钟
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	// |或运算  按键右转  前三个引脚或后为0x0111  ...
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	while(1)
	{
		GPIO_Write(GPIOA,~0x0001);// 0000 0000 0000 0001   取反前~
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0002);// 0000 0000 0000 0010
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0004);// 0000 0000 0000 0100
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0008);// 0000 0000 0000 1000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0010);// 0000 0000 0001 0000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0020);// 0000 0000 0010 0000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0040);// 0000 0000 0100 0000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0080);// 0000 0000 1000 0000
		Delay_ms(500);
	}
 
}
 
 

蜂鸣器

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	//开启GPIOB外设时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//配置使能GPIOA RCC时钟
	GPIO_InitTypeDef GPIOInitStructure;//配置GPIO结构体
	GPIOInitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//通用推挽输出
	GPIOInitStructure.GPIO_Pin=GPIO_Pin_12;//配置PB12
	GPIOInitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置速度为50Mhz
	GPIO_Init(GPIOB,&GPIOInitStructure);//初始化GPIO配置
	while(1)
	{
		//低电平 蜂鸣器响
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(500);
		//高电平 蜂鸣器关闭
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(500);
	}
}

3.GPIO输入 2026-08-09
5.EXTI外部中断 2026-08-15

评论区

© 2026 ZETAI