acmp.h 18.2 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/******************************************************************************
* @brief header file for ACMP utilities. 
*
*******************************************************************************
*
* provide APIs for accessing ACMP
******************************************************************************/
#ifndef _MY_ACMP_H_
#define _MY_ACMP_H_
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************
* Includes
******************************************************************************/

/******************************************************************************
* Constants
******************************************************************************/

/* DAC reference select */
enum
{
    DAC_REF_BANDGAP = 0,
    DAC_REF_VDDA
};

/******************************************************************************
* Macros
******************************************************************************/

/******************************************************************************
 * ACMP module number definition                                               *
 ******************************************************************************/
#define     MAX_ACMP_NO             2

/******************************************************************************
* ACMP positive and negative pin select definition
*
*//*! @addtogroup acmp_pinsel_list
* @{
*******************************************************************************/
#define     ACMP_INPUT_P_EXT0       (0<<4)  /*!< positive pin select external pin 0 */
#define     ACMP_INPUT_P_EXT1       (1<<4)  /*!< positive pin select external pin 1 */
#define     ACMP_INPUT_P_EXT2       (2<<4)  /*!< positive pin select external pin 2 */
#define     ACMP_INPUT_P_DAC        (3<<4)  /*!< positive pin select internal DAC */

#define     ACMP_INPUT_N_EXT0       0       /*!< positive pin select external pin 0 */
#define     ACMP_INPUT_N_EXT1       1       /*!< positive pin select external pin 1 */
#define     ACMP_INPUT_N_EXT2       2       /*!< positive pin select external pin 2 */
#define     ACMP_INPUT_N_DAC        3       /*!< positive pin select internal DAC */
/*! @} End of acmp_pinsel_list                                                */

/******************************************************************************
* ACMP interrupt sensitivity edge definition
*
*//*! @addtogroup acmp_intedgesel
* @{
*******************************************************************************/
#define     ACMP_SENSITIVITYMODE_FALLING   0    /*!< interrupt on falling edge  */
#define     ACMP_SENSITIVITYMODE_RISING    1    /*!< interrupt on rising edge  */
#define     ACMP_SENSITIVITYMODE_ANY       3    /*!< interrupt on falling or rising edge */ 
/*! @} End of acmp_intedgesel                                                 */


/******************************************************************************
* ACMP hysterisis selection definition
*
*//*! @addtogroup acmp_hyst
* @{
*******************************************************************************/
#define     ACMP_HYST_20MV                 (0<<6)  /*!< 20mv hyst */
#define     ACMP_HYST_30MV                 (1<<6)  /*!< 30mv hyst */
/*! @} End of acmp_hyst                                                       */


/******************************************************************************
* ACMP internal DAC reference selection definition
*
*//*! @addtogroup acmp_dacref
* @{
*******************************************************************************/
#define     ACMP_DAC_REFERENCE_BANDGAP     (0<<6)  /*!< select bandgap as refference */
#define     ACMP_DAC_REFERENCE_VDDA        (1<<6)  /*!< select VDDA as refference */
/*! @} End of acmp_dacref                                                     */


/******************************************************************************
* Types
******************************************************************************/

/*! @brief ACMP_CALLBACK function declaration                                  */
typedef void (*ACMP_CallbackPtr)(void);
/*! @} End of acmp_callback                                                    */

/******************************************************************************
* ACMP control status struct
*
*//*! @addtogroup acmp_ctrlstatusstruct
* @{
*******************************************************************************/
/*!
 * @brief ACMP control and status fields type.
 *
 */

typedef union 
{
    uint8_t byte;    				 /*!< byte field of union type */
    struct 
    {
        uint8_t bMod        : 2;     /*!< Sensitivity modes of the interrupt trigger */
        uint8_t bOutEn      : 1;     /*!< Output can be placed onto an external pin */
        uint8_t bOutState   : 1;     /*!< The current value of the analog comparator output */
        uint8_t bIntEn      : 1;     /*!< ACMP interrupt enable */
        uint8_t bIntFlag    : 1;     /*!< ACMP Interrupt Flag Bit */
        uint8_t bHyst       : 1;     /*!< Selects ACMP hystersis */
        uint8_t bEn         : 1;     /*!< Enables the ACMP module */
    }bits;      				     /*!< bitfield of union type */
}ACMP_CtrlStatusType, *ACMP_CtrlStatusPtr;  /*!< ACMP Control/Status reg structure */
/*! @} End of acmp_ctrlstatusstruct                                           */

/******************************************************************************
* ACMP pin select struct
*
*//*! @addtogroup acmp_pinselectstruct
* @{
*******************************************************************************/
/*!
* @brief ACMP external pins control struct.
*
*/

