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

文章目录

  • 接线图
  • 一、配置RCC时钟
  • 二、配置GPIO
  • 1.引入库
  • while循环

  • 接线图


    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.某站自化协教程。

    物联沃分享整理
    物联沃-IOTWORD物联网 » STM32F103C8T6最小系统板实现蜂鸣器报警

    发表评论