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
 *
 * Compile: bcc32 -tWM par.cpp rulbusdcl-omf-static.lib winutils-omf-static.lib
 */

#include <conio.h>              // for kbhit(), getch()
#include <iostream>             // for std::cout
#include <windows.h>            // for Sleep()

#include "rdcl/rb8506_pport.h"  // for class RB8506_Pia etc.

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

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( int  byte, int pin ) {  return ( byte &   mask(pin) ) != 0; }
inline int  clrbit( int& byte, int pin ) {  return   byte &= ~mask(pin); }
inline int  setbit( int& byte, int pin ) {  return   byte |=  mask(pin); }
inline int  xorbit( int& byte, int pin ) {  return   byte ^=  mask(pin); }

static void setPortDirection( Rulbus::RB8506_PiaViaRef port );
static void setLineDirection( Rulbus::RB8506_PiaViaRef port );
static void switchLED_mask  ( Rulbus::RB8506_PiaViaRef port );
static void switchLED_line  ( Rulbus::RB8506_PiaViaRef port );

/*
 * the program
 */

int main()
{
   std::cout << "Switch a LED on and off, a parallel interface demonstration.";

   Rulbus::RB8506_Pia pia( "lamp-control", address, rack );

   // using port functions:

   std::cout << "\nsetting port direction..." ;
   setPortDirection( pia );

   std::cout << "\nusing port data with bitmasks; press a key to contine...";
   switchLED_mask( pia );

   // using line functions:

   std::cout << "\nsetting line direction..." ;
   setLineDirection( pia );

   std::cout << "\nusing single line functions; press a key to contine...";
   switchLED_line( pia );

   std::cout << std::endl;
}

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

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

static void setPortDirection( Rulbus::RB8506_PiaViaRef port )
{
   int dataSwitch = port.getPortDir( portSwitch );
   int dataLED    = port.getPortDir( portLED    );

   port.setPortDir( portSwitch, dataSwitch & ~mask(pinSwitch) );
   port.setPortDir( portLED   , dataLED    |  mask(pinLED   ) );
}

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

static void switchLED_mask( Rulbus::RB8506_PiaViaRef port )
{
   while ( !kbhit() )
   {
      int dataSwitch = port.getPortData( portSwitch );
      int dataLED    = port.getPortData( portLED    );

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

      port.setPortData( portLED, dataLED );

      Sleep( 10 );
   }

   (void) getch();
}

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

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

static void setLineDirection( Rulbus::RB8506_PiaViaRef port )
{
   port.setLineDir( lineSwitch, 'i' );
   port.setLineDir( lineLED   , 'o' );
}

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

static void switchLED_line( Rulbus::RB8506_PiaViaRef port )
{
   while ( !kbhit() )
   {
      int switchOpen = port.getLineLevel( lineSwitch );
                       port.setLineLevel( lineLED, switchOpen );

      Sleep( 10 );
   }

   (void) getch();
}


Generated on Tue Oct 12 14:11:57 2004 for Rulbus Device Class Library for Microsoft Windows by doxygen 1.3.4