	.module finally.c
	.dbfile finally.c
	.area text
;  IX -> 0,x
_SCI_isr::
	.dbfile C:\ICCTJP\include/isrdecl.h
	.dbfunc SCI_isr
	.dbline 41
; /************************************************************************************************************************
; *	Program:		finally.c							*
; *	Description:	This program was written to test the pneumatic		                *
; *					valves and cylinders.  It will prompt the user	*
; *					to select the desired function (manual or 		*
; *					automatic positioning) and to select the desired      *
; *					characteristics of the PWM signal for the		*
; *					valves.					*
; *	Programmer:		Scott Nortman					*
; *	Date:			March, 2000					*
; ************************************************************************************************************************/
; 
; /**************************** Constants *********************************/
; #define pulse_handler TOC2_isr
; #define tolerance 10 
; #define valve_sensor analog(1)
; /**************************** End of Constants **************************/
; 
; /*************************** Interrupts *********************************/
; #pragma interrupt_handler TOC2_isr, RTI_isr, TOF_isr;
; /*************************** End of Interrupts **************************/
; 
; /************************** Includes ************************************/
; #include <tjpbase.h>
; #include <analog.h>
; #include <vectors.h>
; #include <mil.h>
; #include <hc11.h>
; /************************ End of includes *******************************/
; 
; /************************** Global Variables*****************************/
; long duty_cycle;	//duty_cycle in e clocks
; long period;		//period in e clocks
; int error[3] = {0};
; int desired = 150;
; long count = 0;
; 	
; /************************ End of Global Variables ***********************/
; 
; 
; /************************** Function Declarations  **********************/
L2:
	rti
;  IX -> 0,x
_SPI_isr::
	.dbfunc SPI_isr
	.dbline 47
; void init_pulse();		//Initilizes TOC for PWM interrupts
; void pulse_handler();	//This is the function executed at each TOC2_isr
; /******************** End of Function Declarations  *********************/
; 
; 
; /**************************** Main **************************************/
L3:
	rti
;  IX -> 0,x
_PAIE_isr::
	.dbfunc PAIE_isr
	.dbline 53
; 
; void main(void)
; {
; 	int Mp, Md;
; 	int Kp, Kd;
; 	/* VT100 clear screen */ 
L4:
	rti
;  IX -> 0,x
_PAV_isr::
	.dbfunc PAV_isr
	.dbline 59
; 	char clear[]= "\x1b\x5B\x32\x4A\x04"; 
; 	
; 	/* VT100 position cursor at (x,y) = (3,12) command is "\x1b[3;12H"*/
;   	char place[]= "\x1b[1;1H";	/*Home*/
;  
; 	SET_BIT(PACTL, 0x80);
L5:
	rti
;  IX -> 0,x
_TOC3_isr::
	.dbfunc TOC3_isr
	.dbline 89
; 	CLEAR_BIT(PACTL, 0x03); 	//Set RTI rate
; 	SET_BIT(TMSK2, 0x40);		//Enable RTI interrupt
; 	   	
;  	init_pulse();
;   	init_analog();
;   	init_serial();
; 
;   	//printf("%s", clear);  
;   	//printf("%s", place);
;   	
;   	printf("\tTitle:\t\tvalve.c\n"
;   "\tProgrammer:\tScott Nortman\n"
;   "\tDate:\t\tMarch, 2000\n"
;   "\tVersion\t\t1\n\n");
;   
; 	printf("PNEUMATIC CYLINDER DIAGNOSTIC PROGRAM\n\n");
; 	//printf("Enter the desired period in e clocks:\n");
; 	//period = (read_int() * 1000);
; 	printf("Enter the proportional gain\n");
; 	Kp = read_int();
; 	printf("Enter the derivative gain\n");
; 	Kd = read_int();
; 	INTR_ON();
; 	desired = 150;
; 
; 	
; 	
; 	while(1)
; 	{
; 		
L6:
	rti
;  IX -> 0,x
_TIC3_isr::
	.dbfunc TIC3_isr
	.dbline 101
; 		//Proportional control
; 		Mp = Kp * error[0];
; 		//Derivative Control
; 		Md = Kd * (error[0] - error[1]);
; 		
; 		period = Mp + Md;
; 		duty_cycle = (period / 2);
; 	
; 		if (duty_cycle < 0)
; 		{
; 			duty_cycle = -1 * duty_cycle;
; 			CLEAR_BIT(PORTA, 0x20);
L7:
	rti
;  IX -> 0,x
_TIC2_isr::
	.dbfunc TIC2_isr
	.dbline 107
; 		}
; 		else
; 		{
; 			SET_BIT(PORTA, 0x20);
; 		}
; 		
L8:
	rti
;  IX -> 0,x
_TIC1_isr::
	.dbfunc TIC1_isr
	.dbline 113
; 	}/*End While*/
;   	
; }/*End Main*/
; 
; 
; /**************************** End of Main *******************************/
L9:
	rti
