From 83cf83d37790889e5bb3ebb365e8e536814a6256 Mon Sep 17 00:00:00 2001
From: Wenxue <wenxue.guo@avnet.com>
Date: Tue, 02 Sep 2025 09:42:34 +0800
Subject: [PATCH] 定时器和蜂鸣器程序

---
 Core/Src/board/miscdev.c |  134 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+), 0 deletions(-)

diff --git a/Core/Src/board/miscdev.c b/Core/Src/board/miscdev.c
index 45a6d8d..f5f96c5 100644
--- a/Core/Src/board/miscdev.c
+++ b/Core/Src/board/miscdev.c
@@ -130,3 +130,137 @@
 
     return leds[which].status;
 }
+
+/*
+ *+----------------------+
+ *|     GPIO Key API     |
+ *+----------------------+
+ */
+#include "main.h" /* Key1_pin, Key2_Pin, Key3_Pin 定义在该头文件下 */
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
+{
+    if( Key1_Pin == GPIO_Pin )
+    {
+        blink_led( Led_R, 500 );
+    }
+    else if( Key2_Pin == GPIO_Pin )
+    {
+        blink_led( Led_G, 500 );
+    }
+    else if( Key3_Pin == GPIO_Pin )
+    {
+        blink_led( Led_B, 500 );
+    }
+}
+
+/*
+ *+----------------------+
+ *|      printf API      |
+ *+----------------------+
+ */
+#include "usart.h"  /* 全局变量huart1定义在 usart.c中,并在sart.h头文件中声明 */
+
+/* gcc 编译器中的 printf 函数将会调用 __io_putchar() 函数,实现最终的字符打印
+ * keil编译器中的 printf 函数将会调用 fputc() 函数,实现最终的字符打印
+ * 这里我们定义一个宏 PUTCHAR_PROTOTYPE 来兼容这两个编译器所需要的函数原型;
+ */
+#ifdef __GNUC__
+#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
+#else
+#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
+#endif
+
+/*
+ * STM32CubeIDE使用的是 gcc 编译器,它会内建 __GNUC__ 宏定义,所以下面PUTCHAR_PROTOTYPE将会扩展为:
+ *  int __io_putchar(int ch)
+ */
+PUTCHAR_PROTOTYPE
+{
+    /* 调用STM32 HAL库的串口发送函数,将printf要打印的这个字符通过串口发送出去 */
+    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
+    return ch;
+}
+
+/*
+ *+----------------------------+
+ *| ADC noisy & lux sensor API |
+ *+----------------------------+
+ */
+
+#include "adc.h"
+int adc_sample_lux_noisy(uint32_t *lux, uint32_t *noisy)
+{
+    uint8_t            i;
+    uint32_t           timeout = 0xffffff;
+
+    for(i=0; i<ADCCHN_MAX; i++)
+    {
+            HAL_ADC_Start(&hadc1);
+
+            HAL_ADC_PollForConversion(&hadc1, timeout);
+
+            if( ADCCHN_NOISY == i )
+            {
+                *noisy = HAL_ADC_GetValue(&hadc1);
+            }
+            else if( ADCCHN_LUX == i )
+            {
+                *lux = HAL_ADC_GetValue(&hadc1);
+            }
+
+            HAL_Delay(10);
+    }
+
+    HAL_ADC_Stop(&hadc1);
+
+    return 0;
+}
+
+/*
+ *+----------------------------+
+ *|  Timer Buzzer/delay API    |
+ *+----------------------------+
+ */
+#include "tim.h"
+
+/* Max to 60000 us */
+void udelay(uint16_t us)
+{
+    uint16_t differ = 60000-us;
+
+    HAL_TIM_Base_Start(&htim6);
+
+    __HAL_TIM_SET_COUNTER(&htim6, differ);
+
+    while( differ < 60000 )
+    {
+        differ=__HAL_TIM_GET_COUNTER(&htim6);
+    }
+
+    HAL_TIM_Base_Stop(&htim6);
+}
+
+
+void beep_start(uint16_t times, uint16_t interval)
+{
+    while( times-- )
+    {
+        /* Start buzzer */
+        if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4) != HAL_OK)
+        {
+            /* Starting Error */
+            Error_Handler();
+        }
+
+        HAL_Delay(interval);
+
+        /* Stop buzzer */
+        if (HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4) != HAL_OK)
+        {
+            /* Starting Error */
+            Error_Handler();
+        }
+
+        HAL_Delay(interval);
+    }
+}

--
Gitblit v1.9.1