PUYA COMP — COMP_CompareGpioVs1_2VCC_Window

PUYA PY32L020_Firmware V1.2.1 COMP COMP_CompareGpioVs1_2VCC_Window 示例教学

COMP — COMP_CompareGpioVs1_2VCC_Window

固件库: PY32L020_Firmware V1.2.1
芯片: PUYA
源文件: PUYA/PY32L020_Firmware_V1.2.1/Projects/PY32L020-STK/Example/COMP/COMP_CompareGpioVs1_2VCC_Window/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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "main.h"

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

  /* Initialize LED */
  BSP_LED_Init(LED_GREEN);
  
  hcomp1.Instance = COMP1;                                               /* COMP1 */
  hcomp1.Init.InputMinus      = COMP_INPUT_MINUS_IO1;                    /* Minus PB0 */
  hcomp1.Init.InputPlus       = COMP_INPUT_PLUS_IO1;                     /* Plus None*/
  hcomp1.Init.OutputPol       = COMP_OUTPUTPOL_NONINVERTED;              /* Output None Inverted */
  hcomp1.Init.WindowMode      = COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON; /* Window mode COMP2 Plus as Common plus  */
  hcomp1.Init.VrefSrc         = COMP_VREFCMP_SOURCE_VCC;                 /* VrefCmp Select VCC */
  hcomp1.Init.VrefDiv         = COMP_VREFCMP_DIV_8_16VREFCMP;            /* Vrefbuf 8/16  */
  hcomp1.Init.DigitalFilter   = 0x0;                                     /* Filter Disable */
  hcomp1.Init.TriggerMode     = COMP_TRIGGERMODE_NONE;                   /* Trigger None */
  
  /* Initialize COMP */
  if (HAL_COMP_Init(&hcomp1) != HAL_OK)                                 
  {
    APP_ErrorHandler();
  }
  
  hcomp2.Instance = COMP2;                                               /* COMP2 */
  hcomp2.Init.InputMinus      = COMP_INPUT_MINUS_IO2;                    /* Minus PA3 */
  hcomp2.Init.InputPlus       = COMP_INPUT_PLUS_IO2;                     /* Plus VrefBuf */
  hcomp2.Init.OutputPol       = COMP_OUTPUTPOL_NONINVERTED;              /* Output None Inverted */
  hcomp2.Init.WindowMode      = COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON; /* Window mode COMP2 Plus as Common plus */
  hcomp2.Init.VrefSrc         = COMP_VREFCMP_SOURCE_VCC;                 /* VrefCmp Select VCC */
  hcomp2.Init.VrefDiv         = COMP_VREFCMP_DIV_8_16VREFCMP;            /* Vrefbuf 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(&hcomp1); 

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

代码讲解

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

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