lib_adc_tiny.c 4.4 KB
Newer Older
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/**
  ******************************************************************************
  * @file    lib_adc_tiny.c 
  * @author  Application Team
  * @version V1.1.0
  * @date    2019-10-28
  * @brief   ADC_TINY library.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
#include "lib_adc_tiny.h"

#define ANA_REGF_RSTValue (0U)

/**
  * @brief  Initializes the Tiny ADC peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void TADC_DeInit(void)
{
  ANA->REGF = ANA_REGF_RSTValue;
  ANA->INTSTS = ANA_INTSTS_INTSTS13;
  ANA->INTEN &= ~ANA_INTEN_INTEN13;
  ANA->MISC &= ~ANA_MISC_TADCTH;
}

/**
  * @brief  Fills each TADC_InitStruct member with its default value.
  * @param  TADC_InitStruct: pointer to an TADCInitType structure which will be initialized.
  * @retval None
  */
void TADC_StructInit(TADCInitType* TADC_InitStruct)
{
  /*--------------- Reset TADC init structure parameters values ---------------*/
  /* Initialize the SignalSel member */
  TADC_InitStruct->SignalSel = ADCTINY_SIGNALSEL_IOE6;
  /* Initialize the ADTREF1 member */
  TADC_InitStruct->ADTREF1 = ADCTINY_REF1_0_9;  
  /* Initialize the ADTREF2 member */
  TADC_InitStruct->ADTREF2 = ADCTINY_REF2_1_8;  
  /* Initialize the ADTREF3 member */
  TADC_InitStruct->ADTREF3 = ADCTINY_REF3_2_7;  
}

/**
  * @brief  Initializes Tiny ADC.
  * @param  TADC_InitStruct
                SelADT:
                    ADCTINY_SIGNALSEL_IOE6
                    ADCTINY_SIGNALSEL_IOE7
                ADTREF1:
                    ADCTINY_REF1_0_9
                    ADCTINY_REF1_0_7
                ADTREF2:
                    ADCTINY_REF2_1_8
                    ADCTINY_REF2_1_6                
                ADTREF3:
                    ADCTINY_REF3_2_7
                    ADCTINY_REF3_2_5                  
  * @retval None
  */
void TADC_Init(TADCInitType* TADC_InitStruct)
{
  uint32_t tmp;
  
  /* Check parameters */
  assert_parameters(IS_ADCTINY_SELADT(TADC_InitStruct->SignalSel));
  assert_parameters(IS_ADCTINY_ADTREF1(TADC_InitStruct->ADTREF1));
  assert_parameters(IS_ADCTINY_ADTREF2(TADC_InitStruct->ADTREF2));
  assert_parameters(IS_ADCTINY_ADTREF3(TADC_InitStruct->ADTREF3));
  
  tmp = ANA->REGF;
  tmp &= ~(ANA_REGF_ADTSEL     \
          |ANA_REGF_ADTREF1SEL\
          |ANA_REGF_ADTREF2SEL\
          |ANA_REGF_ADTREF3SEL);
  tmp |= (TADC_InitStruct->SignalSel \
          |TADC_InitStruct->ADTREF1\
          |TADC_InitStruct->ADTREF2\
          |TADC_InitStruct->ADTREF3);
  ANA->REGF = tmp;
}

/**
  * @brief  Enables or disables Tiny ADC .
  * @param  NewState
                ENABLE
                DISABLE
  * @retval None
  */
void TADC_Cmd(uint32_t NewState)
{
  /* Check parameters */
  assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  if (NewState == ENABLE)
    ANA->REGF |= ANA_REGF_ADTPDN;
  else
    ANA->REGF &= ~ANA_REGF_ADTPDN;
}

/**
  * @brief  Gets Tiny ADC output value.
  * @param  None
  * @retval Output of Tiny ADC(0 ~ 3).
  */
uint8_t TADC_GetOutput(void)
{
  return ((ANA->CMPOUT & ANA_CMPOUT_TADCO) >> ANA_CMPOUT_TADCO_Pos);
}

/**
  * @brief  Configures Tiny ADC interrupt threshold.
  * @param  THSel:
                ADCTINY_THSEL_0
                ADCTINY_THSEL_1
                ADCTINY_THSEL_2
                ADCTINY_THSEL_3
  * @retval None.
  */
void TADC_IntTHConfig(uint32_t THSel)
{
  uint32_t tmp;
  
  /* Check parameters */
  assert_parameters(IS_ADCTINY_THSEL(THSel)); 
  
  tmp = ANA->MISC;
  tmp &= ~ANA_MISC_TADCTH;
  tmp |= THSel;
  ANA->MISC = tmp;
}

/**
  * @brief  Enables or disables Tiny ADC interrupt.
  * @param  NewState
                ENABLE
                DISABLE
  * @retval None
  */
void TADC_INTConfig(uint32_t NewState)
{
  /* Check parameters */
  assert_parameters(IS_FUNCTIONAL_STATE(NewState));  
  if (NewState == ENABLE)
    ANA->INTEN |= ANA_INTEN_INTEN13;
  else
    ANA->INTEN &= ~ANA_INTEN_INTEN13;
}

/**
  * @brief  Gets Tiny ADC interrupt status.
  * @param  None
  * @retval Interrupt status.
  */
uint8_t TADC_GetINTStatus(void)
{
  if (ANA->INTSTS & ANA_INTSTS_INTSTS13)
    return 1;
  else
    return 0;
}
          
/**
  * @brief  Clears Tiny ADC interrupt status.
  * @param  None
  * @retval None
  */
void TADC_ClearINTStatus(void)
{
  ANA->INTSTS = ANA_INTSTS_INTSTS13;
}

/*********************************** END OF FILE ******************************/