16
12.4
Example program using C language
// This example use quadrature mode
// to count the input pulse in X axis and
// update the counter reading to screen
#include <dos.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#define UPDATE_CNTR 3 //address of UPDATE COUNTER BUFFER
#define READ_H_WORD 4 // address of COUNTER HIGH WORD REGISTER
#define READ_L_WORD 5 // address of COUNTER LOW WORD REGISTER
#define PRELOAD_H_W 6 //address of PRELOAD HIGH WORD TO BUFFER
#define PRELOAD_L_W 7 //address of PRELOAD LOW WORD TO BUFFER
#define PRELOAD_CMD 8 //address of LOAD PRESET VALUE TO COUNTER
#define BASE 0x280 //base I/O address = 280H
void main() // program start
{
int Cntr_low_value; //declaration of Cntr_low_value as interger
int Cntr_hi_value; // declaration of Cntr_hi_value as interger
long Cntr_value; // declaration of Cntr_value as long
clrscr(); // clear screen
printf("Please set base address = 280h\n"); //Request to set Dip switch to 280h
getch(); //wait for keyin
printf("Press any key to preset counter value \n");
printf("set counter = 6476936 \n");
getch(); // wait for keyin
outpw( BASE + 0 , PRELOAD_H_W ) ; //set index to PRELOAD_H_W
outpw( BASE + 2 , 0x0062 ) ; //write data
// 6476936 D = 62D488 H
outpw( BASE + 0 , PRELOAD_L_W ) ; //set index to PRELOAD_L_W
outpw( BASE + 2 , 0xD488 ) ; //write data
outpw( BASE + 0 , PRELOAD_CMD); //set index to PRELOAD_CMD
outpw( BASE + 2 , 0X0001); //load data
// the following are read COUNTER data and display
clrscr();
printf("X-CNTR : 00000000 \n");
printf("Press any key to stop program ! ");
_setcursortype(_NOCURSOR);
while ( ! kbhit() ) // if no key stroke read counter data
{
outpw( BASE + 0 , UPDATE_CNTR); //set index to UPDATE_CNTR
outpw( BASE + 2 , 0X0001); //command to latch
outpw( BASE + 0 , READ_L_WORD ) ; //set index to READ_L_WORD
Cntr_low_value = inpw( BASE + 2 ); //read data
outpw( BASE + 0 , READ_H_WORD ) ; // set index to READ_H_WORD
Cntr_hi_value = inpw( BASE + 2 ); // read data
Cntr_value = Cntr_hi_value * 65536 + Cntr_low_value; //caculate to result
gotoxy(14,1);cprintf("%08lu",Cntr_value);// print to screen
}
} // program end