1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdio.h>
#include "portio32.h"
#define BASE 384
int main()
{
int result; //holds the error or success codes from calls to functions from PortIO32
int value; //holds the value read from Port B (Port 2)
int count=0; //holds how many times the pushbutton has been pressed
char choice;
//First, we will output a notice and ask for user confirmation
//this would not normally be done in an actual application.
printf("Note: This program configures Port 1 as an output port. Please be sure that you do not have interfering hardware connected to port 1 of your card.\n");
printf("Also note that this program assumes your card's base address is %d. If that is incorrect, please edit the definition of BASE in the source code.\nContinue (Y/N)?", BASE);
choice = getchar();
if( (choice != 'y') && (choice != 'Y') )
return(0);
// Note that in this application, the error codes are checked on most function calls.
// In a normal application, it would not be necessary to
// check for errors on all of the functions, but it is done here
// to show that it is possible.
//configure the card. Port A (1) should be an output and Port B (2) and C (3) will be inputs
result=OutByte(BASE + 3, 139); //139 is the proper control byte for this situation
if(result == -1)//error occurred
{
printf("An error occurred while writing a value to a port.\n");
return(-1);
}
printf("Waiting for button to be pressed. To exit, press Ctrl-C\n");
fflush(stdout);//fflush would not normally be used, but it is in this case because of the continuous loop
while(1)//loop to monitor the state of the pushbutton
{ //Note that this loop has no delays, so it uses up
//all of the CPU time that it can. This is done to
//keep the sample code simple. In a real program,
//it is recommended that you implement some kind
//of sleep or timer mechanism.
result = InByte(BASE + 1, &value);//read a value from Port B
if(result == -1)
{
printf("An error occurred while reading a value from a port.\n");
return(-1);
}
//in this sample, we will assume that when the button is pressed,
//it results in Bit 0 being low (0)
if( !(value & 1) )//test whether bit 0 (which has a binary weight of 1) is off
{//the code below will execute if bit 0 was off (button pressed)
count++;//increment counter
printf("Count: %d\n", count);
fflush(stdout);
//output the new count to the LEDs on port A
result=OutByte(BASE, count);
if(result == -1)//function failed
{
printf("An error occurred while writing a value.\n");
return(-1);
}
//Now we will wait until bit 0 goes back to being on (wait til the pushbutton is released)
//so, we will loop while bit 0 is off
while( !(value & 1) )
{
InByte(BASE + 1, &value); //note that this could be (note the R) value=InByteR(BASE + 1);
}//end of inner while loop
}//end of if (test for button pressed)
}//end of while loop
return(0);
}//end of main function |
Partager