;  IX -> 0,x
_IRQ_isr::
	.dbfunc IRQ_isr
	.dbline 125
; 
; 
; 
; /************************** Function Defintions *************************/
; 
; void init_pulse(void)
; {
; 	INTR_OFF();
; 	SET_BIT(TCTL1, 0x80);	//Enable OC2 for falling edge on first sucessful compare (safe default)
; 	CLEAR_BIT(TCTL1, 0x40);
; 	TOC2 = 0;	//Set TOC2 to zero
; 	SET_BIT(TMSK1, 0x40);	//Enable OC2 mask
L10:
	rti
;  IX -> 0,x
_XIRQ_isr::
	.dbfunc XIRQ_isr
	.dbline 131
; 	//INTR_ON();
; 	
; 	printf("End of Init pulse\n\n");
; }
; 
; void pulse_handler()
L11:
	rti
;  IX -> 0,x
_SWI_isr::
	.dbfunc SWI_isr
	.dbline 137
; {	//printf("ISR\n");
; 
; 
; 	//enable toggle
; 	//CLEAR_BIT(TCTL1, 0x80);
; 	//SET_BIT(TCTL1, 0x40);
L12:
	rti
;  IX -> 0,x
_ILLOP_isr::
	.dbfunc ILLOP_isr
	.dbline 143
; 	if (duty_cycle <= 100)	
; 	//This handles if duty cycle is less than or equal to 100 e clocks ( almost always off)
; 	{
; 		SET_BIT(TCTL1, 0x80);	//Enable OC2 for falling edge (make sure pin goes low)
; 		CLEAR_BIT(TCTL1, 0X40);
; 		TOC2 += period;			//Add the period to TOC2
L13:
	rti
;  IX -> 0,x
_COP_isr::
	.dbfunc COP_isr
	.dbline 149
; 	}
; 	
; 	else if ((period - duty_cycle) <= 100)	//This handles what to do if the pin should almost always be high
; 	{
; 		CLEAR_BIT(TCTL1, 0x80);	//Set the pin to rising edge
; 		SET_BIT(TCTL1, 0X40);
L14:
	rti
;  IX -> 0,x
_CLMON_isr::
	.dbfunc CLMON_isr
	.dbline 155
; 		TOC2 += period;			//Add period
; 	}
; 	
; 	else		//This is for the general case
; 	{
; 		if (TCTL1 & 0x40)	//Test bit 6.  If it is a one, you are now high and must change to go low
L15:
	rti
	.area data
_drew::
	.blkw 1
	.area idata
	.word _interrupt_vectors
	.area data
_error::
	.blkb 2
	.area idata
	.word 0
	.area data
	.blkb 4
	.area idata
	.byte 0,0,0,0
	.area data
_desired::
	.blkb 2
	.area idata
	.word 150
	.area data
_count::
	.blkb 4
	.area idata
	.word 0,0
	.area text
L17:
	.byte 27,91,50,'J,4,0
L18:
	.byte 27,91,49,59,49,'H,0
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
;          place -> 3,x
;          clear -> 10,x
;             Kd -> 16,x
;             Kp -> 18,x
;             Md -> 20,x
;             Mp -> 22,x
_main::
	jsr __enterb
	.byte 0x98
	.dbfile finally.c
	.dbfunc main
	.dbline 54
	ldd 0,x
	addd #10
	ldy #L17
	pshx
	xgdx
	ldd #6
	jsr __asgnblk
	pulx
	.dbline 57
	ldd 0,x
	addd #3
	ldy #L18
	pshx
	xgdx
	ldd #7
	jsr __asgnblk
	pulx
	.dbline 59
	ldy #0x1026
	bset 0,y,#128
	.dbline 60
	ldy #0x1026
	bclr 0,y,#0x3
	.dbline 61
	ldy #0x1024
	bset 0,y,#64
	.dbline 63
	jsr _init_pulse
	.dbline 64
	jsr _init_analog
	.dbline 65
	jsr _init_serial
	.dbline 70
	ldd #L19
	jsr _printf
	.dbline 75
	ldd #L20
	jsr _printf
	.dbline 78
	ldd #L21
	jsr _printf
	.dbline 79
	jsr _read_int
	std 18,x
	.dbline 80
	ldd #L22
	jsr _printf
	.dbline 81
	jsr _read_int
	std 16,x
			cli

	.dbline 83
	ldd #150
	std _desired
	jmp L24
L23:
	.dbline 91
	ldd 18,x
	ldy _error
	jsr __muli
	std 22,x
	.dbline 93
	ldd _error
	subd _error+2
	xgdy
	ldd 16,x
	jsr __muli
	std 20,x
	.dbline 95
	ldd 22,x
	addd 20,x
	jsr __d2lreg
	ldy #_period
	jsr __lreg2y
	.dbline 96
	ldy #_period
	jsr __ly2reg
	ldy #L27
	jsr __ly2reg2
	jsr __ldiv
	ldy #_duty_cycle
	jsr __lreg2y
	.dbline 98
	ldy #_duty_cycle
	jsr __ly2reg
	ldy #L30
	jsr __ly2reg2
	jsr __lcmp
	bge L28
	.dbline 100
	ldy #L31
	jsr __ly2reg
	ldy #_duty_cycle
	jsr __ly2reg2
	jsr __lmul
	ldy #_duty_cycle
	jsr __lreg2y
	.dbline 101
	ldy #0x1000
	bclr 0,y,#0x20
	.dbline 102
	bra L29
L28:
	.dbline 105
	ldy #0x1000
	bset 0,y,#32
	.dbline 106
L29:
	.dbline 108
L24:
	.dbline 87
	jmp L23
L16:
	xgdx
	addd #24
	xgdx
	txs
	pulx
	rts
;  IX -> 0,x
_init_pulse::
			sei

	.dbfunc init_pulse
	.dbline 122
	ldy #0x1020
	bset 0,y,#128
	.dbline 123
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 124
	ldd #0
	std 0x1018
	.dbline 125
	ldy #0x1022
	bset 0,y,#64
	.dbline 128
	ldd #L33
	jsr _printf
	.dbline 129
L32:
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
_TOC2_isr::
	jsr __enterb
	.byte 0x82
	.dbfunc TOC2_isr
	.dbline 138
	ldy #_duty_cycle
	jsr __ly2reg
	ldy #L37
	jsr __ly2reg2
	jsr __lcmp
	bgt L35
	.dbline 141
	ldy #0x1020
	bset 0,y,#128
	.dbline 142
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 143
	ldy #_period
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 144
	jmp L36
L35:
	.dbline 146
	ldy #_period
	jsr __ly2reg
	ldy #_duty_cycle
	jsr __ly2reg2
	jsr __lsub
	ldy #L37
	jsr __ly2reg2
	jsr __lcmp
	bgt L38
	.dbline 148
	ldy #0x1020
	bclr 0,y,#0x80
	.dbline 149
	ldy #0x1020
	bset 0,y,#64
	.dbline 150
	ldy #_period
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 151
	bra L39
L38:
	.dbline 155
	ldy #0x1020
	brclr 0,y,#64,L40
	.dbline 157
; 		{
; 			CLEAR_BIT(TCTL1, 0x40);  //Clear bit 6 to set for falling edge on next interrupt
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 158
; 			SET_BIT(TCTL1, 0X80);
	ldy #0x1020
	bset 0,y,#128
	.dbline 159
; 			TOC2 += duty_cycle;		//Add the duty cycle e clocks if you are now high
	ldy #_duty_cycle
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 160
; 		}
	bra L41
L40:
	.dbline 164
; 		
; 		else //if(!(TCTL1 & 0x40))	//Test bit 6.  If it is low, you are now low and must change to go high
; 		{
; 			SET_BIT(TCTL1, 0xC0);	//Set bit 6 to go high next interrupt
	ldy #0x1020
	bset 0,y,#192
	.dbline 165
; 			TOC2 += (period - duty_cycle);
	ldy #_period
	jsr __ly2reg
	ldy #_duty_cycle
	jsr __ly2reg2
	jsr __lsub
	jsr __lregmov
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 166
; 		}
L41:
	.dbline 167
; 	}
L39:
L36:
	.dbline 170
; 	
; 	
; 	CLEAR_FLAG(TFLG1, 0x40)	//Clear the interrupt flag
	ldy #0x1023
	bclr 0,y,#0xbf
	.dbline 171
; }	//This is the end of the interrupt routine
L34:
	inx
	inx
	txs
	pulx
	rti
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
;  func temp: 2,x - 4,x
_RTI_isr::
	jsr __enterb
	.byte 0x84
	.dbfunc RTI_isr
	.dbline 175
