STM32F103C8T6最小系统板实现蜂鸣器报警

文章介绍了如何使用STM32进行RCC时钟配置和GPIO初始化,以控制蜂鸣器的响动。通过SWD接口连接,配置PB12引脚为推挽输出,设置高低电平来实现蜂鸣器的周期性响动。代码示例展示了在while循环中利用GPIO_SetBits和GPIO_ResetBits函数切换引脚状态,配合延时函数实现节奏控制。
摘要由CSDN通过智能技术生成


接线图

在这里插入图片描述
SWD方式下载程序,4线,VCC,GND。
SWDIO:Serial Wire Data Input Output,串行数据输入输出引脚,作为仿真信号的双向数据信号线,建议上拉。
SWCLK:Serial Wire Clock,串行线时钟引脚,作为仿真信号的时钟信号线,建议下拉;
蜂鸣器的IO口接在了最小系统板的PB12引脚上。
蜂鸣器的操作方法和LED方式一样。 注:上图蜂鸣器是低电平有效。

一、配置RCC时钟

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

STM32任何外设**第一步都是先配置时钟。**GPIO都是挂载在APB2总线上的。

二、配置GPIO

1.引入库

推挽输出,PB12引脚,引脚速度50MHZ,因为本程序用来学习,没有考虑低功耗
最后一步就是调用STM32库函数,初始化GPIOB。这5句代码非常常用。一般都是这个套路。

GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

while循环

代码如下(示例):

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);
	}

这段程序的意思是
在while循环中,清除PB12引脚的数据(设置为低电平),延时100ms, PB12引脚置为1,延时100Ms,就是响,不响,响,不响,这样一直循环。
下面是两个函数的具体含义。


```c
/**
  * @brief  Clears the selected data port bits.
  * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  * @retval None
  */
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BRR = GPIO_Pin;
}

翻译:

  • @brief清除选中的数据端口位。
    • @param GPIOx:其中x可以是(A…G),用来选择GPIO外设。
    • @param GPIO_Pin:要写的端口位。
  • *该参数可以是GPIO_Pin_x的任意组合,其中x可以是(0…15)。
    • @retval无
/**
  * @brief  Sets the selected data port bits.
  * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  * @retval None
  */
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BSRR = GPIO_Pin;
}

翻译:

* @brief设置所选数据端口位。
* * @param GPIOx:其中x可以是(A..G),用来选择GPIO外设。
* * @param GPIO_Pin:要写的端口位。
* *该参数可以是GPIO_Pin_x的任意组合,其中x可以是(0..15)* * @retval无

总结
资料来源:
1.STM32固件库
2.某站自化协教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯烟火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值