typedef union 
{
    uint8_t byte;    				 /*!< byte field of union type */
    struct 
    {
        uint8_t bNegPin  : 2;        /*!< Negative pin select */
        uint8_t          : 2;
        uint8_t bPosPin  : 2;        /*!< Positive pin select */
        uint8_t          : 2;
    }bits;      				     /*!< bitfield of union type */  
}ACMP_PinSelType, *ACMP_PinSelPtr; 	     /*!< ACMP Pin select structure */ 
/*! @} End of acmp_pinselectstruct                                            */

/******************************************************************************
* ACMP DAC control struct
*
*//*! @addtogroup acmp_dacctrlstruct
* @{
*******************************************************************************/
/*!
* @brief ACMP internal ADC control struct.
*
*/
typedef union 
{
    uint8_t byte;    				 /*!< byte field of union type */
    struct 
    {
        uint8_t bVal  : 6;           /*!< 6 bit DAC value */
        uint8_t bRef  : 1;           /*!< 6 bit DAC reference select */
        uint8_t bEn   : 1;           /*!< 6 bit DAC enable bit */
    }bits;      				     /*!< bitfield of union type */  
}ACMP_DACType, *ACMP_DACPtr;	     /*!< ACMP DAC control structure */ 
/*! @} End of acmp_dacctrlstruct                                              */

/******************************************************************************
* ACMP pin enable union
*
*//*! @addtogroup acmp_pinenunion
* @{
*******************************************************************************/
/*!
* @brief ACMP external input pin enable control struct.
*
*/
typedef union 
{
    uint8_t byte;    				 /*!< byte field of union type */
    struct 
    {
        uint8_t bEn   : 3;           /*!< ACMP external input pin enable */
        uint8_t bRsvd : 5;
    }bits;      				     /*!< bitfield of union type */  
}ACMP_PinEnType, *ACMP_PinEnPtr;	 /*!< ACMP Pin enable structure */ 
/*! @} End of acmp_pinenunion                                                 */

/******************************************************************************
* ACMP config struct
*
*//*! @addtogroup acmp_configstruct
* @{
*******************************************************************************/
/*!
* @brief ACMP module configuration struct.
*
*/

typedef struct 
{
    ACMP_CtrlStatusType  sCtrlStatus;     /*!< ACMP control and status */
    ACMP_PinSelType      sPinSelect;      /*!< ACMP pin select */
    ACMP_DACType         sDacSet;         /*!< ACMP internal dac set */
    ACMP_PinEnType       sPinEnable;      /*!< ACMP external pin control */
}ACMP_ConfigType, *ACMP_ConfigPtr;
/*! @} End of acmp_configstruct                                               */

/******************************************************************************
* Global variables
******************************************************************************/

/*!
 * inline functions
 */
/******************************************************************************
* ACMP api list.
*
*//*! @addtogroup acmp_api_list
* @{
*******************************************************************************/
/*****************************************************************************//*!
*
* @brief  enable the acmp module.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_Disable.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_Enable(ACMP_Type *pACMPx)
{
    pACMPx->CS |= ACMP_CS_ACE_MASK;
}

/*****************************************************************************//*!
*
* @brief  disable the acmp module.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_Enable.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_Disable(ACMP_Type *pACMPx)
{
    pACMPx->CS &= ~ACMP_CS_ACE_MASK;
}

/*****************************************************************************//*!
*
* @brief  select sensitivity modes of the interrupt trigger.
*        
* @param[in]  pACMPx         pointer to an ACMP module.
* @param[in]  u8EdgeSelect   falling or rising selction, 0~3. 
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_SelectIntMode(ACMP_Type *pACMPx, uint8_t u8EdgeSelect)
{
    pACMPx->CS &= ~ACMP_CS_ACMOD_MASK;
    pACMPx->CS |= ACMP_CS_ACMOD(u8EdgeSelect & 0x3);
}

/*****************************************************************************//*!
*
* @brief  enable the ACMP module analog comparator output.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_DisablePinOut.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_EnablePinOut(ACMP_Type *pACMPx)
{
    pACMPx->CS |= ACMP_CS_ACOPE_MASK;
}

/*****************************************************************************//*!
*
* @brief  disable the ACMP module analog comparator output.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_EnablePinOut.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DisablePinOut(ACMP_Type *pACMPx)
{
    pACMPx->CS &= ~ACMP_CS_ACOPE_MASK;
}

/*****************************************************************************//*!
*
* @brief  select ACMP hystersis. 
*        
* @param[in]  pACMPx           pointer to an ACMP module.
* @param[in]  u8HystSelect     ACMP_HYST_20MV or ACMP_HYST_30MV.   
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_SelectHyst(ACMP_Type *pACMPx, uint8_t u8HystSelect)
{
    pACMPx->CS &= ~ACMP_CS_HYST_MASK;
    pACMPx->CS |= u8HystSelect;
}

/*****************************************************************************//*!
*
* @brief  enable the acmp module interrupt.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_DisableInterrupt.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_EnableInterrupt(ACMP_Type *pACMPx)
{
    pACMPx->CS |= ACMP_CS_ACIE_MASK;
}

/*****************************************************************************//*!
*
* @brief  disable the acmp module interrupt.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_EnableInterrupt.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DisableInterrupt(ACMP_Type *pACMPx)
{
    pACMPx->CS &= ~ACMP_CS_ACIE_MASK;
}

/*****************************************************************************//*!
*
* @brief  get the interrupt flag bit.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_ClrFlag.
*
*****************************************************************************/
__STATIC_INLINE uint8_t ACMP_GetFlag(ACMP_Type *pACMPx)
{
    return (pACMPx->CS & ACMP_CS_ACF_MASK);
}