; 
; void RTI_isr(void)
; {
; 	printf("%d\n", valve_sensor);
	ldd #1
	jsr _analog
	pshb
	psha
	ldd #L43
	jsr _printf
	puly
	.dbline 176
; 	error[2] = error[1];
	ldd _error+2
	std _error+4
	.dbline 177
; 	error[1] = error[0];
	ldd _error
	std _error+2
	.dbline 178
; 	error[0] = desired - valve_sensor;
	ldd #1
	jsr _analog
	std 2,x
	ldd _desired
	subd 2,x
	std _error
	.dbline 179
; 	count += 1;
	ldy #_count
	jsr __ly2reg
	ldy #L47
	jsr __ly2reg2
	jsr __ladd
	ldy #_count
	jsr __lreg2y
	.dbline 180
; 	if (count >=500)
	ldy #_count
	jsr __ly2reg
	ldy #L50
	jsr __ly2reg2
	jsr __lcmp
	blt L48
	.dbline 182
; 	{
; 		count = 0;
	ldy #L30
	jsr __ly2reg
	ldy #_count
	jsr __lreg2y
	.dbline 183
; 		if (desired == 150)
	ldd _desired
	cpd #150
	bne L51
	.dbline 185
; 		{
; 			desired = 100;
	ldd #100
	std _desired
	.dbline 186
; 		}
	bra L52
L51:
	.dbline 188
; 		
; 		else if (desired == 100)
	ldd _desired
	cpd #100
	bne L53
	.dbline 190
; 		{
; 			desired = 150;
	ldd #150
	std _desired
	.dbline 191
; 		}
L53:
L52:
	.dbline 192
; 	}
L48:
	.dbline 193
; 	CLEAR_FLAG(TFLG2, 0x40);
	ldy #0x1025
	bclr 0,y,#0xbf
	.dbline 194
; }
L42:
	inx
	inx
	inx
	inx
	txs
	pulx
	rti
