                         .module slave.c
                         .area text
                       ;  IX -> 0,x
                       ;          dummy -> +3,x
 0000                 _initBoardB::
 0000 BD0000            jsr __enterb
 0003 04                .byte 0x4
                       ; /************************************************************************
                       ; * Term:   Fall 2003
                       ; * Course: ECE 473
                       ; * Name:   Olamide Adabonyan / Philip Lechowicz
                       ; * E-mail: o_mide@yahoo.com / phillech@aol.com
                       ; *
                       ; * REVISION HISTORY
                       ; *
                       ; * First Draft: Oct 18, 2002
                       ; *              -LEDs not lighting up correctly
                       ; *
                       ; * Date Submitted:   ?/??/02
                       ; * Demo Approval:    ________
                       ; ************************************************************************/
                       ; 
                       ; #include <hc11.h>
                       ; #include <stdio.h>
                       ; #include <vector.h>
                       ; 
                       ; #define endProg() asm("swi")
                       ; #define enableInt() asm("cli")
                       ; #define disableInt() asm("sei")
                       ; 
                       ; #define fivePercent 1000
                       ; 
                       ; short ontime, offtime;
                       ; char motorStatus;
                       ; 
                       ; void initBoardB(void);
                       ; void OC1handler(void);
                       ; 
                       ; /*******************************************************
                       ; Name: initBoardB
                       ; Purpose: initilize slave board (boardB)
                       ;          - turn LEDs off
                       ;          - turn motor on with PWM duty cycle of 50%
                       ;          - configure SPI
                       ;          - set up OC1
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; void initBoardB()
                       ; {
                       ;     char dummy;
                       ;     disableInt(); //disable interrupts
 0004 0F                    sei
                       
                       ;     
                       ;     PORTA = 0x00;
 0005 7F1000            clr 0x1000
                       ;     ontime = 10000;
 0008 CC2710            ldd #10000
 000B FD0003            std _ontime
                       ;     offtime = 10000;
 000E CC2710            ldd #10000
 0011 FD0001            std _offtime
                       ;     
                       ;     //??? SPI Configuration... [master basically does everything]
                       ;     SPCR = 0x40;
 0014 C640              ldab #64
 0016 F71028            stab 0x1028
                       ;     DDRD = 0x04;
 0019 C604              ldab #4
 001B F71009            stab 0x1009
                       ;     dummy = SPSR;
                         ; vol
 001E F61029            ldab 0x1029
 0021 E703              stab 3,x
                       ;     dummy = SPDR;
                         ; vol
 0023 F6102A            ldab 0x102a
 0026 E703              stab 3,x
                       ;     
                       ;     TOC1vector= &OC1handler;
 0028 CC0049            ldd #_OC1handler
 002B FDFFE8            std 0xffe8
                       ;     TMSK1 = 0x80;
 002E C680              ldab #128
 0030 F71022            stab 0x1022
                       ;     TFLG1 = 0x80;
 0033 C680              ldab #128
 0035 F71023            stab 0x1023
                       ;     TOC1 = TCNT + ontime;
                         ; vol
 0038 FC100E            ldd 0x100e
 003B F30003            addd _ontime
 003E FD1016            std 0x1016
                       ;     
                       ;     enableInt(); //enable interrupts
 0041 0E                    cli
                       
                       ; }
 0042                 L3:
 0042 08                inx
 0043 08                inx
 0044 08                inx
 0045 08                inx
 0046 35                txs
 0047 38                pulx
 0048                   .dbline 0 ; func end
 0048 39                rts
 0049                 _OC1handler::
                       ; 
                       ; 
                       ; /*******************************************************
                       ; Name: OC1handler
                       ; Purpose: control ontime/offtime for motor (using PWM)
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; #pragma interrupt_handler OC1handler()
                       ; void OC1handler()
                       ; {
                       ;     if (ontime >= 20000)
 0049 FC0003            ldd _ontime
 004C 1A834E20          cpd #20000
 0050 2D1F              blt L5
                       ;     {
                       ;         ontime = 20000;
 0052 CC4E20            ldd #20000
 0055 FD0003            std _ontime
                       ;         offtime = 0;
 0058 CC0000            ldd #0
 005B FD0001            std _offtime
                       ;         TOC1 = TOC1 + ontime;
                         ; vol
 005E FC1016            ldd 0x1016
 0061 F30003            addd _ontime
 0064 FD1016            std 0x1016
                       ;         PORTA |= 0x60;
 0067 18CE1000          ldy #0x1000
 006B 181C0060          bset 0,y,#96
                       ;         //printf("max PWM...\n");   //debugger
                       ;     }
 006F 205D              bra L6
 0071                 L5:
                       ;     else if (offtime >= 20000)
 0071 FC0001            ldd _offtime
 0074 1A834E20          cpd #20000
 0078 2D1C              blt L7
                       ;     {
                       ;         offtime = 20000;
 007A CC4E20            ldd #20000
 007D FD0001            std _offtime
                       ;         ontime = 0;
 0080 CC0000            ldd #0
 0083 FD0003            std _ontime
                       ;         TOC1 = TOC1 + offtime;
                         ; vol
 0086 FC1016            ldd 0x1016
 0089 F30001            addd _offtime
 008C FD1016            std 0x1016
                       ;         PORTA = 0x10;
 008F C610              ldab #16
 0091 F71000            stab 0x1000
                       ;         //printf("min PWM...\n"); //debugger
                       ;     }   
 0094 2038              bra L8
 0096                 L7:
                       ;     else if (motorStatus == 1)
 0096 F60000            ldab _motorStatus
 0099 C101              cmpb #1
 009B 2616              bne L9
                       ;     {
                       ;         motorStatus = 0;
 009D 7F0000            clr _motorStatus
                       ;         TOC1 = TOC1 + offtime;
                         ; vol
 00A0 FC1016            ldd 0x1016
 00A3 F30001            addd _offtime
 00A6 FD1016            std 0x1016
                       ;         PORTA ^= 0x40;
                         ; vol
 00A9 F61000            ldab 0x1000
 00AC C840              eorb #64
 00AE F71000            stab 0x1000
                       ;     }
 00B1 201B              bra L10
 00B3                 L9:
                       ;     else if(motorStatus == 0)
 00B3 7D0000            tst _motorStatus
 00B6 2616              bne L11
                       ;     {
                       ;         motorStatus = 1;
 00B8 C601              ldab #1
 00BA F70000            stab _motorStatus
                       ;         TOC1 = TOC1 + ontime;
                         ; vol
 00BD FC1016            ldd 0x1016
 00C0 F30003            addd _ontime
 00C3 FD1016            std 0x1016
                       ;         PORTA ^= 0x40;
                         ; vol
 00C6 F61000            ldab 0x1000
 00C9 C840              eorb #64
 00CB F71000            stab 0x1000
                       ;     }
 00CE                 L11:
 00CE                 L10:
 00CE                 L8:
 00CE                 L6:
                       ;     
                       ;     TFLG1 = 0x80;
 00CE C680              ldab #128
 00D0 F71023            stab 0x1023
                       ; }
 00D3                 L4:
 00D3                   .dbline 0 ; func end
 00D3 3B                rti
                       ;  IX -> 0,x
                       ;          dummy -> +2,x
                       ;   masterSignal -> +3,x
 00D4                 _main::
 00D4 BD0000            jsr __enterb
 00D7 04                .byte 0x4
                       ; 
                       ; 
                       ; /*******************************************************
                       ; Name: Main
                       ; Purpose:
                       ;         - Initialize system by calling initBoardB() function
                       ;         - Wait for SPI messages and calls other functions
                       ;            to do specific tasks, depending on message recieved.
                       ;         - Controls state of LEDs
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; main()
                       ; {
                       ;     char dummy, masterSignal;
                       ;     
                       ;     initBoardB();
 00D8 BD0000            jsr _initBoardB
 00DB 7E019D            jmp L15
 00DE                 X0:
                       ;     
                       ;     while(1)
                       ;     {
 00DE                 L17:
 00DE                 L18:
                       ;         //dummy = SPSR;
                       ;         //dummy = SPDR;
                       ;         
                       ;         while ((SPSR & 0x80) == 0)
 00DE 18CE1029          ldy #0x1029
 00E2 181F0080F7        brclr 0,y,#128,L17
                       ;         {
                       ;             /*check for collision error
                       ;             if((SPSR & 0x40) != 0)
                       ;             {
                       ;                 dummy = SPSR; //this clears collision error flag (WCOL) together with next step
                       ;                 dummy = SPDR; //read corrupted data
                       ;                 printf("\ncollision error\n");
                       ;             }*/
                       ;         }
                       ;         
                       ;         //clear SPIF bit in SPSR (SPI transfer complete flag)
                       ;         dummy = SPSR;           
                         ; vol
 00E7 F61029            ldab 0x1029
 00EA E702              stab 2,x
                       ;         masterSignal = SPDR;
                         ; vol
 00EC F6102A            ldab 0x102a
 00EF E703              stab 3,x
                       ;         
                       ;         if (masterSignal == 0x00)
 00F1 6D03              tst 3,x
 00F3 2652              bne L20
                       ;         {
                       ;             if (offtime < 0)
 00F5 FC0001            ldd _offtime
 00F8 2C06              bge L22
                       ;                 offtime = 0;
 00FA CC0000            ldd #0
 00FD FD0001            std _offtime
 0100                 L22:
                       ;             ontime -= fivePercent;
 0100 FC0003            ldd _ontime
 0103 8303E8            subd #1000
 0106 FD0003            std _ontime
                       ;             offtime += fivePercent;
 0109 FC0001            ldd _offtime
 010C C303E8            addd #1000
 010F FD0001            std _offtime
                       ;             
                       ;             PORTA |= 0x10;
 0112 18CE1000          ldy #0x1000
 0116 181C0010          bset 0,y,#16
                       ;             if ((PORTA & 0x20) != 0)
 011A 18CE1000          ldy #0x1000
 011E 181F002008        brclr 0,y,#32,L24
                       ;                 PORTA ^= 0x20;
                         ; vol
 0123 F61000            ldab 0x1000
 0126 C820              eorb #32
 0128 F71000            stab 0x1000
 012B                 L24:
                       ; 
                       ;             printf("ontime %d, offtime %d\n", ontime, offtime); //debugger
 012B FC0001            ldd _offtime
 012E 37                pshb
 012F 36                psha
 0130 FC0003            ldd _ontime
 0133 37                pshb
 0134 36                psha
 0135 CC01B9            ldd #L26
 0138 BD0000            jsr _printf
 013B 1838              puly
 013D 1838              puly
                       ;             printf("red PWM\n");
 013F CC01B0            ldd #L27
 0142 BD0000            jsr _printf
                       ;         }
 0145 2056              bra L21
 0147                 L20:
                       ;         else if (masterSignal == 0x01)
 0147 E603              ldab 3,x
 0149 C101              cmpb #1
 014B 2650              bne L28
                       ;         {
                       ;             if (ontime < 0)
 014D FC0003            ldd _ontime
 0150 2C06              bge L30
                       ;                 ontime = 0;
 0152 CC0000            ldd #0
 0155 FD0003            std _ontime
 0158                 L30:
                       ;             ontime += fivePercent;
 0158 FC0003            ldd _ontime
 015B C303E8            addd #1000
 015E FD0003            std _ontime
                       ;             offtime -= fivePercent;
 0161 FC0001            ldd _offtime
 0164 8303E8            subd #1000
 0167 FD0001            std _offtime
                       ;             
                       ;             PORTA |= 0x20;
 016A 18CE1000          ldy #0x1000
 016E 181C0020          bset 0,y,#32
                       ;             if ((PORTA & 0x10) != 0)
 0172 18CE1000          ldy #0x1000
 0176 181F001008        brclr 0,y,#16,L32
                       ;                 PORTA ^= 0x10;
                         ; vol
 017B F61000            ldab 0x1000
 017E C810              eorb #16
 0180 F71000            stab 0x1000
 0183                 L32:
                       ;             
                       ;             printf("ontime %d, offtime %d\n", ontime, offtime); //debugger          
 0183 FC0001            ldd _offtime
 0186 37                pshb
 0187 36                psha
 0188 FC0003            ldd _ontime
 018B 37                pshb
 018C 36                psha
 018D CC01B9            ldd #L26
 0190 BD0000            jsr _printf
 0193 1838              puly
 0195 1838              puly
                       ;             printf("inc PWM\n"); 
 0197 CC01A7            ldd #L34
 019A BD0000            jsr _printf
                       ;         }
 019D                 L28:
 019D                 L21:
 019D                 L15:
 019D 7E00DE            jmp L18
 01A0                 X1:
                       ; /*      else
                       ;         {
                       ;             printf("wrong input recieved");
                       ;         }*/
                       ;     }
                       ;               
                       ;     //endProg(); //4 debugging...
                       ; }
 01A0                 L13:
 01A0 08                inx
 01A1 08                inx
 01A2 08                inx
 01A3 08                inx
 01A4 35                txs
 01A5 38                pulx
 01A6                   .dbline 0 ; func end
 01A6 39                rts
                         .area bss
 0000                 _motorStatus::
 0000                   .blkb 1
 0001                 _offtime::
 0001                   .blkb 2
 0003                 _ontime::
 0003                   .blkb 2
                         .area text
 01A7                 L34:
 01A7 696E632050574D0A00     .byte 'i,'n,'c,32,'P,'W,'M,10,0
 01B0                 L27:
 01B0 7265642050574D0A00     .byte 'r,'e,'d,32,'P,'W,'M,10,0
 01B9                 L26:
 01B9 6F6E74696D652025642C206F66667469   .byte 'o,'n,'t,'i,'m,'e,32,37,'d,44,32,'o,'f,'f,'t,'i
 01C9 6D652025640A00    .byte 'm,'e,32,37,'d,10,0
