#code 0x2400								/* Origin of code at 2400 hex    		*/
#data 0x2000								/* Variable space starts at 2000 hex	*/
#stack 0x1f00								/* Stack Space at 1F00 hex			*/

#include <startup.c>							/* Startup.c - Jmp to Main 			*/
#include <define.h>							/* Defines all ports (for small-c and ASM */
#include <b-vector.h>							/* Buffalo Interrupt vectors			*/
#include <buf-sub.h>							/* Buffalo Sub Routines				*/
#include <motor.c>							/* Motor Control Routines for Expanded IO */
#include <debug.c>							/* Debug routines (interface with CME-PC  */
#include <a2d-d2a.c>							/* A2D and D2A conversion routines		*/

unsigned int dummy;							/* Declare variables				*/
unsigned int t1;
unsigned int t2;
unsigned int t3;
unsigned int prog;
unsigned int prog_last;

main()
{
        dummy = 0;							/* Set default values				*/
        prog = 0;
        prog_last = 0x00ff;
        a2d_setup();							/* Set up A2D converter				*/

        while (dummy == 0)						/* Loop						*/
        {
                if (prog != prog_last)					/* If Program to run changed			*/
                {
                        pokeb (0xb582,0);				/* Clear all Expanded I/O Card Outputs    	*/
                        pokeb (0xb583,0);
                        pokeb (0xb584,0);
                        pokeb (0xb585,0);
                        pokeb (0xb586,0);
                        pokeb (0xb587,0);
                        pokeb (0xb588,0);
                        mtr_init(0xB583,0,1);				/* Re-initialize motor control port		*/
                }
               
                switch (prog)						/* Select program to run by setting prog  	*/
                {
                        case 0:						/* Prog 0 - Output port (IO2) = Input 		*/
                                t1 = peekb(0xb580);			/*          Port (IO0)				*/
                                pokeb(0xb582,t1);
                                break;

                        case 1:						/* Prog 1 - Light Meter (PBS Input on PE7)	*/
                                t1 = A2D[7];				/*          Intensity bar graph on Output 	*/
                                t2 = 0;					/*  		Port (IO2)			*/
	
                                if (t1 > 0)
                                        t2 = 1;
                                if (t1 > 32)
                                        t2 = 3;
                                if (t1 > 64)
                                        t2 = 7;
                                if (t1 > 96)
                                        t2 = 15;
                                if (t1 > 128)
                                        t2 = 31;
                                if (t1 > 160)
                                        t2 = 63;
                                if (t1 > 192)
                                        t2 = 127;
                                if (t1 > 224)
                                        t2 = 255;

                                pokeb(0xb582,t2);
                                break;

                        case 2:							/* Prog 2 - Temperature alarm 		*/
                                t1 = (A2D[4] * 29 + 92);			/*          Thermistor on PE4 		*/
                                if (prog != prog_last)				/*		Set Point in T2   	*/
                                        t2 = t1;				/* 		Dead Band in T3		*/
                                if (t1>(t2+t3))					/*		Output port shows 	*/
                                        pokeb(0xb582,0x80);			/* 		High/OK/LOW alarm 	*/
                                else if (t1<(t2-t3))				/* 		lights			*/
                                        pokeb(0xb582,0x20);
                                else
                                        pokeb(0xb582,0x40);

                                break;


                        case 3:							/* Prog 3 - Stepper motor		*/
                                t1 = peekb(0xb580);				/*          free run demo    		*/
                                mtr_mode(A,t1 & 3);				
                                mtr_scon(A);					/* 		IO1 = Speed 		*/
                                mtr_clock(A,peekb(0xb581)*0xff,50);		/*		IO2 = Mode + Dir  	*/
                                mtr_on(A);
                                t1 = t1 & 4;
                                switch (t1)
                                {
                                        case 0:
                                                mtr_forward(A);
                                                break;
                                        case 4:
                                                mtr_reverse(A);
                                                break;
                                }
                                break;
	
                        case 4:							/* Prog 4 - DC Motor Demo		*/
                                mtr_clock(B,0xff00,peekb(0xB581)*100/255);
                                mtr_on(B);
                                switch (peekb(0xb580) & 1)			/* 		IO1 = Duty Cycle  	*/
                                {						/*                (IO1 / 255) 		*/
                                        case 0:
                                                mtr_forward(B);			/* 		IO0 = Dir + On/OFF	*/
                                                break;
                                        case 1:
                                                mtr_reverse(B);
                                                break;
                                }
                                break;


                        case 5:							/* Prog - 5 D2A Demo			*/
                                if (prog != prog_last)				/* 		Chn A = Sawtooth  	*/
                                {						/*		Chn B = Sawtooth  	*/
                                        t1 = 0;					/*		Chn C = Sawtooth  	*/
                                        t2 = 20;				/*		Chn D = Square 		*/
                                        t3 = 40;				
                                        d2a_setup();				/* Chn B+C are Same shape as  		*/
                                }						/* Chn A but delayed			*/

                                D2A_VALUE[0] = t1;
                                D2A_VALUE[1] = t2;
                                D2A_VALUE[2] = t3;

                                if (t1>128) 
                                        D2A_VALUE[3] = 150;
                                if (t1<=128)
                                        D2A_VALUE[3] = 10;

                                d2a();

                                t1 ++;
                                t2 ++;
                                t3 ++;
                                if (t1 > 255)
                                        t1 = 0;
                                if (t2 > 255)
                                        t2 = 0;
                                if (t3 > 255)
                                        t3 = 0;
                                break;


                        case 6:							/* Prog 6 - D2A Chn B = PE3		*/
                                D2A_VALUE[1] = A2D[3];
                                d2a();
                                break;


                        case 7:							/* Prog 7 - Stepper Motor 		*/
                                if (prog != prog_last)				/*		Go defined number	*/
                                {						/* 		of steps then stop	*/
                                        t2 = 0xffff;				/* 		t2 = steps to go  	*/
                                        t3 = 0xffff;
                                }
                                if (t2 != t3)
                                {
                                        mtr_stepcnt(A,t2);
                                        t3=t2;
                                }
                                t1 = peekb(0xb580);
                                mtr_mode(A,t1 & 3);
                                mtr_scon(A);
                                mtr_clock(A,peekb(0xb581)*0xff,50);
                                t1 = t1 & 4;
                                switch (t1)
                                {
                                        case 0:
                                                mtr_forward(A);
                                                break;
                                        case 4:
                                                mtr_reverse(A);
                                                break;
                                }
                                break;


                }


                a2d();									/* Get Values from A2D	*/
                prog_last = prog;
                debug();								/* Run Debug routine	*/
                
        }
        

}