;  IX -> 0,x
_TOF_isr::
	.dbfunc TOF_isr
	.dbline 198
; 
; void TOF_isr(void)
; {
; 	CLEAR_FLAG(TFLG1, 0x80);
	ldy #0x1023
	bclr 0,y,#0x7f
	.dbline 199
; }
L55:
	rti
	.area bss
_period::
	.blkb 4
_duty_cycle::
	.blkb 4
_timertjp::
	.blkb 2
_days::
	.blkb 2
_hours::
	.blkb 2
_minutes::
	.blkb 2
_seconds::
	.blkb 2
_msec::
	.blkb 2
	.area text
L50:
	.word 0,500
L47:
	.word 0,1
L43:
	.byte 37,'d,10,0
L37:
	.word 0,100
L33:
	.byte 'E,'n,'d,32,'o,'f,32,'I,'n,'i,'t,32,'p,'u,'l,'s
	.byte 'e,10,10,0
L31:
	.word 65535,65535
L30:
	.word 0,0
L27:
	.word 0,2
L22:
	.byte 'E,'n,'t,'e,'r,32,'t,'h,'e,32,'d,'e,'r,'i,'v,'a
	.byte 't,'i,'v,'e,32,'g,'a,'i,'n,10,0
L21:
	.byte 'E,'n,'t,'e,'r,32,'t,'h,'e,32,'p,'r,'o,'p,'o,'r
	.byte 't,'i,'o,'n,'a,'l,32,'g,'a,'i,'n,10,0
L20:
	.byte 'P,'N,'E,'U,'M,'A,'T,'I,'C,32,'C,'Y,'L,'I,'N,'D
	.byte 'E,'R,32,'D,'I,'A,'G,'N,'O,'S,'T,'I,'C,32,'P,'R
	.byte 'O,'G,'R,'A,'M,10,10,0
L19:
	.byte 9,'T,'i,'t,'l,'e,58,9,9,'v,'a,'l,'v,'e,46,'c
	.byte 10,9,'P,'r,'o,'g,'r,'a,'m,'m,'e,'r,58,9,'S,'c
	.byte 'o,'t,'t,32,'N,'o,'r,'t,'m,'a,'n,10,9,'D,'a,'t
	.byte 'e,58,9,9,'M,'a,'r,'c,'h,44,32,50,48,48,48,10
	.byte 9,'V,'e,'r,'s,'i,'o,'n,9,9,49,10,10,0
