找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1860|回复: 1
收起左侧

蜂鸣器通过ic触摸板改变声音 stm32源码

[复制链接]
ID:331753 发表于 2018-5-16 23:33 | 显示全部楼层 |阅读模式
单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"

  41. /* USER CODE BEGIN Includes */

  42. /* USER CODE END Includes */

  43. /* Private variables ---------------------------------------------------------*/

  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/
  46.         uint16_t myDelay;

  47. /* USER CODE END PV */

  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static void MX_GPIO_Init(void);

  51. /* USER CODE BEGIN PFP */
  52. /* Private function prototypes -----------------------------------------------*/

  53. /* USER CODE END PFP */

  54. /* USER CODE BEGIN 0 */

  55. /* USER CODE END 0 */

  56. int main(void)
  57. {

  58.   /* USER CODE BEGIN 1 */

  59.   /* USER CODE END 1 */

  60.   /* MCU Configuration----------------------------------------------------------*/

  61.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  62.   HAL_Init();

  63.   /* USER CODE BEGIN Init */

  64.   /* USER CODE END Init */

  65.   /* Configure the system clock */
  66.   SystemClock_Config();

  67.   /* USER CODE BEGIN SysInit */
  68.         HAL_SYSTICK_Config(SystemCoreClock/1000000U);
  69.   /* USER CODE END SysInit */

  70.   /* Initialize all configured peripherals */
  71.   MX_GPIO_Init();

  72.   /* USER CODE BEGIN 2 */
  73.         myDelay = 310;
  74.   /* USER CODE END 2 */

  75.   /* Infinite loop */
  76.   /* USER CODE BEGIN WHILE */
  77.   while (1)
  78.   {
  79.   /* USER CODE END WHILE */

  80.   /* USER CODE BEGIN 3 */

  81.                 while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
  82.                 {
  83.           HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_2);
  84.                 HAL_Delay(myDelay);
  85.                 }
  86.           HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
  87.                 HAL_Delay(20000);
  88.   }
  89.   /* USER CODE END 3 */

  90. }

  91. /** System Clock Configuration
  92. */
  93. void SystemClock_Config(void)
  94. {

  95.   RCC_OscInitTypeDef RCC_OscInitStruct;
  96.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  97.     /**Initializes the CPU, AHB and APB busses clocks
  98.     */
  99.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  100.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  101.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  102.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  103.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  104.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  105.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  106.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  107.   {
  108.     _Error_Handler(__FILE__, __LINE__);
  109.   }

  110.     /**Initializes the CPU, AHB and APB busses clocks
  111.     */
  112.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  113.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  114.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  116.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  117.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  118.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  119.   {
  120.     _Error_Handler(__FILE__, __LINE__);
  121.   }

  122.     /**Configure the Systick interrupt time
  123.     */
  124.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  125.     /**Configure the Systick
  126.     */
  127.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  128.   /* SysTick_IRQn interrupt configuration */
  129.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  130. }

  131. /** Configure pins as
  132.         * Analog
  133.         * Input
  134.         * Output
  135.         * EVENT_OUT
  136.         * EXTI
  137. */
  138. static void MX_GPIO_Init(void)
  139. {

  140.   GPIO_InitTypeDef GPIO_InitStruct;

  141.   /* GPIO Ports Clock Enable */
  142.   __HAL_RCC_GPIOC_CLK_ENABLE();
  143.   __HAL_RCC_GPIOD_CLK_ENABLE();
  144.   __HAL_RCC_GPIOA_CLK_ENABLE();
  145.   __HAL_RCC_GPIOB_CLK_ENABLE();

  146.   /*Configure GPIO pin Output Level */
  147.   HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);

  148.   /*Configure GPIO pin Output Level */
  149.   HAL_GPIO_WritePin(buzzer_GPIO_Port, buzzer_Pin, GPIO_PIN_RESET);

  150.   /*Configure GPIO pin : led_Pin */
  151.   GPIO_InitStruct.Pin = led_Pin;
  152.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  153.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  154.   HAL_GPIO_Init(led_GPIO_Port, &GPIO_InitStruct);

  155.   /*Configure GPIO pin : buzzer_Pin */
  156.   GPIO_InitStruct.Pin = buzzer_Pin;
  157.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  158.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  159.   HAL_GPIO_Init(buzzer_GPIO_Port, &GPIO_InitStruct);

  160.   /*Configure GPIO pin : PB12 */
  161.   GPIO_InitStruct.Pin = GPIO_PIN_12;
  162.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  163.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  164.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  165.   /*Configure GPIO pin : PB13 */
  166.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  167.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  168.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  169.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  170.   /* EXTI interrupt init*/
  171.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  172.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

  173. }

  174. /* USER CODE BEGIN 4 */

  175. /* USER CODE END 4 */

  176. /**
  177.   * @brief  This function is executed in case of error occurrence.
  178.   * @param  None
  179.   * @retval None
  180.   */
  181. void _Error_Handler(char * file, int line)
  182. {
  183.   /* USER CODE BEGIN Error_Handler_Debug */
  184.   /* User can add his own implementation to report the HAL error return state */
  185.   while(1)
  186.   {
  187.   }
  188.   /* USER CODE END Error_Handler_Debug */
  189. }

  190. ……………………

  191. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
step2_buzzer.rar (5.15 MB, 下载次数: 5)
回复

使用道具 举报

ID:1 发表于 2018-5-17 01:47 | 显示全部楼层
分享原理图后可获得100积分
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表