	.module valve4.c
	.dbfile valve4.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:			February 3, 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 = 0;
; int gain;
; int desired = 150;
; long count = 0;
; 	
; /************************ End of Global Variables ***********************/
; 
; 
L2:
	rti
;  IX -> 0,x
_SPI_isr::
	.dbfunc SPI_isr
	.dbline 47
; /************************** Function Declarations  **********************/
; void init_pulse();		//Initilizes TOC for PWM interrupts
; void pulse_handler();	//This is the function executed at each TOC2_isr
; /******************** End of Function Declarations  *********************/
; 
; 
L3:
	rti
;  IX -> 0,x
_PAIE_isr::
	.dbfunc PAIE_isr
	.dbline 53
; /**************************** Main **************************************/
; 
; void main(void)
; {
; 	/* VT100 clear screen */ 
; 	char clear[]= "\x1b\x5B\x32\x4A\x04"; 
L4:
	rti
;  IX -> 0,x
_PAV_isr::
	.dbfunc PAV_isr
	.dbline 59
; 	
; 	/* VT100 position cursor at (x,y) = (3,12) command is "\x1b[3;12H"*/
;   	char place[]= "\x1b[1;1H";	/*Home*/
;  
; 	SET_BIT(PACTL, 0x80);
; 	CLEAR_BIT(PACTL, 0x03); //Set RTI rate
L5:
	rti
;  IX -> 0,x
_TOC3_isr::
	.dbfunc TOC3_isr
	.dbline 89
; 	SET_BIT(TMSK2, 0x40);//Enable RTI nterrupt
; 	   	
;  	init_pulse();
;   	init_analog();
;   	init_serial();
; 
;   	//printf("%s", clear);  
;   	//printf("%s", place);
;   	
;   	printf("\tTitle:\t\tvalve.c\n"
;   "\tProgrammer:\tScott Nortman\n"
;   "\tDate:\t\tFebruary, 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 gain\n");
; 	gain = read_int();
; 	INTR_ON();
; 	desired = 150;
; 
; 	
; 	
; 	while(1)
; 	{
; 		
; 		
; 		duty_cycle = error * gain;
; 	
L6:
	rti
;  IX -> 0,x
_TIC3_isr::
	.dbfunc TIC3_isr
	.dbline 101
; 		if (duty_cycle < 0)
; 		{
; 			duty_cycle = -1 * duty_cycle;
; 			CLEAR_BIT(PORTA, 0x20);
; 		}
; 		else
; 		{
; 			SET_BIT(PORTA, 0x20);
; 		}
; 		
; 	}/*End While*/
;   	
L7:
	rti
;  IX -> 0,x
_TIC2_isr::
	.dbfunc TIC2_isr
	.dbline 107
; }/*End Main*/
; 
; 
; /**************************** End of Main *******************************/
; 
; 
L8:
	rti
;  IX -> 0,x
_TIC1_isr::
	.dbfunc TIC1_isr
	.dbline 113
; 
; /************************** Function Defintions *************************/
; 
; void init_pulse(void)
; {
; 	INTR_OFF();
L9:
	rti
;  IX -> 0,x
_IRQ_isr::
	.dbfunc IRQ_isr
	.dbline 125
; 	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
; 	//INTR_ON();
; 	
; 	printf("End of Init pulse\n\n");
; }
; 
; void pulse_handler()
; {	//printf("ISR\n");
; 
L10:
	rti
;  IX -> 0,x
_XIRQ_isr::
	.dbfunc XIRQ_isr
	.dbline 131
; 
; 	//enable toggle
; 	//CLEAR_BIT(TCTL1, 0x80);
; 	//SET_BIT(TCTL1, 0x40);
; 	if (duty_cycle <= 100)	
; 	//This handles if duty cycle is less than or equal to 50 e clocks ( almost always off)
L11:
	rti
;  IX -> 0,x
_SWI_isr::
	.dbfunc SWI_isr
	.dbline 137
; 	{
; 		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
; 	}
; 	
L12:
	rti
;  IX -> 0,x
_ILLOP_isr::
	.dbfunc ILLOP_isr
	.dbline 143
; 	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);
; 		TOC2 += period;			//Add period
; 	}
L13:
	rti
;  IX -> 0,x
_COP_isr::
	.dbfunc COP_isr
	.dbline 149
