Interfacing 8255 with Parallel Port - C Code

/*
** 8255.c
**
** Illustrates how 8255 may be configured for outputs on 8255 Ports
** A, B and C.
**
** 8255 is first setup with control word 0x80
**   Mode set flag active - bit 7 = 1
**	Mode selection 0 - bits 6 5 = 0 0
**   Port A output - bit 4 = 0
**   Port C (upper) output - bit 3 = 0
**   Mode selection 0 - bit 2 = 0
**   Port B output - bit 1 = 0
**   Port C (lower) output - bit 0 = 0
**
** Data may then be output by calling out_data (port, data) where
**   Port A - 0
**   Port B - 1
**   Port C - 2
**
** P. H. Anderson, Morgan State Univeristy, June 20, '96
**
*/

#include <stdio.h>
#include <dos.h>

#define DATA 0x03bc  /* set for your machine */
#define STATUS DATA+1
#define CONTROL DATA+2

void reset(void);
void write_clock(int a1a0);
void set_control_word(void);
void out_data(int port, int data);

void main(void)
{
   int port;
   reset();
   set_control_word();
   /* now we are ready to go */

   /* each port exercised with 0x00, 0xaa, 0x55 and 0xff */
   for (port = 0; port<3; port++)
   {
	 out_data(port, 0x00);
	 delay(1000);
	 out_data(port, 0xaa);
	 delay(1000);
	 out_data(port, 0x55);
	 delay(1000);
	 out_data(port, 0xff);
	 delay(1000);
   }
}

void set_control_word(void)
{
   int a1a0 = 0x03;
   outportb(DATA, 0x80); /* out control word on data leads */

   /* bring a1 a0 to 1 1, WR to 1 */
   outportb(CONTROL,
       ( ((0x00) | (a1a0 << 1) | 0x01) ^ 0x0b)  & 0x0f); 

   /* now wink WR low and high */
   write_clock(a1a0);
}

void out_data(int port, int data)
{
   int a1a0 = port;
   outportb(DATA, data); /* put data on data leads */

   /* set a1 and a0 to 0 0, 0 1 or 1 0, WR to 1 */
   outportb(CONTROL, 
       ( ((0x00) | (a1a0 << 1) | 0x01) ^ 0x0b) & 0x0f);

   /* now wink WR low and high */
   write_clock(a1a0);
}

void write_clock(int a1a0)
{
   /* bring WR low */
   outportb(CONTROL, 
	(((0x00) | (a1a0<<1) & (~0x01) ) ^ 0x0b) & 0x0f);

   /* bring WR back high */
   outportb(CONTROL, 
 	(( (0x00) | (a1a0<<1) | (0x01)) ^ 0x0b) & 0x0f);
}

void reset(void)
{
   /* bring reset high */
   outportb(CONTROL, 0x08^0x0b);

}