PUYA COMP — COMP_CompareGpioVs1_2VCC_Polling

PUYA PY32L020_Firmware V1.2.1 COMP COMP_CompareGpioVs1_2VCC_Polling 示例教学

COMP — COMP_CompareGpioVs1_2VCC_Polling

固件库: PY32L020_Firmware V1.2.1
芯片: PUYA
源文件: PUYA/PY32L020_Firmware_V1.2.1/Projects/PY32L020-STK/Example/COMP/COMP_CompareGpioVs1_2VCC_Polling/Src/main.c


功能简介

本示例演示 COMP 的基本用法。

完整代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "main.h"

int main(void)
{
  /* Reset of all peripherals, Initializes the Systick. */ 
  HAL_Init();

  /* Initialize LED */
  BSP_LED_Init(LED_GREEN);
  
  hcomp2.Instance = COMP2;                                            /* COMP2 */
  hcomp2.Init.InputMinus      = COMP_INPUT_MINUS_IO1;                 /* Minus Input PA4 */
  hcomp2.Init.InputPlus       = COMP_INPUT_PLUS_IO2;                  /* Plus Input VrefCmp */
  hcomp2.Init.OutputPol       = COMP_OUTPUTPOL_NONINVERTED;           /* Output None Inverted */
  hcomp2.Init.WindowMode      = COMP_WINDOWMODE_DISABLE;              /* Window mode Disable */
  hcomp2.Init.VrefSrc         = COMP_VREFCMP_SOURCE_VCC;              /* VrefCmp Select VCC */
  hcomp2.Init.VrefDiv         = COMP_VREFCMP_DIV_8_16VREFCMP;         /* Vrefcmp 8/16  */
  hcomp2.Init.DigitalFilter   = 0x0;                                  /* Filter Disable */
  hcomp2.Init.TriggerMode     = COMP_TRIGGERMODE_NONE;                /* Trigger None */
  
  /* Initialize COMP */
  if (HAL_COMP_Init(&hcomp2) != HAL_OK)                                 
  {
    APP_ErrorHandler();
  }
  /* COMP Start */
  HAL_COMP_Start(&hcomp2); 

  while (1)
  {
    if(HAL_COMP_GetOutputLevel(&hcomp2) == 1)
    {
      BSP_LED_On(LED_GREEN);
    }
    else
    {
      BSP_LED_Off(LED_GREEN);
    }      
  }
}

代码讲解

板级初始化:初始化开发板上的 LED、按键等基础外设。

世界是你们
使用 Hugo 构建
主题 StackJimmy 设计