/*****************************************************************************//*!
*
* @brief  clear the interrupt flag bit.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_GetFlag.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_ClrFlag(ACMP_Type *pACMPx)
{
    pACMPx->CS &= ~ACMP_CS_ACF_MASK;
}

/*****************************************************************************//*!
*
* @brief  ACMP Positive Input Select.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8PosPinSel   positive input select, ACMP_INPUT_P_EXT0~2 or ACMP_INPUT_P_DAC.     
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_NegativeInputSelect.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_PositiveInputSelect(ACMP_Type *pACMPx, uint8_t u8PosPinSel)
{
    pACMPx->C0 &= ~ACMP_C0_ACPSEL_MASK;
    pACMPx->C0 |= u8PosPinSel;
}

/*****************************************************************************//*!
*
* @brief  ACMP Negative Input Select.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8NegPinSel   negative input select, ACMP_INPUT_N_EXT0~2 or ACMP_INPUT_N_DAC.  
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_PositiveInputSelect.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_NegativeInputSelect(ACMP_Type *pACMPx, uint8_t u8NegPinSel)
{
    pACMPx->C0 &= ~ACMP_C0_ACNSEL_MASK;
    pACMPx->C0 |= u8NegPinSel;
}

/*****************************************************************************//*!
*
* @brief  Enable 6 bit DAC in ACMP module.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_DacDisable.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DacEnable(ACMP_Type *pACMPx)
{
    pACMPx->C1 |= ACMP_C1_DACEN_MASK;
}

/*****************************************************************************//*!
*
* @brief  Disable 6 bit DAC in ACMP module.
*        
* @param[in]  pACMPx      pointer to an ACMP module.
*              
* @return none.
*
* @ Pass/ Fail criteria: none.
*
* @see    ACMP_DacEnable.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DacDisable(ACMP_Type *pACMPx)
{
    pACMPx->C1 &= ~ACMP_C1_DACEN_MASK;
}

/*****************************************************************************//*!
*
* @brief  ACMP 6 bit DAC Reference Select.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8RefSelect   dac reference select:ACMP_DAC_REFERENCE_BANDGAP or ACMP_DAC_REFERENCE_VDDA.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DacReferenceSelect(ACMP_Type *pACMPx, uint8_t u8RefSelect)
{
    pACMPx->C1 &= ~ACMP_C1_DACREF_MASK;
    pACMPx->C1 |= u8RefSelect;
}

/*****************************************************************************//*!
*
* @brief  ACMP 6 bit DAC Output Value Set.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8DacValue    dac output set, Voutput= (Vin/64)x(DACVAL[5:0]+1).
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_DacOutputSet(ACMP_Type *pACMPx, uint8_t u8DacValue)
{
    ASSERT(!(u8DacValue & (~ACMP_C1_DACVAL_MASK)));
    pACMPx->C1 &= ~ACMP_C1_DACVAL_MASK;
    pACMPx->C1 |= ACMP_C1_DACVAL(u8DacValue);
}

/*****************************************************************************//*!
*
* @brief  Enable ACMP input pin.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8InputPin    ACMP external pin, 0~2.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_InputPinEnable(ACMP_Type *pACMPx, uint8_t u8InputPin)
{
    ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
    pACMPx->C2 |= ACMP_C2_ACIPE(u8InputPin);
}

/*****************************************************************************//*!
*
* @brief  Disable ACMP input pin.
*        
* @param[in]  pACMPx        pointer to an ACMP module.
* @param[in]  u8InputPin    ACMP external pin, 0~2.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
__STATIC_INLINE void ACMP_InputPinDisable(ACMP_Type *pACMPx, uint8_t u8InputPin)
{
    ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
    pACMPx->C2 &= ~ACMP_C2_ACIPE(u8InputPin);
}

/*! @} End of acmp_api_list                                                  */

/******************************************************************************
* Global functions
******************************************************************************/
void ACMP_Init(ACMP_Type *pACMPx, ACMP_ConfigType *pConfig);
void ACMP_DeInit(ACMP_Type *pACMPx); 
void ACMP_ConfigDAC(ACMP_Type *pACMPx, ACMP_DACType *pDACConfig);
void ACMP_SetCallback(ACMP_Type *pACMPx, ACMP_CallbackPtr pfnCallback);

#ifdef __cplusplus
}
#endif
#endif /* _MY_ACMP_H_ */