Abstract
The digitalRead() returns if the specified pin is HIGH or LOW. If the value of the corresponding bit of the register is 1 it returns HIGH, if 0 it returns LOW.
There are three registers, PORTB, PORTC and PORTD. The relation of the bit of the registers and the pins of Arduino Uno is shown below. Ax is an analog pin, Dx is a digital pin.
PORTB | ||||||||
---|---|---|---|---|---|---|---|---|
Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Pin | - | - | D13 | D12 | D11 | D10 | D9 | D8 |
PORTC | ||||||||
Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Pin | - | - | A5 | A4 | A3 | A2 | A1 | A0 |
PORTD | ||||||||
Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Pin | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
The rough diagram to convert Arduino pin number to ATmega328P register is shown below.
The digitalRead() is a function that dose
- calculate which bit of PORTx for the pin
- read the value of the PORTx
- return the result
Source Code
The digitalRead() is defined in hardware/arduino/avr/cores/arduino/wiring_digital.c as below.
|
|
The input argument is pin. The type of the variable is uint8_t.
First calculate the timer of the pin. Some of the digital pins are related to timer. These pins can output PWM.
|
|
The digitalPinToTimer() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It returns a timer name if the pin is related to a timer. If not, it returns NOT_ON_TIMER.
Next calculate the bit mask, which bit in the PORTx, of the pin.
|
|
The digitalPinToBitMask() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It returns a value with a corresponding bit, shown in the table above, of DDR set to 1. For example, if the pin is 13, the value 0b00100000 is returned. It means the fifth bit of PORTB.
Next, calculate a port corresponds to the pin.
|
|
The digitalPinToPort() is a macro defined also in hardware/arduino/avr/cores/arduino/Arduino.h. It returns the PORT corresponds to the pin. If the pin is from 0 to 7, it returns PD, from 8 to 13 it returns PB, from 14(A0) to 19(A5) it returns PC.
If the port is NOT_A_PIN, the function returns LOW.
|
|
In case of Arduino Uno, if the input value is correct/valid, digitalPinToPort() dose not return NOT_A_PIN. If the input value is incorrect/invalid, it refers to the out of range of an array, and its behavior is undefined. This means we must not give an incorrect input parameters.
|
|
If the pin is related to timer, turn it off. The turnOffPWM() is a function defined in hardware/arduino/avr/cores/arduino/wiring_digital.c, and it stops the timer.
After that, the port is converted to a register, then calculate logical AND with bit. If the result is not 0, the function returns HIGH, else it returns LOW.
|
|
The portInputRegister() is a macro defined in hardware/arduino/avr/cores/arduino/Arduino.h. It converts the port to an address. It returns one of the PORTB, PORTC or PORTD. If the port is equal to PB, it returns PORTB.
Because PORTs are addresses, ‘*’ is required to get the content of the address.
If the result of calculating logical AND with bit is not 0 it returns HIGH, if 0 it returns LOW.
Version
Arduino AVR Boards 1.8.6
Last Update
March 21, 2023