nPZero IC
API and Examples for the nPZero
Loading...
Searching...
No Matches
ADC Example

Description

This example demonstrates the usage of the nPZero driver to setup the ADC and there threshould's. Booth ADC's (Internal and External) are set in the example code, the ADC_IN pin for this example is connected to VDD.

Code Example ADC

Code snippet ...

  • The example will firstly setup the configuration registers for ADC. This is done in SYSCFG1!!!.
  • Set the Over and Under threshold registers in the nPZero by setting ....
// Set Global Timeout
static void set_global_timeout(np0_device_s *device)
{
// Wake up Host every 5.71 min at 10Hz (343 sec * 10Hz = 0x0D66)
np0_device_set_wakeup_timeout(device, 0x66, 0x0D);
}
// Set System Config 1
static void set_system_config1(np0_device_s *device)
{
device->syscfg1.sclk_div_en = 0;
device->syscfg1.sclk_sel = 0;
device->syscfg1.wup1 = 0;
device->syscfg1.wup2 = 0;
device->syscfg1.wup3 = 0;
device->syscfg1.wup4 = 0;
if (np0_write_SYSCFG1(device->syscfg1) != OK)
{
printf("Failed to write SYSCFG1 register\r\n");
}
}
// Set System Config 2
static void set_system_config2(np0_device_s *device, uint8_t adc_ext_on, uint8_t adc_div_en,
np0_adc_div_sel_e adc_div_sel, uint8_t WUP_A1, uint8_t WUP_A2, uint8_t wupmod)
{
device->syscfg2.adc_ext_on = adc_ext_on;
device->syscfg2.adc_div_en = adc_div_en;
device->syscfg2.adc_div_sel = adc_div_sel;
device->syscfg2.ta1_en = WUP_A1; // Enables the internal ADC channel trigger (VBAT pin) as a wake up source
device->syscfg2.ta2_en = WUP_A2; // Enables the external ADC channel (ADC_IN pin) as a wake up source
device->syscfg2.wupmod = wupmod;
if (np0_write_SYSCFG2(device->syscfg2) != OK)
{
printf("Failed to write SYSCFG2 register\n\r\n");
}
}
/*****************************************************************************
* Public Methods
*/
void example_adc_external(np0_device_s *device)
{
printf("Executing external ADC example\r\n");
set_global_timeout(device);
set_system_config1(device);
set_system_config2(device, 1, 1, ADC_DIV_SEL_2, 0, 1, 0);
// Set External ADC (ADC_IN) over threshold Value
device->throva2.throva = 0x1E; // 1.3 V
if (np0_write_THROVA2(device->throva2) != OK)
{
printf("Failed to write THROVA01 register\r\n");
return;
}
// Set External ADC (ADC_IN) under threshold value
device->thruna2.thruna = 0x0B; // (3.6 V)
if (np0_write_THRUNA2(device->thruna2) != OK)
{
printf("Failed to write THRUNA2 register\r\n");
return;
}
// Put the device int to sleep mode
}
void example_adc_internal(np0_device_s *device)
{
printf("Executing internal ADC example\r\n");
set_global_timeout(device);
set_system_config1(device);
set_system_config2(device, 0, 1, ADC_DIV_SEL_2, 1, 0, 0);
// Set Internal ADC over threshold Value
device->throva1.throva = 0x1E; // 1.3 V
if (np0_write_THROVA1(device->throva1) != OK)
{
printf("Failed to write THROVA01 register\r\n");
return;
}
// Set Internal ADC under threshold value
device->thruna1.thruna = 0x0B; // 3.6 V
if (np0_write_THRUNA1(device->thruna1) != OK)
{
printf("Failed to write THRUNA1 register\r\n");
return;
}
// Put the device int to sleep mode
}
*
np0_status_e np0_write_SYSCFG1(const np0_register_syscfg1_s syscfg1)
Writes the syscfg1 struct to the syscfg1 register.
Definition: np0.c:84
np0_status_e np0_write_THRUNA1(const np0_register_thruna1_s thruna1)
Writes the thruna1 struct to the TRHUNA1 register.
Definition: np0.c:201
np0_status_e np0_write_SYSCFG2(const np0_register_syscfg2_s syscfg2)
Writes the syscfg2 struct to the syscfg2 register.
Definition: np0.c:104
np0_status_e np0_write_THROVA1(const np0_register_throva1_s throva1)
Writes the throva1 struct to the throva1 register.
Definition: np0.c:173
@ OK
Definition: np0.h:32
np0_status_e np0_write_THRUNA2(const np0_register_thruna2_s thruna2)
Writes the thruna2 struct to the TRHUNA2 register.
Definition: np0.c:215
np0_status_e np0_write_THROVA2(const np0_register_throva2_s throva2)
Writes the throva2 struct to the throva2 register.
Definition: np0.c:187
@ SCLK_DIV_SEL_2
Definition: np0.h:135
np0_adc_div_sel_e
Definition: np0.h:143
@ ADC_DIV_SEL_2
Definition: np0.h:144
void np0_device_go_sleep(np0_device_s *device)
Put the device into sleep mode.
Definition: np0_device_control.c:298
void np0_device_set_wakeup_timeout(np0_device_s *device, uint8_t tout_l, uint8_t tout_h)
Sets time out until host wakes up.
Definition: np0_device_control.c:325
Definition: np0.h:467
np0_register_throva1_s throva1
Definition: np0.h:477
np0_register_syscfg2_s syscfg2
Definition: np0.h:474
np0_register_syscfg1_s syscfg1
Definition: np0.h:473
np0_register_throva2_s throva2
Definition: np0.h:478
np0_register_thruna2_s thruna2
Definition: np0.h:480
np0_register_thruna1_s thruna1
Definition: np0.h:479
uint8_t sclk_sel
Definition: np0.h:242
uint8_t wup4
Definition: np0.h:250
uint8_t wup3
Definition: np0.h:248
uint8_t wup2
Definition: np0.h:246
uint8_t wup1
Definition: np0.h:244
uint8_t sclk_div_en
Definition: np0.h:239
np0_sclk_div_sel_e sclk_div_sel
Definition: np0.h:241
np0_adc_div_sel_e adc_div_sel
Definition: np0.h:261
uint8_t adc_ext_on
Definition: np0.h:257
uint8_t ta2_en
Definition: np0.h:264
uint8_t wupmod
Definition: np0.h:266
uint8_t adc_div_en
Definition: np0.h:259
uint8_t ta1_en
Definition: np0.h:262
uint8_t throva
Definition: np0.h:413
uint8_t throva
Definition: np0.h:420
uint8_t thruna
Definition: np0.h:427
uint8_t thruna
Definition: np0.h:434

Note

  • Adapt the code example to match your MCU throw np0_hal_read and np0_hal_write.
  • Consult the nPZero driver function documentation for precise function usage.