Pulse Counter

Overview

The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of an input signal. Each pulse counter unit has a 16-bit signed counter register and two channels that can be configured to either increment or decrement the counter. Each channel has a signal input that accepts signal edges to be detected, as well as a control input that can be used to enable or disable the signal input. The inputs have optional filters that can be used to discard unwanted glitches in the signal.

Application Example

Pulse counter with control signal and event interrupt example: examples/16_pcnt.

API Reference

Header Files

Macros

Type Definitions

Enumerations

enum pcnt_ctrl_mode_t

Values:

PCNT_MODE_KEEP = 0

Control mode: won’t change counter mode

PCNT_MODE_REVERSE = 1

Control mode: invert counter mode(increase -> decrease, decrease -> increase);

PCNT_MODE_DISABLE = 2

Control mode: Inhibit counter(counter value will not change in this condition)

PCNT_MODE_MAX
enum pcnt_count_mode_t

Values:

PCNT_COUNT_DIS = 0

Counter mode: Decrease counter value

PCNT_COUNT_INC = 1

Counter mode: Increase counter value

PCNT_COUNT_DEC = 2

Counter mode: Inhibit counter(counter value will not change in this condition)

PCNT_COUNT_MAX
enum pcnt_unit_t

Values:

PCNT_UNIT_0 = 0

PCNT unit0

PCNT_UNIT_1 = 1

PCNT unit1

PCNT_UNIT_2 = 2

PCNT unit2

PCNT_UNIT_3 = 3

PCNT unit3

PCNT_UNIT_4 = 4

PCNT unit4

PCNT_UNIT_5 = 5

PCNT unit5

PCNT_UNIT_6 = 6

PCNT unit6

PCNT_UNIT_7 = 7

PCNT unit7

PCNT_UNIT_MAX
enum pcnt_channel_t

Values:

PCNT_CHANNEL_0 = 0x00

PCNT channel0

PCNT_CHANNEL_1 = 0x01

PCNT channel1

PCNT_CHANNEL_MAX
enum pcnt_evt_type_t

Values:

PCNT_EVT_L_LIM = 0

PCNT watch point event: Minimum counter value

PCNT_EVT_H_LIM = 1

PCNT watch point event: Maximum counter value

PCNT_EVT_THRES_0 = 2

PCNT watch point event: threshold0 value event

PCNT_EVT_THRES_1 = 3

PCNT watch point event: threshold1 value event

PCNT_EVT_ZERO = 4

PCNT watch point event: counter value zero event

PCNT_EVT_MAX

Structures

struct pcnt_config_t

Pulse Counter configure struct.

Functions

esp_err_t pcnt_unit_config(pcnt_config_t *pcnt_config)

Configure Pulse Counter unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_config: Pointer of Pulse Counter unit configure parameter

esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t *count)

Get pulse counter value.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: Pulse Counter unit number
  • count: Pointer to accept counter value

esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)

Pause PCNT counter of PCNT unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: PCNT unit number

esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)

Resume counting for PCNT counter.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: PCNT unit number, select from pcnt_unit_t

esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)

Clear and reset PCNT counter value to zero.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: PCNT unit number, select from pcnt_unit_t

esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)

Enable PCNT interrupt for PCNT unit.

Note
Each Pulse counter unit has five watch point events that share the same interrupt. Configure events with pcnt_event_enable() and pcnt_event_disable()
Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: PCNT unit number

esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)

Disable PCNT interrupt for PCNT uint.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • pcnt_unit: PCNT unit number

esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)

Enable PCNT event of PCNT unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).

esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)

Disable PCNT event of PCNT unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).

esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)

Set PCNT event value of PCNT unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
  • value: Counter value for PCNT event

esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)

Get PCNT event value of PCNT unit.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
  • value: Pointer to accept counter value for PCNT event

esp_err_t pcnt_isr_register(uint32_t pcnt_intr_num, void (*fn)(void *), void *arg, )

Register PCNT interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on.

Note
Users should know that which CPU is running and then pick a INUM that is not used by system. We can find the information of INUM and interrupt level in soc.h.
Note
Note that the handler function MUST be defined with attribution of “IRAM_ATTR”.
Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Function pointer error.
Parameters
  • pcnt_intr_num: PCNT interrupt number, check the info in soc.h, and please see the core-isa.h for more details
  • fn: Interrupt handler function.
Parameters
  • arg: Parameter for handler function

esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)

Configure PCNT pulse signal input pin and control input pin.

Note
Set to PCNT_PIN_NOT_USED if unused.
Note
Set to PCNT_PIN_NOT_USED if unused.
Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • channel: PCNT channel number
  • pulse_io: Pulse signal input GPIO
Parameters
  • ctrl_io: Control signal input GPIO

esp_err_t pcnt_filter_enable(pcnt_unit_t unit)

Enable PCNT input filter.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number

esp_err_t pcnt_filter_disable(pcnt_unit_t unit)

Disable PCNT input filter.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number

esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)

Set PCNT filter value.

Note
filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • filter_val: PCNT signal filter value, counter in APB_CLK cycles. Any pulses lasting shorter than this will be ignored when the filter is enabled.

esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)

Get PCNT filter value.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • filter_val: Pointer to accept PCNT filter value.

esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode, pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode)

Set PCNT counter mode.

Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • unit: PCNT unit number
  • channel: PCNT channel number
  • pos_mode: Counter mode when detecting positive edge
  • neg_mode: Counter mode when detecting negative edge
  • hctrl_mode: Counter mode when control signal is high level
  • lctrl_mode: Counter mode when control signal is low level