In this project, we’ll design a 16-bit IO port (input-output port) library for Arduino. It’s possible to send direct 16-bit data to any Arduino board using this library. It connects any of Arduino’s 16 pins, so they work as a 16-bit IO port.
First, you must select 16 pins from Arduino to combine as the 16-bit port and configure the data direction as input or output. The data direction is set by the character ‘O’ for output and ‘I’ for input.
The library has five functions, as explained below. Two constructors create the port object(s). One object sends 16-bit digital output to the port pins, and another receives the 16-bit digital input from the port pins. One additional object alters or sets the port’s IO direction.
The library’s functions
This library can interface any 16-bit device, such as ADC, DAC, MUX, or any other digital device, to receive 16-bit input or send 16-bit output.
Here are the library’s five functions:
Here are the library’s five functions:
1. IO_Port_16bit(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8,int pin9,int pin10,int pin11,int pin12,int pin13,int pin14,int pin15,int pin16, char dir)
This is the constructor. It creates the object(s) of this class and one or many 16-bit port(s) by combining specific Arduino pins. You must specify which Arduino pins to combine for the port and the data direction (input or output). The constructor’s last argument (“dir”) defines whether the port works as input or output.
- If dir=’O,’ it means the port works as output
- If dir=’I’, port works as input.
The same port cannot work as input and output, whether simultaneously or alternatively. If the data direction is not selected properly, you’ll see an error message.
2. IO_Port_16bit(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8, int pin9, int pin10,int pin11,int pin12,int pin13,int pin14,int pin15,int pin16)
This is another constructor. It also creates the object(s) of this class and one or many 16-bit port(s) by combining specific Arduino pins. You must specify which Arduino pins to combine for the port. However, this constructor does not indicate the data direction as input or output. After creating the port object, you must set the port direction using the set_IO_direction function. This constructor lets a programmer alter the port data direction in run time, and the same port can work as input or output alternatively (but not simultaneously).
3. set_IO_direction(char dir)
This function specifies the input or output direction of a port. It has one character argument, which is ‘I’ for the input port and ‘O’ for the output port. If the data direction is not selected, it displays an error message on Arduino’s Serial Monitor.
4. send_16bit_data(unsigned int byt)
This function sends 16-bit data to the specified pins. It gives “int” data (must be < 65535) as an argument directly to Arduino’s 16 pins. If the data is > 65535, Arduino’s Serial Monitor displays an error message.
5. get_16bit_data(void)
This function receives 16-bit data from the specified pins. It returns 16-bit unsigned “int” data by reading the status of Arduino’s 16 pins
Example 1: Taking the 16-bit input and display it on Arduino’s serial monitor
#include <IO_Port_16bit.h>
IO_Port_16bit my16bitport(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
int i;
long input_double_byte;
void setup()
{
// put your setup code here, to run once:
my16bitport.set_IO_direction(‘I’);
Serial.begin(9600);
}
void loop()
{
input_double_byte = my16bitport.get_16bit_data();
Serial.print(“input data: “);
Serial.println(input_double_byte);
delay(1000);
}
Example 2: The 16 LED chaser program
#include <IO_Port_16bit.h>
IO_Port_16bit my16bitport(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
void setup()
{
// declare port direction as output specifying ‘O’
my16bitport.set_IO_direction(‘O’);
}
void loop()
{
unsigned int i;
for(i=1;i<65535;i*=2)
{
my16bitport.send_16bit_data(i); // send data as 2,4,8,16
delay(200); // 32,..,..,…65534
}
}
You may also like:
Filed Under: Arduino Projects, Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.