Arduinolibrary for 8-bit IO port
Weall know that, the arduino gives digital output using “digitalWrite()”function and gets digital input through “digitalRead()” function. Butusing these two functions, it can give output or get input from any one pin - meansat a time it can give output to or take input from single pin only. If anyonewants to interface any 8-bit IO device like 7-segment display, DIP switches orDAC (Digital to Analog convertor) with arduino, it can not give direct 8-bitdigital output to device but instead it has to give output to 8 different pins.The data byte (8-bit) pattern is sent to different pins in form of 1’s and 0’s.for example if data byte is 37h (0011 0111) one has to send these 0 n 1 patternto 8 different pins using digitalWrite() function.
So toovercome this situation here I present 8-bit IO Port (input-output port)library for arduino. Using this library one can send direct 8 bit data to anyarduino board pins configured as a combined 8-bit port or can get 8-bit inputfrom these configured pins. This library clubs any 8 pins of arduino togetherto work as 8-bit IO port. Just one has to select any 8 arduino pins to becombined as 8-bit port and it has to configure its data direction as input oroutput. Data direction is set by character ‘O’ for output or ‘I’ for input.
Thelibrary has only 5 functions. There are two constructors that will create portobject(s), one function to send 8-bit digital output to port pins, one functionto get 8-bit digital input from port pins and one additional function to alter/ set IO direction of port. Here all 5 functions are explained in brief andthere after some examples are given with explanations.
1. IO_Port_8bit(int pin1, intpin2, int pin3, int pin4, int pin5, int pin6, int pin7, int pin8, char dir)
This is constructor. It will createobject(s) of this class and thus it will create one or many 8-bit port(s) by combiningdistinct arduino pins. One has to specify 8 different arduino pins to becombined as port along with data direction as input or output - means portworks as either input or output. The last argument dir in this constructor definesport works as input or output. If dir='O' means port work as output and ifdir='I', port works as input. The same port cannot work as input and outputboth simultaneously or even alternatively. Also it indicates error if data directionis not selected
2. IO_Port_8bit(int pin1, intpin2, int pin3, int pin4, int pin5, int pin6, int pin7, int pin8)
Thisis another constructor. It will also create object(s) of this class and thus itwill create one or many 8-bit port(s) by combining distinct arduino pins. Onehas to specify 8 different arduino pins to be combined as port. But it does notspecify the data direction as input or output. After creating port object usingthis constructor, one has to set the port direction using set_IO_directionfunction. So this constructor allows programmer to alter port data direction inrun time using this constructor, the same port can work as input or outputalternatively (but not simultaneously)
3. set_IO_direction(char dir)
Thisfunction specifies the input/output direction of port. It has one characterargument that can be 'I' for port as input or 'O' for port as output. If datadirection is not selected, it displays error on serial monitor of arduino
4. send_8bit_data(int byt)
Thisfunction sends 8 bit data to specified pins. Just give int data (must be <255) as an argument that is directly given to 8 different pins. If data is>255 it displays error on serial monitor of arduino.
5. get_8bit_data(void)
Thisfunction gets 8 bit data from specified pins. It returns 8-bit int data byreading status of 8 different pins
Example1: blink 8 leds alternatively at a rate of 1 Hz
#include<IO_Port_8bit.h>
IO_Port_8bitmyport(2, 3, 4, 5, 6, 7, 8, 9, ’O’); //create output port
void setup() // nothing required in setup
{
}
void loop()
{
myport.send_8bit_data(85);// send data toblink all odd LEDs
delay(500);
myport.send_8bit_data(170); send data toblink all even LEDs
delay(500);
}
Example2: display binary counting pattern on LEDs from 0 to F
#include<IO_Port_8bit.h>
IO_Port_8bitmy8bitport(2, 3, 4, 5, 6, 7, 8, 9);//create port object
void setup()
{
my8bitport.set_IO_direction('O');// set portdirection
}
void loop()
{
int i;
for(i=0;i<16;i++) //send data 0 to 15 to display
{ // binary pattern
myport.send_8bit_data(i);
delay(200);
}
}
Example3: indicate analog input voltage level on 8-bit LED bar graph display
#include<IO_Port_8bit.h>
IO_Port_8bitmyport(2, 3, 4, 5, 6, 7, 8, 9, ’O’); //create output port object
void setup()
{
myport.send_8bit_data(255); //blink all LEDs of bar graph once
delay(500);
myport.send_8bit_data(0);
}
void loop()
{
int level;
level = analogRead(A0); //read analog input voltage
level = map(level, 0, 1023, 0, 80);// limit thevoltage from 0 to 80
//increase or decrease bar graph level asper input
if((level<80) && (level>70))myport.send_8bit_data(255);
elseif((level<=70) && (level>60)) myport.send_8bit_data(127);
elseif((level<=60) && (level>50)) myport.send_8bit_data(63);
elseif((level<=50) && (level>40)) myport.send_8bit_data(31);
elseif((level<=40) && (level>30)) myport.send_8bit_data(15);
elseif((level<=30) && (level>20)) myport.send_8bit_data(7);
elseif((level<=20) && (level>10))myport.send_8bit_data(3);
elseif((level<=10) && (level>0)) myport.send_8bit_data(1);
elseif(level==0) myport.send_8bit_data(0);
}
Example4: get 8-bit digital input from DIP switches and display value on serialmonitor
#include<IO_Port_8bit.h>
IO_Port_8bitmyport(2, 3, 4, 5, 6, 7, 8, 9); //create port object
void setup()
{
Serial.begin(9600); // initializeserial commu.
myport.set_IO_direction(‘I’); // set port direction
Serial.println("8-bit input porttest");
}
void loop()
{
int input_byt;
input_byt = myport.get_8bit_data(); // read the DIP switch status
Serial.print("received input = "); // and
Serial.println(input_byt); // display its value
delay(1000);
}
Comments