acmp.c 5.9 KB
Newer Older
Z
zhongjiequan 已提交
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

/******************************************************************************
*
* @brief providing APIs for configuring ACMP. 
*
*******************************************************************************
*
* provide APIs for configuring ACMP
******************************************************************************/
#include "common.h"
#include "acmp.h"
/******************************************************************************
* Global variables
******************************************************************************/

/******************************************************************************
* Constants and macros
******************************************************************************/

/******************************************************************************
* Local types
******************************************************************************/

/******************************************************************************
* Local function prototypes
******************************************************************************/

/******************************************************************************
* Local variables
******************************************************************************/

ACMP_CallbackPtr ACMP_Callback[2] = {(ACMP_CallbackPtr)NULL};

/******************************************************************************
* Local functions
******************************************************************************/
/******************************************************************************
* Global functions
******************************************************************************/
void ACMP0_Isr(void);
void ACMP1_Isr(void);

/******************************************************************************
* ACMP api list.
*
*//*! @addtogroup acmp_api_list
* @{
*******************************************************************************/

/*****************************************************************************//*!
*
* @brief initialize ACMP as per control field.
*        
* @param   pACMPx         pointer to an ACMP register base.
* @param   pConfig        control parameters.  
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see   ACMP_DeInit.  
*
*****************************************************************************/
void ACMP_Init(ACMP_Type *pACMPx, ACMP_ConfigType *pConfig) 
{
    if(ACMP0 == pACMPx)
    {    
        /* enable clock to ACMP */
        SIM->SCGC |= SIM_SCGC_ACMP0_MASK;
 
        /* enable ACMP interrupt */
        if(pConfig->sCtrlStatus.bits.bIntEn)
            NVIC_EnableIRQ(ACMP0_IRQn);
    }
    else
    {
        SIM->SCGC |= SIM_SCGC_ACMP1_MASK;
        if(pConfig->sCtrlStatus.bits.bIntEn)
            NVIC_EnableIRQ(ACMP1_IRQn);            
    }
    /* neg and pos pin are not equal */
    pACMPx->C0 = pConfig->sPinSelect.byte;
    ACMP_ConfigDAC(pACMPx, &pConfig->sDacSet );
    //pACMPx->C1 = pConfig->sDacSet.byte;
    pACMPx->C2 = pConfig->sPinEnable.byte;
    pACMPx->CS = pConfig->sCtrlStatus.byte;
}


/*****************************************************************************//*!
*
* @brief write ACMP register bits.
*        
* @param   pACMPx      pointer to an ACMP register base.
* @param   pDACConfig   pointer to an ACMP DAC control structure.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ACMP_ConfigDAC(ACMP_Type *pACMPx, ACMP_DACType *pDACConfig) 
{
    pACMPx->C1 = pDACConfig->byte;  
}

/*****************************************************************************//*!
*
* @brief deinit ACMP module.
*        
* @param   pACMPx      pointer to an ACMP register base.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see   ACMP_Init.  
*
*****************************************************************************/
void ACMP_DeInit(ACMP_Type *pACMPx) 
{
    if(ACMP0 == pACMPx)
    {    
        if(pACMPx->CS & ACMP_CS_ACIE_MASK)
            NVIC_DisableIRQ(ACMP0_IRQn);
    }
    else
    {
        if(pACMPx->CS & ACMP_CS_ACIE_MASK)
            NVIC_DisableIRQ(ACMP1_IRQn);            
    }
    
    pACMPx->CS = 0;
    pACMPx->C0 = 0;
    pACMPx->C1 = 0;
    pACMPx->C2 = 0;
    
    if(ACMP0 == pACMPx)
    {    
        SIM->SCGC &= ~SIM_SCGC_ACMP0_MASK;
    }
    else
    {
        SIM->SCGC &= ~SIM_SCGC_ACMP1_MASK;           
    } 
}

/*****************************************************************************//*!
*
* @brief  set up ACMP callback routines to be called by interrupt service routine.
*        
* @param  pACMPx       pointer to an ACMP register base. 
* @param   pfnCallback  callback routine.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ACMP_SetCallback(ACMP_Type *pACMPx, ACMP_CallbackPtr pfnCallback)
{
    if(ACMP0 == pACMPx)
    {
        ACMP_Callback[0] = pfnCallback;
    }
    else
    {
        ACMP_Callback[1] = pfnCallback;
    }
}

/*! @} End of acmp_api_list                                                  */


/*****************************************************************************//*!
*
* @brief  ACMP0 interrupt service routine.
*        
* @param  none. 
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ACMP0_Isr(void)
{

    if(ACMP_Callback[0])
    {
        ACMP_Callback[0]();             /* call callback routine */
    }
}

/*****************************************************************************//*!
*
* @brief  ACMP1 interrupt service routine.
*        
* @param  none. 
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ACMP1_Isr(void)
{

    if(ACMP_Callback[1])
    {
        ACMP_Callback[1]();             /* call callback routine */
    }
}