CAD CAM EDM DRO - Yahoo Group Archive

Re: as simple as 1,2,3... ENCODER/JOG

on 2000-11-14 16:33:56 UTC
Hi Jack, Bill, et al,

Here is a snip of code, and pin defines for the Joystick Port. No
library required! Pardon the tab's, they come out funny in the email.
the switch bits are available on a simple port read (201h).

This is in Borland 'C' 4.5, same logic could even be done in BASIC! I
generate stepper pulses (Jog), you can display an increasing/decreasing
count on the P.C. screen (software counter).

Operation:

Connect encoder to 5v, A output to sw 1, B output to sw 2, and ground.

Put a knob on the encoder shaft, mark a reference point, and turn the
knob 360 degrees. count is steps (lines) per rev. Or do 10x turns, etc.


Extra credit: 1. Output step at EACH state change (4X).

2. Gate a software counter with edge of index signal connected
to another joystick switch pin.


Enjoy! (Jog frog Jog!)

Alan

/*-------------------------------------------------------------------------*/
#define GAMEPORT 0x201

/* states of encoder */
#define ADS1 1
#define ADS2 2
#define ADS3 3
#define ADS4 4

/* bits of joystick port */
#define ENQ_A1 0x01
#define ENQ_Q1 0x02
#define ENQ_A2 0x04
#define ENQ_Q2 0x08

void JoyFunction()
{
int JoyStatus;
static int LastJoyStatus = 0x80;
static int ad_state = ADS1;
bool aval, qval;


do
{
JoyStatus = GetJoySwitches(); /* check for encoder bit changes */

if(JoyStatus != LastJoyStatus) /* something moved */
{
aval = (bool)(JoyStatus & ENQ_A1);
qval = (bool)(JoyStatus & ENQ_Q1);

switch(ad_state)
{
case ADS1:
if( aval && !qval )
ad_state = ADS2;
else
if( !aval && qval )
ad_state = ADS4;
break;

case ADS2:
if( !aval && !qval )
ad_state = ADS3;
else
if( aval && qval )
ad_state = ADS1;
break;

case ADS3:
if( !aval && qval )
ad_state = ADS4;
else
if( aval && !qval )
ad_state = ADS2;
break;

case ADS4: /* take action in state 4 */
if( aval && qval )
{
ad_state = ADS1;
dir = FWD;
}
else
if( !aval && !qval )
{
ad_state = ADS3;
dir = REV;
}

RawStep(chan, dir); /* MOVE THE STEPPER */
Delay(nn); /* set step rate */
break;
}//switch ad_state

LastJoyStatus = JoyStatus;
}//JoyStatus != LastJoyStatus
}
while(NotDone);
}

/*------------------------------------------------------------------------*/
int GetJoySwitches()

/*
* Get Joystick switches
*
* 0x01 X
* 0x02 X
* 0x04 Y
* 0x08 Y
*
* 11/6/00 alm
*/


{

asm
{
mov dx, GAMEPORT // joystick port address 201h
in al, dx // read the switches
//
mov cl, 4 // shift high nibble down
shr al, cl //
xor al, 0x0F // invert
and ax, 0x0F // mask to switch values
}

return(_AX);
}

/*------------------------------------------------------------------------*/
// Joystick Pinout
// m1 f1 f2
//-----------------------------------------------------------
//1 : +5vDC 1
//2 : Stick 1 button 1 2
//3 : Stick 1 X-position 3
//4 : Gnd 4
//5 : Gnd 5
//6 : Stick 1 Y-position 6
//7 : Stick 1 button 2 7
//8 : +5vDC 8
//9 : +5vDC 9 1
//10 : Stick 2 button 1 10 2
//11 : Stick 2 X-position 11 3
//12 : Gnd 12 4 and 5 (ignore, may be for
MIDI)
//13 : Stick 2 Y-position 13 6
//14 : Stick 2 button 2 14 7
//15 : +5vDC 15 9 (ignore, may be for MIDI)

/*------------------------------------------------------------------------*/


jmw@... wrote:
>
> Allan, Bill, Jon et al, thanks very much for your suggestions re
> counting encoder lines. The gameport/PC method sounds most likely to
> work for me.
>
> I've written my share of Fortran and C in a VAX/VMS environment, some
> 8096 microcontroller assembler, and lots of SQL and AWK on the PC.
> But I've never talked to a PC game port. Do you gentlemen have a
> favorite PC product for doing this? I've got a PC AWK compiler which
> can use C subroutines; maybe there's a C gameport toolbox that would
> facilitate communication with the gameport? Or mabe some PC product
> has all this stuff built in?
>
> Thanks.
>
> --Jack
>
> Welcome to CAD_CAM_EDM_DRO@...,an unmoderated list for the discussion of shop built systems, for CAD, CAM, EDM, and DRO.
>
> Addresses:
> Post message: CAD_CAM_EDM_DRO@egroups.com
> Subscribe: CAD_CAM_EDM_DRO-subscribe@egroups.com
> Unsubscribe: CAD_CAM_EDM_DRO-unsubscribe@egroups.com
> List owner: CAD_CAM_EDM_DRO-owner@egroups.com, wanliker@...
> Moderator: jmelson@... [Moderator]
> URL to this page: http://www.egroups.com/group/CAD_CAM_EDM_DRO
> FAQ: http://www.ktmarketing.com/faq.html
> bill,
> List Manager

Discussion Thread

Alan Marconett KM6VV 2000-11-14 16:33:56 UTC Re: as simple as 1,2,3... ENCODER/JOG Alan Marconett KM6VV 2000-11-14 18:18:32 UTC re: as simple as 1,2,3... ENCODER/JOG Fred Smith 2000-11-15 06:21:22 UTC Re: re: as simple as 1,2,3... ENCODER/JOG