; 	
; 	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
; 		{
; 			CLEAR_BIT(TCTL1, 0x40);  //Clear bit 6 to set for falling edge on next interrupt
L14:
	rti
;  IX -> 0,x
_CLMON_isr::
	.dbfunc CLMON_isr
	.dbline 155
; 			SET_BIT(TCTL1, 0X80);
; 			TOC2 += duty_cycle;		//Add the duty cycle e clocks if you are now high
; 		}
; 		
; 		else //if(!(TCTL1 & 0x40))	//Test bit 6.  If it is low, you are now low and must change to go high
; 		{
L15:
	rti
	.area data
_drew::
	.blkw 1
	.area idata
	.word _interrupt_vectors
	.area data
_error::
	.blkb 2
	.area idata
	.word 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
;  func temp: 2,x - 4,x
;          place -> 5,x
;          clear -> 12,x
_main::
	jsr __enterb
	.byte 0x92
	.dbfile valve4.c
	.dbfunc main
	.dbline 53
	ldd 0,x
	addd #12
	ldy #L17
	pshx
	xgdx
	ldd #6
	jsr __asgnblk
	pulx
	.dbline 56
	ldd 0,x
	addd #5
	ldy #L18
	pshx
	xgdx
	ldd #7
	jsr __asgnblk
	pulx
	.dbline 58
	ldy #0x1026
	bset 0,y,#128
	.dbline 59
	ldy #0x1026
	bclr 0,y,#0x3
	.dbline 60
	ldy #0x1024
	bset 0,y,#64
	.dbline 62
	jsr _init_pulse
	.dbline 63
	jsr _init_analog
	.dbline 64
	jsr _init_serial
	.dbline 69
	ldd #L19
	jsr _printf
	.dbline 74
	ldd #L20
	jsr _printf
	.dbline 75
	ldd #L21
	jsr _printf
	.dbline 76
	jsr _read_int
	std 2,x
	ldd #1000
	ldy 2,x
	jsr __muli
	jsr __d2lreg
	ldy #_period
	jsr __lreg2y
	.dbline 77
	ldd #L22
	jsr _printf
	.dbline 78
	jsr _read_int
	std _gain
			cli

	.dbline 80
	ldd #150
	std _desired
	bra L24
L23:
	.dbline 88
	ldd _error
	ldy _gain
	jsr __muli
	jsr __d2lreg
	ldy #_duty_cycle
	jsr __lreg2y
	.dbline 90
	ldy #_duty_cycle
	jsr __ly2reg
	ldy #L28
	jsr __ly2reg2
	jsr __lcmp
	bge L26
	.dbline 92
	ldy #L29
	jsr __ly2reg
	ldy #_duty_cycle
	jsr __ly2reg2
	jsr __lmul
	ldy #_duty_cycle
	jsr __lreg2y
	.dbline 93
	ldy #0x1000
	bclr 0,y,#0x20
	.dbline 94
	bra L27
L26:
	.dbline 97
	ldy #0x1000
	bset 0,y,#32
	.dbline 98
L27:
	.dbline 100
L24:
	.dbline 84
	bra L23
L16:
	xgdx
	addd #18
	xgdx
	txs
	pulx
	rts
;  IX -> 0,x
_init_pulse::
			sei

	.dbfunc init_pulse
	.dbline 114
	ldy #0x1020
	bset 0,y,#128
	.dbline 115
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 116
	ldd #0
	std 0x1018
	.dbline 117
	ldy #0x1022
	bset 0,y,#64
	.dbline 120
	ldd #L31
	jsr _printf
	.dbline 121
L30:
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
_TOC2_isr::
	jsr __enterb
	.byte 0x82
	.dbfunc TOC2_isr
	.dbline 130
	ldy #_duty_cycle
	jsr __ly2reg
	ldy #L35
	jsr __ly2reg2
	jsr __lcmp
	bgt L33
	.dbline 133
	ldy #0x1020
	bset 0,y,#128
	.dbline 134
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 135
	ldy #_period
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 136
	jmp L34
L33:
	.dbline 138
	ldy #_period
	jsr __ly2reg
	ldy #_duty_cycle
	jsr __ly2reg2
	jsr __lsub
	ldy #L35
	jsr __ly2reg2
	jsr __lcmp
	bgt L36
	.dbline 140
	ldy #0x1020
	bclr 0,y,#0x80
	.dbline 141
	ldy #0x1020
	bset 0,y,#64
	.dbline 142
	ldy #_period
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 143
	bra L37
L36:
	.dbline 147
	ldy #0x1020
	brclr 0,y,#64,L38
	.dbline 149
	ldy #0x1020
	bclr 0,y,#0x40
	.dbline 150
	ldy #0x1020
	bset 0,y,#128
	.dbline 151
	ldy #_duty_cycle
	jsr __ly2reg2
	; vol
	ldd 0x1018
	jsr __ud2lreg
	jsr __ladd
	jsr __lreg2d
	std 0x1018
	.dbline 152
	bra L39
L38:
	.dbline 156
; 			SET_BIT(TCTL1, 0xC0);	//Set bit 6 to go high next interrupt
	ldy #0x1020
	bset 0,y,#192
	.dbline 157
; 			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 158
; 		}
L39:
	.dbline 159
