DPRG List  

DPRG: Re: Vector 2x sample code reply

Subject: DPRG: Re: Vector 2x sample code reply
From: Eric B. Olsen robojoc at worldnet.att.net
Date: Sun Jul 13 23:25:01 CDT 1997

This is a multi-part message in MIME format.

- --------------76D666BE1D7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Terry P. Gathright wrote:
> Hi Eric,
>  I have just finished hooking up my vector2x compass and it seems to
> work fine thanks to Tom Brusehaver's good work and instructions.The only
> goof  I made was to reverse the input and output lines. After about two
> hours of inspection and probing with a logic probe I realized the
> problem and  corrected it.
>   Do you have any sample code for using the v2x compass on a small robot
> with two drive motors and wheels? I don't have a clue about writing code
> but can follow directions to the tee if there is nothing left out.
>   I've tried to load other programs while the compass was running and
> crashed the handyboard. I have comp_disable(); and comp_init();
> functions but a working code to study would be helpfull to me to
> understand code.
>    I am working to build a computer controlled lawnmower in the future.
>                                                 Thanks in advance
>                                                 Terry Gathright
>                                                 6412 Leslie Street
>                                                 Metairie, La. 70003
>                                                 669 at worldnet.att.net
> I bought a v2x compass from Jamco, wired to my handyboard

Hi Terry,

I don't know if I can help you.  Do you want application code, or just
interface code?  If you want a sample of interface code, I'd first
qualify by saying that any such 'code solutions' are dependent on your
interface specifics.  Also, it's important to know if your Vector will
run in "Master Mode" or "Slave Mode".  I stated in my article that Slave
mode may be easier for direct control of a CPU as it may require the
least dedicated hardware.  Refer to the Vector documents for each
particular mode.  Also, I am not familiar with "handyboard", but I'll
assume it's a prototyping breadboard (!?).

I have included a very specific example of code used in GeneSys-1 at
this point.  It's working, but I will be improving on it later.  It is
written in C, so you will need to brush up a bit in this area.  I
haven't written any "flowcharts", or I would include this instead.

I've attached the sample code as a text based "attachment".  Hopefully,
you'll get it!

P.S.  If you're looking for application code, sky's the limit.  While my
robot can 'read' the compass ... actually using it will have to wait for
the final code development for which I have yet to finish.  Good Luck!

Eric B. Olsen
Las Vegas, NV

robojoc at worldnet.att.net

PSPS  You can contact Vector at http://www.pcweb.com/pni

- --------------76D666BE1D7
Content-Type: text/plain; charset=us-ascii; name="Vector.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Vector.c"


#define COMPASS         ((uchar *)(0x01C004))   /* used to read compass data */
#define OUT0            ((uchar *)(0x01C000))   /* used to control compass, I/O write */

		 /* Compass function proto-types and globals.... */

void init_compass(void);                /* used to initialize vector compass */
int get_heading(void);                  /* used to read compass */

xdata int heading;
xdata uchar compass_read;


void main()

/*  all of your normal initialization stuff here! .... */


	 printf("Testing compass\n");

	 while(1) {
		 printf("heading is %i\n", get_heading());

}                               /* End of Main */


/* The init_compass() routine assumes that: 

		bit 0 of OUT0 = RES		(Vector's RESOLUTION signal)
		bit 1 of OUT0 = P/C		(Vector's P/C "convert" signal)
		bit 2 of OUT0 = RESET		(Vector's RESET signal)
		bit 3 of OUT0 = CAL		(Vector's CALIBRATE signal)

and that,

		The input port "COMPASS" is used to read the low order 
		data byte of the vector compass serial stream when OUT0 bit 4 = 0,
		and reads the high byte of the Vector serial stream when OUT0 bit 4 = 1.

		(A dedicated hardware circuit has been used to act as a serial to
		 parallel converter ... this circuit also generates a hardware interrupt
		 when the shift process is done)

                This routine also assumes the Vector is placed in Master mode by
                tying the M/S pin low, the X-Flip and Y-Flip pins are unconnected, 
                and the SS pin is left unconnected.


void init_compass(void)

	 *OUT0 = 0xFF;               /* set all outputs high */

	 set_timer1(100);	     /* delay for 100 milliseconds

	 *OUT0 = 0xFF & ~0x04;       /* clear RESET by setting RESET = 0 */

	 set_timer1(20);             /* delay for 20 ms */

	 *OUT0 = 0xFF;               /* take reset high again */

	 set_timer1(600);            /* wait for 600 ms */

	 *OUT0 = 0xFE;               /* place RES low for faster sampling */

	 set_timer1(100);            /* delay for 100 ms */

	 IE0 = 0;                    /* clear any pending int 0's (of the 8032 CPU) */
	 EX0 = 1;                    /* enable external interrupt 0 (of the 8032 CPU) */
	 heading = 0;                /* init compass heading to zero (clear application variable) */

/********  The get_heading() routine returns the direction of the Vector compass
           in binary, ranging from 0 to 359 degrees.

           This routine is functional, but not an efficient version for 
           interrupt controlled operation.  However, it can be modified for
           Master Mode operation using "polling" (no interrupts), and it 
           might also be modified to operate in Slave mode ... by providing
           the neccesary clocking and alteration of control signals.

int get_heading(void)
xdata uchar value;

	 compass_read = FALSE;

	 *OUT0 = 0xFE & ~0x02;               /* place P/C low for 10 ms */

	 set_timer1(10);                     /* delay for 10 milliseconds

	 *OUT0 = 0xFE;                       /* place P/C back high */

	 set_timer1(100);		     /* the interrupt should fire, or timeout */
	 while((!compass_read) && (!timeout1));

	 if(timeout1) {
	 else {
	    *OUT0 = 0x0E;               	/* select low compass byte */

	    value = *COMPASS;			/* read low byte and store in 'value' */

	    heading = (value & 0xF0)/16 * 10 + (value & 0x0F);  /* convert two BCD digits to binary and store in heading */

	    *OUT0 = 0xFE;               	/* select high compass byte */

	    heading += (*COMPASS) * 100;	/* convert and add high BCD byte to heading */

	    return(heading);			/* return result from 0 to 359


/****  This is the GeneSys-1 interrupt routine handler for the Vector compass  ***/

/*  This interrupt routine assumes that it is called after all data bits from the 
    Vector have been shifted out (Master mode).  For this, a circuit is used that 
    counts the clock output from the vector, and when it reaches 16 pulses, asserts 
    an interrupt signal to the 8032 type processor.  

interrupt [0x03] void external_0(void)    /* Int-0 external interrupt; */

	 compass_read = TRUE;	/* Vector is done shifting all data bits, set flag */


- --------------76D666BE1D7--


More information about the DPRG mailing list