1.在sdk_config.h中加入宏
// COMP_ENABLED - nrf_drv_comp - COMP peripheral driver //========================================================== #ifndef COMP_ENABLED #define COMP_ENABLED 1 #endif // COMP_CONFIG_REF - Reference voltage // Internal 1.2V // Internal 1.8V // Internal 2.4V // VDD // ARef
#ifndef COMP_CONFIG_REF #define COMP_CONFIG_REF 1 #endif
// COMP_CONFIG_MAIN_MODE - Main mode // Single ended // Differential
#ifndef COMP_CONFIG_MAIN_MODE #define COMP_CONFIG_MAIN_MODE 0 #endif
// COMP_CONFIG_SPEED_MODE - Speed mode // Low power // Normal // High speed
#ifndef COMP_CONFIG_SPEED_MODE #define COMP_CONFIG_SPEED_MODE 2 #endif
// COMP_CONFIG_HYST - Hystheresis // No // 50mV
#ifndef COMP_CONFIG_HYST #define COMP_CONFIG_HYST 0 #endif
// COMP_CONFIG_ISOURCE - Current Source // Off // 2.5 uA // 5 uA // 10 uA
#ifndef COMP_CONFIG_ISOURCE #define COMP_CONFIG_ISOURCE 0 #endif
// COMP_CONFIG_INPUT - Analog input // 0 // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef COMP_CONFIG_INPUT #define COMP_CONFIG_INPUT 7 #endif
// COMP_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef COMP_CONFIG_IRQ_PRIORITY #define COMP_CONFIG_IRQ_PRIORITY 7 #endif
2.导入nrfx_comp.c到工程
3.comp事件处理函数
#include "nrf_drv_comp.h"
void comp_event_handler(nrf_comp_event_t evt) { printf("\r\ncomp_event_handler\r\n"); }
4.comp初时化使用 uint32_t err_code; nrf_drv_comp_config_t comp_config = NRF_DRV_COMP_DEFAULT_CONFIG(NRF_COMP_INPUT_7); comp_config.isource = NRF_COMP_ISOURCE_Ien5uA; err_code = nrf_drv_comp_init(&comp_config, comp_event_handler); nrf_drv_comp_start(NRF_DRV_COMP_EVT_EN_CROSS_MASK ,NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT);