/*The prupose of this simple program is to demonstrate
the use of a 8255 printer port interface card in connection
with an opto-relay card*/
/*In this program, Port A is set to input port while
Port B and Port C are set to output ports*/

#include "stdio.h"
#include "dos.h"
#include "conio.h"
#define print_address 0x378
#define controlword 0x90
/*Port A as input while Port B and C as output */
int *inputbyte=0;
void init_printer(void);
/*printer port initialisation*/
void outputbyte(int out_byte);
/*output data to Port B*/
void readbyte(void);
/*read in and convert data from Port A*/
void bitconvert(int *hbyte,int *lbyte);
/*conversion of read-in data */

void main()
{
int outbyte=0;
init_printer(); /*initialise the printer port*/
while (outbyte>=0)
{printf("\nPlease input the byte to be sent to Port B or a -ve vlaue to exit : " );
scanf("%d", &outbyte);
if (outbyte<0)
break;
outputbyte(outbyte); /*output control byte to 8255*/
printf("\nThe output byte to Port B is : %d",outbyte);
readbyte();
/*read in and convert data from Port A*/
printf("\nThe actual input byte is %d", *inputbyte);
}
printf("\nProgram ends. Thank you!");
}

void init_printer(void)
/*printer initialisation*/
{
outportb(print_address,controlword);
delay(10);
outportb(print_address+2,4);
delay(10);
outport(print_address+2,6);
delay(10);
outportb(print_address+2,4);
delay(10);
}

void outputbyte(int out_byte)
/*output data to Port B*/
{
outportb(print_address,out_byte);
outportb(print_address+2, 12);
delay(20);
outportb(print_address+2, 14);
delay(20);
outportb(print_address+2, 12);
delay(20);
}

void readbyte(void)
/*read in and convert data from Port A*/
{
int *highbyte,*lowbyte;
outportb(print_address,0);
outportb(print_address+2,8);
delay(50);
outportb(print_address+2,9);
*lowbyte=inportb(print_address+1);
outportb(print_address,1);
*highbyte=inportb(print_address+1);
outportb(print_address+2,8);
printf("\nIn Port A, the input highbyte is %d and the input lowbyte is %d", *highbyte, *lowbyte);
bitconvert(highbyte,lowbyte);
}

void bitconvert(int *hbyte,int *lbyte)
/*conversion of read in data */
{
*hbyte&128;
*hbyte=*hbyte<<1;/*conversion of high byte data*/
*lbyte&128;
*lbyte=*lbyte>>3;/*conversion of low byte data*/
*inputbyte=*lbyte+*hbyte;/*add both high byte and low byte data*/
}