This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

General Purpose Digital IO

Functions to handle DIO ports

The following functions are used to operate General Purpose Digital IO (DIO).

  • pinMode()
  • digitalWrite()
  • digitalRead()
  • attachIntDio()
  • detachIntDio()

Constants

Pin Names and Numbers

DefinitionName
const uint8_t PIN_DIGITAL::DIO0 .. 19DIO pins 0 to 19
const uint8_t PIN_DIGITAL::DO0 .. 1DO pins 0,1

Pin Modes (DIO0..19)

The following enumerated values are handled by the type E_PIN_MODE.

DefinitionPull-upName
PIN_MODE::INPUTNoInput
PIN_MODE::OUTPUTNoOutput
PIN_MODE::INPUT_PULLUPYesInput with Pull-up
PIN_MODE::OUTPUT_INIT_HIGHNoOutput (initial state HIGH)
PIN_MODE::OUTPUT_INIT_LOWNoOutput (initial state LOW)
PIN_MODE::WAKE_FALLINGNoInput, wake pin, falling edge
PIN_MODE::WAKE_RISINGNoInput, wake pin, rising edge
PIN_MODE::WAKE_FALLING_PULLUPYesInput, wake pin, falling edge with pull-up
PIN_MODE::WAKE_RISING_PULLUPYesInput, wake pin, rising edge with pull-up
PIN_MODE::DISABLE_OUTPUTYesReturn to input state

Pin Modes (DO0,1)

The following enumerated values are handled by the type E_PIN_MODE.

DefinitionName
PIN_MODE::OUTPUTOutput
PIN_MODE::OUTPUT_INIT_HIGHOutput (initial state HIGH)
PIN_MODE::OUTPUT_INIT_LOWOutput (initial state LOW)
PIN_MODE::DISABLE_OUTPUTStop output setting

Pin States

The following enumerated values are handled by the type E_PIN_STATE.

DefinitionValueName
PIN_STATE::HIGH1HIGH level (=Vcc level)
PIN_STATE::LOW0LOW level (=GND level)

Pin Rising and Falling Edges

The following enumerated values are handled by the type E_PIN_INT_MODE.

DefinitionName
PIN_INT_MODE::FALLINGFalling edge
PIN_INT_MODE::RISINGRising edge

1 - pinMode()

Initialization function
Configures the settings for DIO (General Purpose Digital IO) pins.
void pinMode(uint8_t u8pin, E_PIN_MODE mode)

This function allows configuration of the states of DIO0 to DIO19 and DO0,1 pins. For details on the configuration values, refer to the enumerated values of E_PIN_MODE in the DIO explanation and the DO explanation.

2 - digitalWrite()

Digital output function
Changes the setting of a digital output pin.
static inline void digitalWrite(uint8_t u8pin, E_PIN_STATE ulVal)

Beforehand, set the target pin as output using pinMode(). The first parameter specifies the pin number to be set. The second parameter specifies either HIGH or LOW.

3 - digitalRead()

Digital input function
Reads the value of a port configured as input.
static inline E_PIN_STATE digitalRead(uint8_t u8pin)

You can get the input value of a pin previously configured as input as LOW or HIGH.

4 - attachIntDio()

Function to register a DIO interrupt handler
Registers a DIO interrupt handler.
void attachIntDio(uint8_t u8pin, E_PIN_INT_MODE mode)

For a pin configured as input beforehand, the first parameter is the pin number for which you want to enable the interrupt, and the second parameter specifies the interrupt direction (rising edge, falling edge).

Example

Sets up an interrupt that triggers when the DIO5 pin changes from HIGH to LOW.

void setup() {
  the_twelite.app.use<myAppClass>();

  pinMode(PIN_DIGITAL::DIO5, PIN_MODE::INPUT_PULLUP);
  attachIntDio(PIN_DIGITAL::DIO5, PIN_INT_MODE::FALLING);
}

void loop() {
  ;
}

myAppClass.hpp

class myAppClass: public mwx::BrdPal, MWX_APPDEFS_CRTP(myAppClasslMot)
{

};

Basic definition of the behavior myAppClass. Details are omitted.

myAppClass.cpp

/*****************************************************************/
// MUST DEFINE CLASS NAME HERE
##define __MWX_APP_CLASS_NAME myAppClass
##include "_mwx_cbs_cpphead.hpp"
/*****************************************************************/

MWX_DIO_INT(PIN_DIGITAL::DIO5, uint32_t arg, uint8_t& handled) {
  static uint8_t ct;
  digitalWrite(PIN_DIGITAL::DIO12, (++ct & 1) ? HIGH : LOW);
	handled = false; // if true, no further event.
}

MWX_DIO_EVENT(PIN_DIGITAL::DIO5, uint32_t arg) {
  Serial << '*';
}

/*****************************************************************/
// common procedure (DO NOT REMOVE)
##include "_mwx_cbs_cpptail.cpp"
// MUST UNDEF CLASS NAME HERE
##undef __MWX_APP_CLASS_NAME
} // mwx
/*****************************************************************/

Interrupt handler description for the behavior myAppClass. When an interrupt occurs on DIO5, it toggles the output setting of DIO12, and after the interrupt handler finishes, an event occurs that prints * to the serial port Serial.

5 - detachIntDio()

Function to unregister a DIO interrupt handler
Unregisters a DIO interrupt handler.
void detachIntDio(uint8_t u8pin)

6 - digitalReadBitmap()

Digital input function (batch)
Reads the values of the input-configured ports in batch.
uint32_t digitalReadBitmap()

Values are stored in order from the LSB side: DIO0 … DIO19.

Pins on the HIGH side are set to 1, and pins on the LOW side are set to 0.