; 	}
L37:
L34:
	.dbline 162
; 	
; 	
; 	CLEAR_FLAG(TFLG1, 0x40)	//Clear the interrupt flag
	ldy #0x1023
	bclr 0,y,#0xbf
	.dbline 163
; }	//This is the end of the interrupt routine
L32:
	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 167
; 
; void RTI_isr(void)
; {
; 	printf("Actual:  %d", valve_sensor);
	ldd #1
	jsr _analog
	pshb
	psha
	ldd #L41
	jsr _printf
	puly
	.dbline 168
; 	printf("                Setpoint:  %d\n", desired); 
	ldd _desired
	pshb
	psha
	ldd #L42
	jsr _printf
	puly
	.dbline 169
; 	error = desired - valve_sensor;
	ldd #1
	jsr _analog
	std 2,x
	ldd _desired
	subd 2,x
	std _error
	.dbline 170
; 	count = count + 1;
	ldy #_count
	jsr __ly2reg
	ldy #L43
	jsr __ly2reg2
	jsr __ladd
	ldy #_count
	jsr __lreg2y
	.dbline 171
; 	if (count >=500)
	ldy #_count
	jsr __ly2reg
	ldy #L46
	jsr __ly2reg2
	jsr __lcmp
	blt L44
	.dbline 173
; 	{
; 		count = 0;
	ldy #L28
	jsr __ly2reg
	ldy #_count
	jsr __lreg2y
	.dbline 174
; 		if (desired == 150)
	ldd _desired
	cpd #150
	bne L47
	.dbline 176
; 		{
; 			desired = 100;
	ldd #100
	std _desired
	.dbline 177
; 		}
	bra L48
L47:
	.dbline 179
; 		
; 		else if (desired == 100)
	ldd _desired
	cpd #100
	bne L49
	.dbline 181
; 		{
; 			desired = 150;
	ldd #150
	std _desired
	.dbline 182
; 		}
L49:
L48:
	.dbline 183
; 	}
L44:
	.dbline 184
; 	CLEAR_FLAG(TFLG2, 0x40);
	ldy #0x1025
	bclr 0,y,#0xbf
	.dbline 185
; }
L40:
	inx
	inx
	inx
	inx
	txs
	pulx
	rti
;  IX -> 0,x
_TOF_isr::
	.dbfunc TOF_isr
	.dbline 192
; 
; void TOF_isr(void)
; {
; 	
; 	
; 	
; 	CLEAR_FLAG(TFLG1, 0x80);
	ldy #0x1023
	bclr 0,y,#0x7f
	.dbline 193
; }
L51:
	rti
	.area bss
_gain::
	.blkb 2
_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
L46:
	.word 0,500
L43:
	.word 0,1
L42:
	.byte 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
	.byte 'S,'e,'t,'p,'o,'i,'n,'t,58,32,32,37,'d,10,0
L41:
	.byte 'A,'c,'t,'u,'a,'l,58,32,32,37,'d,0
L35:
	.word 0,100
L31:
	.byte 'E,'n,'d,32,'o,'f,32,'I,'n,'i,'t,32,'p,'u,'l,'s
	.byte 'e,10,10,0
L29:
	.word 65535,65535
L28:
	.word 0,0
L22:
	.byte 'E,'n,'t,'e,'r,32,'t,'h,'e,32,'g,'a,'i,'n,10,0
L21:
	.byte 'E,'n,'t,'e,'r,32,'t,'h,'e,32,'d,'e,'s,'i,'r,'e
	.byte 'd,32,'p,'e,'r,'i,'o,'d,32,'i,'n,32,'e,32,'c,'l
	.byte 'o,'c,'k,'s,58,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,'F,'e,'b,'r,'u,'a,'r,'y,44,32,50,48
	.byte 48,48,10,9,'V,'e,'r,'s,'i,'o,'n,9,9,49,10,10
	.byte 0
