pport.cpp

This is an example that shows how the RB8506 dual parallel interface (PIA/VIA) may be used.

/*
 * pport.cpp - switch a LED on and off, a parallel interface demonstration.
 *
 * The switch is connected to PB3 of parallel interface, Pia 2,
 * the LED is connected to PB0.
 *
 * The program continuously reads the switch and puts the LED on
 * when the switch is closed and puts the LED off when the switch
 * is open. Note the inverted logic of the switch (pull-up) as
 * well as of the LED (pulled down).
 *
 *  +5V           parallel interface           +5V
 *   +             +------------+               +
 *   |             |    pia2    |   \\          |
 *   +---===---+---| PB3    PB0 |---|<|---===---+
 *        /    |   |            |
 *   +---/  ---+   +------------+
 *   | switch  LED
 *  ---
 *
 * The program uses two methods:
 * 1. mask: read and set the i/o-pins by reading and writing port B
 * 2. line: read and set the i/o-pins by reading and wrting a single line
 *
 * The Rulbus configuration file must define the port as "pport" at address 0x94:
 *
 * rack "a rack" {
 *    rb8506_pia "pport" {
 *       address = 0x94;
 *    }
 * }
 *
 * Compile: bcc32 par.cpp rulbus.lib
 */

#include "rulbus.h"
#include <stdio.h>      // for printf()
#include <conio.h>      // for kbhit(), getch()
#include <windows.h>    // for Sleep()

enum  Pin { pin0, pin1, pin2, pin3, pin4, pin5, pin6, pin7, };

const char* portName   = "pport"; // the name in rulbus.conf

const Pin   pinLED     = pin0;    // the LED is at PB0
const Pin   pinSwitch  = pin3;    // the switch is at PB4

const char* portLED    = "PB";    // for port functions
const char* portSwitch = "PB";

const char* lineLED    = "PB0";   // for line functions
const char* lineSwitch = "PB3";

const int   rack       = 0;
const int   address    = 0x94;    // this is the parallel interface, Pia 2

inline int  mask( int pin ) {  return 1 << pin; }

inline int  tstbit( int32  byte, int pin ) {  return ( byte &   mask(pin) ) != 0; }
inline int  clrbit( int32& byte, int pin ) {  return   byte &= ~mask(pin); }
inline int  setbit( int32& byte, int pin ) {  return   byte |=  mask(pin); }
inline int  xorbit( int32& byte, int pin ) {  return   byte ^=  mask(pin); }

static void setPortDirection( int handle );
static void setLineDirection( int handle );
static void switchLED_mask  ( int handle );
static void switchLED_line  ( int handle );

/*
 * the program
 */

int main()
{
   printf( "Switch a LED on and off, a parallel interface demonstration." );

   int32 handle;
   rb8506_pport_open( &handle, portName );

   // using port functions:

   printf( "\nsetting port direction..." );
   setPortDirection( handle );

   printf( "\nusing port data with bitmasks; press a key to contine..." );
   switchLED_mask( handle );

   // using line functions:

   printf( "\nsetting line direction..." );
   setLineDirection( handle );

   printf( "\nusing single line functions; press a key to contine..." );
   switchLED_line( handle );

   printf( "\n" );

   return rb8506_pport_close( handle );
}

//--- solution using port-i/o with bitmasks

/*
 * Set data direction, using bitmasks.
 */

static void setPortDirection(int handle)
{
   int32 dataSwitch, dataLED;
   rb8506_pport_getPortDir( handle, portSwitch, &dataSwitch );
   rb8506_pport_getPortDir( handle, portLED   , &dataLED    );

   rb8506_pport_setPortDir( handle, portSwitch, dataSwitch & ~mask(pinSwitch) );
   rb8506_pport_setPortDir( handle, portLED   , dataLED    |  mask(pinLED   ) );
}

/*
 * Read the switch and control the LED using bit-masks.
 */

static void switchLED_mask(int handle)
{
   while ( !kbhit() )
   {
      int32 dataSwitch, dataLED;
      rb8506_pport_getPortData( handle, portSwitch, &dataSwitch );
      rb8506_pport_getPortData( handle, portLED   , &dataLED    );

      if ( tstbit( dataSwitch, pinSwitch ) ) setbit( dataLED, pinLED );
      else                                   clrbit( dataLED, pinLED );

      rb8506_pport_setPortData( handle, portLED, dataLED );

      Sleep( 10 );
   }

   (void) getch();
}

//--- solution using line-i/o

/*
 * Set data direction, using line-functions.
 */

static void setLineDirection(int handle)
{
   rb8506_pport_setLineDir( handle, lineSwitch, 'i' );
   rb8506_pport_setLineDir( handle, lineLED   , 'o' );
}

/*
 * Read the switch and control the LED using single line i/o-functions.
 */

static void switchLED_line(int handle)
{
   while ( !kbhit() )
   {
      int32 switchOpen;

      rb8506_pport_getLineLevel( handle, lineSwitch, &switchOpen );
      rb8506_pport_setLineLevel( handle, lineLED   ,  switchOpen );

      Sleep( 10 );
   }

   (void) getch();
}


Generated on Wed Apr 6 08:59:18 2005 for Rulbus Device Library for Microsoft Windows by doxygen 1.4.0