                        .module lab2sv15.c
                        .area data
 0000           _motorstatus::
 0000                   .blkb 1
                        .area idata
 0000 00                .byte 0
                        .area data
                        .area text
                ;  IX -> 0,x
                ;          dummy -> +3,x
 0000           _initSlave::
 0000 BD0000            jsr __enterb
 0003 04                .byte 0x4
 0004           ; /**********************************
 0004           ;       Nick Sitarski
 0004           ;       Vladi Gergov
 0004           ;       Lab 2
 0004           ;       ECE 473 W 2003
 0004           ;       Professor Shaout
 0004           ;       Slave
 0004           ; **********************************/
 0004           ; 
 0004           ; #include <hc11.h>
 0004           ; #include <stdio.h>
 0004           ; #include <vector.h>
 0004           ; 
 0004           ; /*********************************
 0004           ;       EQUATES
 0004           ; **********************************/
 0004           ; 
 0004           ; #define fivePercent 1000
 0004           ; 
 0004           ; /**********************************
 0004           ;       PRAGMA DEFINES
 0004           ; **********************************/
 0004           ; 
 0004           ; #pragma interrupt_handler PWMhandler()
 0004           ; 
 0004           ; /**********************************
 0004           ;       GLOBAL VARIABLES
 0004           ; **********************************/
 0004           ; 
 0004           ; short ontime, offtime;
 0004           ; char motorstatus=0;
 0004           ; 
 0004           ; /**********************************
 0004           ;       FUNCTION PROTOTYPES
 0004           ; **********************************/
 0004           ; 
 0004           ; void initSlave(void);
 0004           ; void PWMhandler(void);
 0004           ; 
 0004           ; /*************************************************************
 0004           ; Function: initSlave
 0004           ; Purpose: This function performs the initilization steps
 0004           ; for the target hc11 to be in slave mode.
 0004           ; Inputs: None
 0004           ; Outputs: None
 0004           ; **************************************************************/
 0004           ; 
 0004           ; void initSlave(void)
 0004           ; {
 0004           ;       char dummy;             /* dummy variable */
 0004           ;       
 0004           ;       ontime = 10000;         /* setup variable for motor to start at 50% duty cycle */
 0004 CC2710            ldd #10000
 0007 FD0002            std _ontime
 000A           ;       offtime = 10000;        /* setup variable for motor to start at 50% duty cycle */
 000A CC2710            ldd #10000
 000D FD0000            std _offtime
 0010           ; 
 0010           ;       SPCR = 0x40;            /* Enable the SPI system */
 0010 C640              ldab #64
 0012 F71028            stab 0x1028
 0015           ;       DDRD = (DDRD | 0x04);   /* Master in slave out (MISO) corresponding pin on Port D set to an output */
 0015 18CE1009          ldy #0x1009
 0019 181C0004          bset 0,y,#4
 001D           ;       PORTD = 0x00;   /* Clear Port D */
 001D 7F1008            clr 0x1008
 0020           ;       PACTL = 0x80;   /* Set PA7 to an output */
 0020 C680              ldab #128
 0022 F71026            stab 0x1026
 0025           ;       PORTA = 0x00;   /* Clear Port A */
 0025 7F1000            clr 0x1000
 0028           ;       dummy = SPSR;   /* Clear the Clear the SPIF ( Transfer Compleate flag ) */
 0028                   ; vol
 0028 F61029            ldab 0x1029
 002B E703              stab 3,x
 002D           ;       dummy = SPDR;   /* Clear the Clear the SPIF ( Transfer Compleate flag ) */
 002D                   ; vol
 002D F6102A            ldab 0x102a
 0030 E703              stab 3,x
 0032           ; 
 0032           ;       TMSK1 = 0x08;   /* Enable the OC5 Interrupt */
 0032 C608              ldab #8
 0034 F71022            stab 0x1022
 0037           ;       TFLG1 = 0x08;   /* Clear the OC5 Interrupt flag */
 0037 C608              ldab #8
 0039 F71023            stab 0x1023
 003C           ;       
 003C           ;       /****
 003C           ;       ISR ADDRESS INIT FROM VECTOR.H
 003C           ;       ****/
 003C           ;       TOC5vector = &PWMhandler;
 003C CC004E            ldd #_PWMhandler
 003F DDD4              std 0xd4
 0041           ;       
 0041           ;       printf("Initialization Compleate");
 0041 CC0211            ldd #L4
 0044 BD0000            jsr _printf
 0047           ;       
 0047           ; }
 0047           L3:
 0047 08                inx
 0048 08                inx
 0049 08                inx
 004A 08                inx
 004B 35                txs
 004C 38                pulx
 004D                   .dbline 0 ; func end
 004D 39                rts
 004E           _PWMhandler::
 004E           ; 
 004E           ; /*************************************************************
 004E           ; Function: PWMhandler
 004E           ; Purpose: This function controls the Pulse width modulation
 004E           ; Inputs: None
 004E           ; Outputs: None
 004E           ; **************************************************************/
 004E           ; 
 004E           ; void PWMhandler(void)
 004E           ; {
 004E           ; 		TFLG1 = 0x08;
 004E C608              ldab #8
 0050 F71023            stab 0x1023
 0053           ;               
 0053           ;               if(motorstatus==0) {
 0053 7D0000            tst _motorstatus
 0056 2618              bne L6
 0058           ;                       PORTA |= 0x80;                  /* Turn Motor on leaving LED's alone */
 0058 18CE1000          ldy #0x1000
 005C 181C0080          bset 0,y,#128
 0060           ;                       motorstatus = 1;                /* Tells the next ISR that the motor was turned on */
 0060 C601              ldab #1
 0062 F70000            stab _motorstatus
 0065           ;                       TOC5 = TOC5 + ontime;   /* Reschedule OC5 with new values */
 0065                   ; vol
 0065 FC101E            ldd 0x101e
 0068 F30002            addd _ontime
 006B FD101E            std 0x101e
 006E           ;               }
 006E 2014              bra L7
 0070           L6:
 0070           ; 		
 0070           ; 		else {
 0070           ; 			PORTA &= 0x7F;			/* Turns Motor off leaving LED's alone */
 0070 18CE1000          ldy #0x1000
 0074 181D0080          bclr 0,y,#0x80
 0078           ;                       motorstatus=0;                  /* Tells the next ISR that the motor was turned on */
 0078 7F0000            clr _motorstatus
 007B           ;                       TOC5 = TOC5 + offtime;  /* Reschedule OC1 with new values */
 007B                   ; vol
 007B FC101E            ldd 0x101e
 007E F30000            addd _offtime
 0081 FD101E            std 0x101e
 0084           ;               }
 0084           L7:
 0084           ; }
 0084           L5:
 0084                   .dbline 0 ; func end
 0084 3B                rti
 0085           ;  IX -> 0,x
 0085           ;          dummy -> +2,x
 0085           ;      spdr_data -> +3,x
 0085           _main::
 0085 BD0000            jsr __enterb
 0088 04                .byte 0x4
 0089           ; 
 0089           ; /*************************************************************
 0089           ; Function: Main
 0089           ; Purpose: This function controls the Pulse width modulation. It
 0089           ; recives the data from the master via the SPI and then decides
 0089           ; how to react according to what the data contains. If its a 1
 0089           ; then the motor gets turned off if its a 2 then the motor gets
 0089           ; turned on at 50% duty cycle and if its a 4 then the motor speed
 0089           ; is increased by 5%.
 0089           ; Inputs: None
 0089           ; Outputs: None
 0089           ; **************************************************************/
 0089           ; 
 0089           ; void main(void)
 0089           ; {
 0089           ;       char dummy, spdr_data;
 0089           ; 
 0089           ;       initSlave();            /* Calling the initialization function for the slave */
 0089 BD0000            jsr _initSlave
 008C           ;       
 008C           ;       asm("sei");                     /* Disable global interupts */
 008C 0F                        sei
 008D           
 008D           ;       
 008D           ;       dummy = SPSR;           /* Clear the SPIF flag using dummy variable */
 008D                   ; vol
 008D F61029            ldab 0x1029
 0090 E702              stab 2,x
 0092           ;       dummy = SPDR;           /* Clear the SPIF flag using dummy variable */
 0092                   ; vol
 0092 F6102A            ldab 0x102a
 0095 E702              stab 2,x
 0097 7E017F            jmp L10
 009A           X0:
 009A           ; 
 009A           ; 	while(1)
 009A           ; 	{
 009A           L12:
 009A           L13:
 009A           ; 		while ((SPSR&0x80)==0);		/* Wait untill data has been recived from the master via SPI */
 009A 18CE1029          ldy #0x1029
 009E 181F0080F7        brclr 0,y,#128,L12
 00A3           ;               
 00A3           ;               dummy = SPSR;           /* clear SPIF bit in SPSR via dummy dump (SPI transfer complete flag) */
 00A3                   ; vol
 00A3 F61029            ldab 0x1029
 00A6 E702              stab 2,x
 00A8           ;               spdr_data = SPDR;       /* Get the data from the SPDR and store it in spdr_data variable */
 00A8                   ; vol
 00A8 F6102A            ldab 0x102a
 00AB E703              stab 3,x
 00AD           ; 
 00AD           ;               /********************************
 00AD           ;               This if statement is active only when switch one from the master has been pressed
 00AD           ;               It turnes the interrupts and the motor off, and turns the RED LED on. Also schedules
 00AD           ;               the ontime and offtime to 50% duty cycle for initialization purposes and clears the
 00AD           ;               motorstatus global variable because the motor has been shut off
 00AD           ;               ********************************/
 00AD           ;               
 00AD           ;               if(spdr_data==0x01)
 00AD E603              ldab 3,x
 00AF C101              cmpb #1
 00B1 2635              bne L15
 00B3           ;               {       
 00B3           ;                       asm("sei");
 00B3 0F                        sei
 00B4           
 00B4           ;                       
 00B4           ;                       PORTA=0x00;
 00B4 7F1000            clr 0x1000
 00B7           ;                       PORTA=0x10;
 00B7 C610              ldab #16
 00B9 F71000            stab 0x1000
 00BC           ;                       offtime=10000;
 00BC CC2710            ldd #10000
 00BF FD0000            std _offtime
 00C2           ;                       ontime=10000;
 00C2 CC2710            ldd #10000
 00C5 FD0002            std _ontime
 00C8           ;               
 00C8           ;                       motorstatus=0;
 00C8 7F0000            clr _motorstatus
 00CB           ;                                                       
 00CB           ;                       printf("\nSwitch 1 Pressed...\nSetting PWM to: ");
 00CB CC01EB            ldd #L17
 00CE BD0000            jsr _printf
 00D1           ;                       printf("%d ontime, %d offtime", ontime, offtime);
 00D1 FC0000            ldd _offtime
 00D4 37                pshb
 00D5 36                psha
 00D6 FC0002            ldd _ontime
 00D9 37                pshb
 00DA 36                psha
 00DB CC01D5            ldd #L18
 00DE BD0000            jsr _printf
 00E1 1838              puly
 00E3 1838              puly
 00E5           ;               }
 00E5 7E017F            jmp L16
 00E8           L15:
 00E8           ; 		
 00E8           ; 		/********************************
 00E8           ; 		This else if statement is active only when switch two from the master has been pressed
 00E8           ; 		It turnes the interrupts and the motor on, and turns the GREEN LED on. Also schedules
 00E8           ; 		the ontime and offtime to 50% duty cycle for initialization purposes.
 00E8           ; 		********************************/
 00E8           ; 		
 00E8           ; 		else if	(spdr_data==0x02)
 00E8 E603              ldab 3,x
 00EA C102              cmpb #2
 00EC 2639              bne L19
 00EE           ;               {
 00EE           ;                       asm("cli");
 00EE 0E                        cli
 00EF           
 00EF           ;                       
 00EF           ;                       PORTA |= 0x20;
 00EF 18CE1000          ldy #0x1000
 00F3 181C0020          bset 0,y,#32
 00F7           ;                       PORTA &= 0xA0;
 00F7 18CE1000          ldy #0x1000
 00FB 181D005F          bclr 0,y,#0x5f
 00FF           ; 
 00FF           ;                       ontime = 10000;
 00FF CC2710            ldd #10000
 0102 FD0002            std _ontime
 0105           ;                       offtime = 10000;
 0105 CC2710            ldd #10000
 0108 FD0000            std _offtime
 010B           ;                       
 010B           ;                       printf("\nSwitch 2 Pressed...\nSetting PWM to: ");
 010B CC01AF            ldd #L21
 010E BD0000            jsr _printf
 0111           ;                       printf("%d ontime, %d offtime", ontime, offtime);
 0111 FC0000            ldd _offtime
 0114 37                pshb
 0115 36                psha
 0116 FC0002            ldd _ontime
 0119 37                pshb
 011A 36                psha
 011B CC01D5            ldd #L18
 011E BD0000            jsr _printf
 0121 1838              puly
 0123 1838              puly
 0125           ;               }
 0125 2058              bra L20
 0127           L19:
 0127           ; 		
 0127           ; 		/********************************
 0127           ; 		This if statement is active only when switch three from the master has been pressed
 0127           ; 		It turnes the interrupts and the motor on, and turns the YELLOW LED on. Also schedules
 0127           ; 		the ontime and offtime to 5% more each time it runs. The check for an ontime bigger than
 0127           ; 		20k is because if the ontime has reached 20k then we are at 100% duty cycle and there is
 0127           ; 		no need to increase it by 5% again.
 0127           ; 		********************************/
 0127           ; 		
 0127           ; 		else if(spdr_data==0x04)
 0127 E603              ldab 3,x
 0129 C104              cmpb #4
 012B 2652              bne L22
 012D           ;               {
 012D           ;                       asm("cli");
 012D 0E                        cli
 012E           
 012E           ;                       
 012E           ;                       PORTA |= 0x40;
 012E 18CE1000          ldy #0x1000
 0132 181C0040          bset 0,y,#64
 0136           ;                       PORTA &= 0xc0;
 0136 18CE1000          ldy #0x1000
 013A 181D003F          bclr 0,y,#0x3f
 013E           ; 
 013E           ;                       ontime += fivePercent;
 013E FC0002            ldd _ontime
 0141 C303E8            addd #1000
 0144 FD0002            std _ontime
 0147           ;                       offtime -= fivePercent;
 0147 FC0000            ldd _offtime
 014A 8303E8            subd #1000
 014D FD0000            std _offtime
 0150           ; 
 0150           ;                       if(ontime > 20000)
 0150 FC0002            ldd _ontime
 0153 1A834E20          cpd #20000
 0157 2F0C              ble L24
 0159           ;                       {
 0159           ;                               ontime = 20000;
 0159 CC4E20            ldd #20000
 015C FD0002            std _ontime
 015F           ;                               offtime = 0;
 015F CC0000            ldd #0
 0162 FD0000            std _offtime
 0165           ;                       }
 0165           L24:
 0165           ; 						
 0165           ; 			printf("\nSwitch 3 Pressed...\nSetting PWM to: ");
 0165 CC0189            ldd #L26
 0168 BD0000            jsr _printf
 016B           ;                       printf("%d ontime, %d offtime", ontime, offtime);
 016B FC0000            ldd _offtime
 016E 37                pshb
 016F 36                psha
 0170 FC0002            ldd _ontime
 0173 37                pshb
 0174 36                psha
 0175 CC01D5            ldd #L18
 0178 BD0000            jsr _printf
 017B 1838              puly
 017D 1838              puly
 017F           ;               }
 017F           L22:
 017F           L20:
 017F           L16:
 017F           L10:
 017F 7E009A            jmp L13
 0182           X1:
 0182           ; 	}
 0182           ; }
 0182           L8:
 0182 08                inx
 0183 08                inx
 0184 08                inx
 0185 08                inx
 0186 35                txs
 0187 38                pulx
 0188                   .dbline 0 ; func end
 0188 39                rts
                        .area bss
 0000           _offtime::
 0000                   .blkb 2
 0002           _ontime::
 0002                   .blkb 2
                        .area text
 0189           L26:
 0189 0A537769746368203320507265737365  .byte 10,'S,'w,'i,'t,'c,'h,32,51,32,'P,'r,'e,'s,'s,'e
 0199 642E2E2E0A53657474696E672050574D  .byte 'd,46,46,46,10,'S,'e,'t,'t,'i,'n,'g,32,'P,'W,'M
 01A9 20746F3A2000      .byte 32,'t,'o,58,32,0
 01AF           L21:
 01AF 0A537769746368203220507265737365  .byte 10,'S,'w,'i,'t,'c,'h,32,50,32,'P,'r,'e,'s,'s,'e
 01BF 642E2E2E0A53657474696E672050574D  .byte 'd,46,46,46,10,'S,'e,'t,'t,'i,'n,'g,32,'P,'W,'M
 01CF 20746F3A2000      .byte 32,'t,'o,58,32,0
 01D5           L18:
 01D5 2564206F6E74696D652C202564206F66  .byte 37,'d,32,'o,'n,'t,'i,'m,'e,44,32,37,'d,32,'o,'f
 01E5 6674696D6500      .byte 'f,'t,'i,'m,'e,0
 01EB           L17:
 01EB 0A537769746368203120507265737365  .byte 10,'S,'w,'i,'t,'c,'h,32,49,32,'P,'r,'e,'s,'s,'e
 01FB 642E2E2E0A53657474696E672050574D  .byte 'd,46,46,46,10,'S,'e,'t,'t,'i,'n,'g,32,'P,'W,'M
 020B 20746F3A2000      .byte 32,'t,'o,58,32,0
 0211           L4:
 0211 496E697469616C697A6174696F6E2043  .byte 'I,'n,'i,'t,'i,'a,'l,'i,'z,'a,'t,'i,'o,'n,32,'C
 0221 6F6D706C6561746500        .byte 'o,'m,'p,'l,'e,'a,'t,'e,0
