About Inputs and Outputs
Most general purpose pins on a microcontroller can function in one of two modes: input mode, or output mode.
When acting as an input, the general purpose input/output pin will be placed in a high-impedance state. The microcontroller will then sense the general purpose input/output pin, and the program can read the state of the pin and make decisions based on it.
When in output mode, the microcontroller will connect the general purpose input/output pin to either Vcc (the positive supply), or Vss (ground, or the negative supply). The program can then set the state of the pin to either high or low.
GCBASIC will attempt to determine the direction of each general purpose input/output pin, and set it appropriately, when possible. However, if the pin is both read from and written to in your program, then it must be configured to input or output mode by the program, using the appropriate Dir commands.
Example of dir commands.
'The port address is microcontroller specific. Portx.x is a general case for PICs and AVRs
dir PORTB.0 in ' <<< the Dir instruction setting a single pin's direction
dir PORTB.1 out
'The port address is microcontroller specific. GPIOx.x is a general case for some PICs
dir GPIO.0 in
dir GPIO.1 Out
'Set the whole port as an output
dir PORTB out
dir GPIO out
'Set the whole port as an input
dir PORTC in
dir GPIO inKey line: dir PORTB.0 in — configures a single pin (bit 0 of PORTB) as a digital input; the remaining lines show the same command used on a whole
port and on the alternate GPIO naming some PIC families use.
Microchip specifics for read/write operations
For the specific ports and general purpose input/output pins available for a specific microcontroller, please refer to the datasheet.
| Port | Purpose | Example |
|---|---|---|
|
PORTx maps to the microcontroller’s digital pins 0 to 7, where x can be A, B, C, D, E, F, or G |
Read: PORTx is the port data register for a read operation. |
uservar=PORTA uservar=PORTA.1 |
|
PORTx maps to the microcontroller’s digital pins 0 to 7, where x can be A, B, C, D, E, F, or G |
Write: PORTx is the port data register for a write operation; LATx is not required, as GCBASIC will implement LATx when needed. See #Option NoLatch for more information on LAT registers and how to disable this automatic function. |
PORTA=255 PORTA.1=1 |
To read a general purpose input/output pin, you need to ensure the direction is correct: DIR Portx IN (default is IN), or a specific set of port bits.
uservar = PORTx.n can be used.
Examples:
uservar = PORTb.0
uservar = PORTbTo write to a general purpose input/output pin, you need to ensure the direction is correct: DIR Portx OUT for the port, or a specific set of port bits.
PORTx.n = uservar can be used.
Examples:
PORTb.0 = uservar
PORTb = uservarATMEL specifics for read/write operations
Using a Mega328p as a general example, the following provides insights for AVR devices. For the specific ports and general purpose input/output pins available for a specific microcontroller, please refer to the datasheet.
| Port | Write operation | Read operation |
|---|---|---|
|
PORTD maps to the Mega328p (and other AVR microcontrollers) digital pins 0 to 7 |
PORTD - The Port D Data Register - write operation (a read operation on a port will provide the pull-up status) |
PIND - The Port D Input Pins Register - read only |
|
PORTB maps to the Mega328p (and other AVR microcontrollers) digital pins 8 to 13. The two high bits (6 & 7) map to the crystal pins and are not usable |
PORTB - The Port B Data Register - write operation (a read operation on a port will provide the pull-up status) |
PINB - The Port B Input Pins Register - read only |
|
PORTC maps to the Mega328p (and other AVR microcontrollers) analog pins 0 to 5. Pins 6 & 7 are only accessible on the Mega328p Mini |
PORTC - The Port C Data Register - write operation (a read operation on a port will provide the pull-up status) |
PINC - The Port C Input Pins Register - read only |
To read a general purpose input/output pin, you need to ensure the direction is correct: DIR Portx IN (default is IN), or a specific set of port bits.
uservar = PINx.n can be used, and to read a whole data port use uservar = PINx.
Examples:
uservar = PINb.0
uservar = PINbTo write to a general purpose input/output pin, you need to ensure the direction is correct: DIR Portx OUT for the port, or a specific set of port bits.
PORTx.n = uservar can be used, and to write to a whole data port use PORTx = uservar.
Examples:
PORTb.0 = uservar
PORTb = uservar
Setting Ports and Port.bit
You can set a port as shown above with a variable, or you can set it with a constant, or any combination using the bitwise and logical operators.
#define InitStateofPort 0b11110000
PORTb = InitStateofPort 'will unconditionally set bits 4:7
PORTb = 0b11110000 'will unconditionally set bits 4:7
PORTb = uservar OR 0b11110000 'will OR bits 4:7 to ensure bits 4:7 are setThe following is also valid - read a port.bit and then set port.bit with a variable or port value, as shown below.
dir PORTB out
PORTB.0 = NOT PORTB.0The user code above may cause issues with glitches when the read and write operations occur. Let us look at the generated assembler.
;portb.0 = NOT portb.0
banksel SYSTEMP1
clrf SysTemp1
btfsc PORTB,0
incf SysTemp1,F
comf SysTemp1,F
bcf PORTB,0
btfsc SysTemp1,0
bsf PORTB,0To resolve any glitches, add #option Volatile to your user code.
#option Volatile PORTB.0 ' <<< the #option Volatile directive resolving the read-modify-write glitch
dir PORTB out
PORTB.0 = NOT PORTB.0Key line: #option Volatile PORTB.0 — forces the compiler to generate a glitch-free read-modify-write sequence for bit 0 of PORTB (compare the two ASM listings
below), at the cost of slightly larger generated code.
This option produces the following assembler, resolving the glitch issue.
;portb.0 = NOT portb.0
banksel SYSTEMP1
clrf SysTemp1
btfsc PORTB,0
incf SysTemp1,F
comf SysTemp1,F
btfsc SysTemp1,0
bsf PORTB,0
btfss SysTemp1,0
bcf PORTB,0
See Also:
- Dir — setting a pin or port’s direction
- #Option Volatile — forcing glitch-free read-modify-write operations, as used above
- #Option NoLatch — disabling the automatic LATx substitution on writes

