文章插图
的CHRG 和 STDBY引脚时高阻态或者拉低状态 , 所以我们设置的IO口为内部上拉输入状态 , 我们只需要检测三种状态
充电器未接入 CHRG引脚 低电平;STDBY引脚 低电平正在充电 CHRG引脚 低电平;STDBY引脚 高电平电池充满 CHRG引脚 高电平;STDBY引脚 低电平 电源锁定
电源锁定指的是我们通过按键按下 , 系统上电开机后我们通过程序控制 IO 口拉高来控制供电开关mos管持续导通 , 这样当我们松开按键的时候系统依然正常供电工作 , 我们的硬件电路中除了通过 IO 口来锁定电源之外还可以通过插入充电器来锁定电源 。
工程建立
我们把RTT文件夹复制一份并重命名位Power作为我们本节的工程文件夹 。
修改.h
打开.h文件 , 在其中添加下边的配置宏定义
// nrf_strerror#define NRF_STRERROR_ENABLED 1// TIMER periperal driver#define NRFX_TIMER_ENABLED 1#define NRFX_TIMER0_ENABLED 1// SAADC peripheral driver#define NRFX_SAADC_ENABLED 1// PPI peripheral allocator#define NRFX_PPI_ENABLED 1
上边配置模块就是我们要用到的模块 。
修改
打开文件 , 将 和 变量修改为如下代码
# Source files common to all targetsSRC_FILES += \$(PROJ_DIR)/main.c \$(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52.S \$(SDK_ROOT)/modules/nrfx/mdk/system_nrf52.c \$(SDK_ROOT)/components/libraries/atomic/nrf_atomic.c \$(SDK_ROOT)/components/libraries/balloc/nrf_balloc.c \$(SDK_ROOT)/components/libraries/util/app_util_platform.c \$(SDK_ROOT)/components/libraries/experimental_memobj/nrf_memobj.c \$(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_backend_rtt.c \$(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_default_backends.c \$(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_frontend.c \$(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_str_formatter.c \$(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_backend_serial.c \$(SDK_ROOT)/external/segger_rtt/SEGGER_RTT.c \$(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_Syscalls_GCC.c \$(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_printf.c \$(SDK_ROOT)/external/fprintf/nrf_fprintf.c \$(SDK_ROOT)/external/fprintf/nrf_fprintf_format.c \\\$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_ppi.c \$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_saadc.c \$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_timer.c \$(SDK_ROOT)/components/libraries/util/app_error.c \$(SDK_ROOT)/components/libraries/util/app_error_weak.c \$(SDK_ROOT)/components/libraries/strerror/nrf_strerror.c \# Include folders common to all targetsINC_FOLDERS += \$(PROJ_DIR) \$(SDK_ROOT)/modules/nrfx \$(SDK_ROOT)/modules/nrfx/mdk \$(SDK_ROOT)/integration/nrfx \$(SDK_ROOT)/components/toolchain/cmsis/include \$(SDK_ROOT)/components/drivers_nrf/nrf_soc_nosd \$(SDK_ROOT)/components/libraries/atomic \$(SDK_ROOT)/components/libraries/balloc \$(SDK_ROOT)/components/libraries/delay \$(SDK_ROOT)/components/libraries/strerror \$(SDK_ROOT)/components/libraries/util \$(SDK_ROOT)/components/libraries/experimental_log/src \$(SDK_ROOT)/components/libraries/experimental_log \$(SDK_ROOT)/components/libraries/experimental_memobj \$(SDK_ROOT)/components/libraries/experimental_section_vars \$(SDK_ROOT)/external/fprintf \$(SDK_ROOT)/external/segger_rtt \\\$(SDK_ROOT)/modules/nrfx/hal \$(SDK_ROOT)/modules/nrfx/drivers/include \
其实就是添加了本节要用的的源文件和头文件包含路径 。
修改main.c
打开main.c文件将代码修改为如下代码
#include "nrf_delay.h"#include "nrf_log.h"#include "nrf_log_ctrl.h"#include "nrf_log_default_backends.h"#include "nrfx_saadc.h"#include "nrfx_ppi.h"#include "nrfx_timer.h"#include "nrf_gpio.h"#define POWER_LOCK_PIN6#define CHRG_STATE_PIN3#define CHRG_STDBY_PIN4#define ADC_2_MILIVOLT(ADC)(ADC*1000/4551)static const nrfx_timer_tm_timer = NRFX_TIMER_INSTANCE(0);static nrf_saadc_value_tsaadc_value;static nrf_ppi_channel_tm_ppi_channel;void timer_handler(nrf_timer_event_t event_type, void * p_context){}void saadc_callback(nrfx_saadc_evt_t const * p_event){ret_code_t err_code;static uint8_t calib_cnt = 0;if(p_event->type == NRFX_SAADC_EVT_DONE){if(calib_cnt ++ > 100){calib_cnt = 0;nrfx_saadc_calibrate_offset();}else{err_code = nrfx_saadc_buffer_convert(&saadc_value, 1);APP_ERROR_CHECK(err_code);int16_t adc_val = ADC_2_MILIVOLT(p_event->data.done.p_buffer[0]);int16_t bat_val = adc_val * 2;NRF_LOG_INFO("ADC milivolt: %d mV, Battery: %d mV", adc_val, bat_val);}}}void saadc_init(void){ret_code_t err_code;nrfx_saadc_config_t saadc_config = {.resolution= NRF_SAADC_RESOLUTION_14BIT,.oversample= NRF_SAADC_OVERSAMPLE_DISABLED,.interrupt_priority = APP_IRQ_PRIORITY_LOWEST,.low_power_mode= false,};nrf_saadc_channel_config_t channel_config = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,.resistor_n = NRF_SAADC_RESISTOR_DISABLED,.gain= NRF_SAADC_GAIN1_6,.reference= NRF_SAADC_REFERENCE_INTERNAL,.acq_time= NRF_SAADC_ACQTIME_40US,.mode= NRF_SAADC_MODE_SINGLE_ENDED,.burst= NRF_SAADC_BURST_DISABLED,.pin_p= (nrf_saadc_input_t)(NRF_SAADC_INPUT_AIN3),.pin_n= NRF_SAADC_INPUT_DISABLED,};err_code = nrfx_saadc_init(&saadc_config, saadc_callback);APP_ERROR_CHECK(err_code);err_code = nrfx_saadc_channel_init(3, &channel_config);APP_ERROR_CHECK(err_code);// Block Readnrfx_saadc_sample_convert(3, &saadc_value);NRF_LOG_INFO("Start up Block Read milivolt: %d mV", ADC_2_MILIVOLT(saadc_value));}void saadc_sampling_event_init(void){ret_code_t err_code;nrfx_timer_config_t timer_cfg = {.frequency= NRF_TIMER_FREQ_500kHz,.mode= NRF_TIMER_MODE_TIMER,.bit_width= NRF_TIMER_BIT_WIDTH_32,.interrupt_priority = APP_IRQ_PRIORITY_LOWEST,.p_context= NULL,};err_code = nrfx_timer_init(&m_timer, &timer_cfg, timer_handler);APP_ERROR_CHECK(err_code);/* setup m_timer for compare event every 1000ms */uint32_t ticks = nrfx_timer_ms_to_ticks(&m_timer, 1000);nrfx_timer_extended_compare(&m_timer,NRF_TIMER_CC_CHANNEL0,ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,false);nrfx_timer_enable(&m_timer);uint32_t timer_compare_event_addr = nrfx_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0);uint32_t saadc_sample_task_addr= nrfx_saadc_sample_task_get();/* setup ppi channel so that timer compare event is triggering sample task in SAADC */err_code = nrfx_ppi_channel_alloc(&m_ppi_channel);APP_ERROR_CHECK(err_code);err_code = nrfx_ppi_channel_assign(m_ppi_channel,timer_compare_event_addr,saadc_sample_task_addr);APP_ERROR_CHECK(err_code);}void saadc_sampling_event_enable(void){ret_code_t err_code = nrfx_saadc_buffer_convert(&saadc_value, 1);APP_ERROR_CHECK(err_code);err_code = nrfx_ppi_channel_enable(m_ppi_channel);APP_ERROR_CHECK(err_code);}/*** @brief Function for application main entry.*/int main(void){NRF_LOG_INIT(NULL);NRF_LOG_DEFAULT_BACKENDS_INIT();// Config Power lock pinnrf_gpio_cfg_output(POWER_LOCK_PIN);// Config charging state pinnrf_gpio_cfg_input(CHRG_STATE_PIN, NRF_GPIO_PIN_PULLUP);nrf_gpio_cfg_input(CHRG_STDBY_PIN, NRF_GPIO_PIN_PULLUP);// Config Battery ADCsaadc_init();saadc_sampling_event_init();saadc_sampling_event_enable();int cnt = 0;// Lock Power supplynrf_gpio_pin_set(POWER_LOCK_PIN);while (true){nrf_delay_ms(1000);if(nrf_gpio_pin_read(CHRG_STATE_PIN) && nrf_gpio_pin_read(CHRG_STDBY_PIN)){// No Charger}else if(!nrf_gpio_pin_read(CHRG_STATE_PIN) && nrf_gpio_pin_read(CHRG_STDBY_PIN)){// Charging}else if(nrf_gpio_pin_read(CHRG_STATE_PIN) && !nrf_gpio_pin_read(CHRG_STDBY_PIN)){// Charging Complete}if(cnt
- 不适合做双眼皮的眼型图片,什么眼型适合割双眼皮
- 蜡丸帛书的解释蜡丸帛书的出处及用法
- 不锈钢铆钉和铝铆钉的区别,不锈钢铆钉铆在不锈钢板上可以吗
- 狼心狗行的解释狼心狗行的出处及用法
- 联床风雨的解释联床风雨的出处及用法
- 不要坐晚上的飞机,小孩坐飞机要不要买票
- 龙血凤髓的解释龙血凤髓的出处及用法
- 不顺的时候怎样摸貔貅,摸了貔貅嘴怎么办
- 令人齿冷的解释令人齿冷的出处及用法
- 力所不逮的解释力所不逮的出处及用法