/*------------------------------------------------------*/
/*       Analog to Digital / Digital to Analog          */
/*                                                      */
/*                      A2D-D2A.C                       */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           2.1                    */
/*                Date:          4/21/00                */
/*                                                      */
/*                                                      */
/* Routines:                                            */
/*     a2d_setup()                                      */
/*     d2a_setup()                                      */
/*     a2d()                                            */
/*     d2a()                                            */
/*______________________________________________________*/

unsigned int    A2D[8];
unsigned int    D2A_VALUE[4];
unsigned int    D2A_ADDR[4];
unsigned int    D2A_LDAC;



/*------------------------------------------------------*/
/*                   a2d_setup Routine                  */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.1                    */
/*                Date:          2/5/00                 */
/*                                                      */
/*                                                      */
/* Function: When called, the routine prepares the      */
/*           68HC11's A to D converter                  */
/*______________________________________________________*/



a2d_setup()
{
        bit_set (OPTION,0x80);                  /* enable+power up a2d converter          */
        bit_clr (OPTION,0x40);                  /* select 68HC11's clock                  */        
}


/*------------------------------------------------------*/
/*                   d2a_setup Routine                  */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.1                    */
/*                Date:          2/5/00                 */
/*                                                      */
/*                                                      */
/* Function: When called, the routine prepares the      */
/*           Expanded I/O Cards D to A converter        */
/*______________________________________________________*/


d2a_setup()
{

        D2A_ADDR[0] = 0xb584;
        D2A_ADDR[1] = 0xb585;
        D2A_ADDR[2] = 0xb586;
        D2A_ADDR[3] = 0xb587;
        D2A_LDAC = 0xb588;

        D2A_VALUE[0] = 0;
        D2A_VALUE[1] = 0;
        D2A_VALUE[2] = 0;
        D2A_VALUE[3] = 0;

        d2a();

        
}

/*------------------------------------------------------*/
/*              Analog to Digital Conversion            */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           3.0                    */
/*                Date:          1/28/2000              */
/*                                                      */
/*  Name:      a2d()                                    */
/*                                                      */
/*  Function:  a/d conversion on PE0 to PE7 (storing in */
/*             A2D[0] -> A2D[7] respectively)           */
/*                                                      */
/*  Modifies:  ADR1, ADR2, ADR3, ADR4,A2D[0..7]         */
/*______________________________________________________*/


a2d()
{
        d_int();                                /* disable interrupts - crash if not done */
        pokeb (ADCTL,0x10);                     /* start a2d conversion on PE0 -> 3       */
        while ((peekb(ADCTL) & 0x80)) ;         /* wait until conversion complete         */
        A2D[0] = peekb(ADR1);
        A2D[1] = peekb(ADR2);
        A2D[2] = peekb(ADR3);
        A2D[3] = peekb(ADR4);
        pokeb (ADCTL,0x14);                     /* start a2d conversion on PE4 -> 7       */
        while ((peekb(ADCTL) & 0x80)) ;         /* wait until conversion complete         */
        A2D[4] = peekb(ADR1);
        A2D[5] = peekb(ADR2);
        A2D[6] = peekb(ADR3);
        A2D[7] = peekb(ADR4);
        e_int();                                /* re-enable interrupts                   */
}



/*------------------------------------------------------*/
/*              Digital to Analog Conversion            */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.0                    */
/*                Date:          4/04/2000              */
/*                                                      */
/*  Name:      d2a()                                    */
/*                                                      */
/*  Function:  Send data out to D/A convertor's         */
/*             D2A[0] -> OUT A                          */
/*             D2A[1] -> OUT B                          */
/*             D2A[2] -> OUT C                          */
/*             D2A[3] -> OUT D                          */
/*                                                      */
/*  Modifies:  none                                     */
/*______________________________________________________*/

d2a()
{
#asm
        ldx _D2A_ADDR+2
        ldaa _D2A_VALU+3
        staa $0,x
        ldx _D2A_ADDR+4
        ldaa _D2A_VALU+5
        staa $0,x
        ldx _D2A_ADDR+6
        ldaa _D2A_VALU+7
        staa $0,x
        ldx _D2A_ADDR
        ldaa _D2A_VALU+1
        staa $0,x


        ldx _D2A_LDAC
        ldaa #$0
        staa $0,x
        
#endasm
}

      

