Snippet:Digital I/O

From Curuxa

The most basic and simple way for a the microcontroller in a Main Board to communicate with Modules is by using digital inputs and outputs.

Contents

Digital I/O setup on all Main Boards but MBP8

Digital pins are set as inputs or outputs using the TRISx registers/variables.

For more information, read section "I/O Ports" on the datasheet of the microcontroller used in your Main Board.

Traditional, default way

In the TRISx registers/variables, bits set as "1" define input pins, and bits set as "0" define output pins on the microcontroller.

#include <pic16fxxx.h>
 
// other stuff...
 
void main() {
    //configure all bits/pins of a digital I/O port as inputs. Remember: ''0xFF'' in hexadecimal is the same as ''0b11111111'' in binary
    TRISA=0xFF; //set all PORTA bits as inputs
    TRISB=0xFF; //set all PORTB bits as inputs
 
    //configure all bits/pins of a digital I/O port as outputs. Remember: ''0x00'' in hexadecimal is the same as ''0b00000000'' in binary
    TRISC=0x00; //set all PORTC bits as outputs
    TRISD=0x00; //set all PORTD bits as outputs
 
    //configure a single digital I/O as an input
    TRISB3=1; //set RB3 as intput
 
    //configure a single digital I/O as an output
    TRISD1=0; //set RD1 as output
 
    // the rest of your program...
}


Recommended way

Instead of working with zeroes and ones all the time, it's recommended to use some macro definitions to make your life easier.

#include <pic16fxxx.h>
 
#define DigitalInput 1
#define DigitalOutput 0
#define AllDigitalInputs 0xFF
#define AllDigitalOutputs 0x00
 
// other stuff...
 
void main() {
    //configure all bits/pins of a digital I/O port as inputs
    TRISA=AllDigitalInputs; //set all PORTA bits as inputs
    TRISB=AllDigitalInputs; //set all PORTB bits as inputs
 
    //configure all bits/pins of a digital I/O port as outputs
    TRISC=AllDigitalOutputs; //set all PORTC bits as outputs
    TRISD=AllDigitalOutputs; //set all PORTD bits as outputs
 
    //configure a single digital I/O as an input
    TRISB3=DigitalInput; //set RB3 as intput
 
    //configure a single digital I/O as an output
    TRISD1=DigitalOutput; //set RD1 as output
 
    // the rest of your program...
}


Digital I/O setup on MBP8

Digital pins in MBP8 are set as inputs or outputs using the TRISIO register/variable.

For more information about digital I/O, read the section "GPIO Port" on the PIC12F683 datasheet, the microcontroller used in MBP8.

Traditional, default way

In the TRISx registers/variables, bits set as "1" define input pins, and bits set as "0" define output pins on the microcontroller.

#include <pic12f683.h>
 
// other stuff...
 
void main() {
    //set all digital pins as inputs. Remember: ''0xFF'' in hexadecimal is the same as ''0b11111111'' in binary
    TRISIO=0xFF;
 
    // or...
 
    //set all digital pins as outputs. Remember: ''0x00'' in hexadecimal is the same as ''0b00000000'' in binary
    TRISIO=0x00;
}


Recommended way

Instead of working with zeroes and ones all the time, it's recommended to use some macro definitions to make your life easier.

#include <pic12f683.h>
 
#define DigitalInput 1
#define DigitalOutput 0
#define AllDigitalInputs 0xFF
#define AllDigitalOutputs 0x00
#define SetBit(v,bit) v |= (1 << bit);
#define ClearBit(v,bit) v &= ~(1 << bit);
#define SetDigitalInput(pin) SetBit(TRISIO,pin)
#define SetDigitalOutput(pin) ClearBit(TRISIO,pin)
 
// other stuff...
 
void main() {
    //set all digital pins as inputs
    TRISIO=AllDigitalInputs;
 
    //set all digital pins as outputs
    TRISIO=AllDigitalOutputs;
 
    //set a single digital pin as input
    SetDigitalInput(5); //set GP5 (microcontroller pin number 2) as input
 
    //set a single digital pin as output
    SetDigitalOutput(5); //set GP5 (microcontroller pin number 2) as output
}


Using digital intputs/outputs

For managing digital pins on the microcontroller we can use the PORTx (PORTA, PORTB...) variables for the entire port (see datasheet) or Rxx (RA3, RB0, RC3, RD7...) for individual pins.

Remember to setup them as digital inputs before reading them, or as digital outputs before writing a value to them.

The following examples show how to turn on a LED (LTIND-A) when we press a button (SISW-SPST), and to turn it off the rest of the time. SISW-SPST is connected to RB3, and LTIND-A to RB6.

Traditional, default way

In the PORTx and Rxx registers/variables, bits set as "1" mean voltage close to 5V, and bits set as "0" mean voltage close to 0V.

#include <pic16fxxx.h>
 
// other stuff...
 
void main() {
    TRISB3=1; //set RB3 as intput
    TRISB6=0; //set RB6 as output
 
    //start infinite loop
    while(1) {
        if(RB3 == 0) RB6 = 1; //set RB6 to 5V when RB3 is close to 0V
        else RB6 = 0; //set RB6 to 0V when RB3 is close to 5V
    }
}

Recommended way

Let's use some macro definitions to make the code easier to write and read.

#include <pic16fxxx.h>
 
#define DigitalInput 1
#define DigitalOutput 0
 
#define Button RB3
#define Pressed 1
#define Released 0
#define LED RB6
#define ON 1
#define OFF 0
 
// other stuff...
 
void main() {
    TRISB3=DigitalInput; //set RB3 as intput
    TRISB6=DigitalOutput; //set RB6 as output
 
    //start infinite loop
    while(1) {
        if(Button == Pressed) LED = ON; //turn on LED when pressing button
        else LED = OFF; //turn off LED when button is released
    }
}


On MBP8 we would use TRISIO instead of TRISx, GPIO instead of PORTx, and GPx instead of Rxx.