                         .module pwmcalc.c
                         .area data
 0000                 _T_counts::
 0000                   .blkb 4
                         .area idata
 0000 000007D0          .word 0,2000
                         .area data
 0004                 _per_on::
 0004                   .blkb 4
                         .area idata
 0004 0000004B          .word 0,75
                         .area data
                         .area text
 0000                 _main::
                       ; /* C Listing 11.4 */
                       ; 
                       ; #include <hc11.h>   /* Include register definitions */
                       ; 
                       ; /* Declarations */
                       ; unsigned int long OC2hi, OC2lo; /* Duty cycle data */
                       ; unsigned int long T_counts=0x7d0, per_on=75;
                       ; 
                       ; 
                       ; /* Prototypes */
                       ; void initoc2(void);
                       ; void rtoc2(void);
                       ; void calc(void);
                       ; 
                       ; void main(void)
                       ; {
                       ;     INTR_OFF();
 0000 0F                        sei
                       
                       ;     /* set up jump vector for buffalo */
                       ;     asm("   ldaa #$7e");
 0001 867E                      ldaa #$7e
                       
                       ;     asm("   staa $dc");
 0003 97DC                      staa $dc
                       
                       ;     asm("   ldd #_rtoc2");
 0005 CC002C                    ldd #_rtoc2
                       
                       ;     asm("   std $dd");
 0008 DDDD                      std $dd
                       
                       ; 
                       ;     initoc2();
 000A BD0015            jsr _initoc2
 000D 2003              bra L5
 000F                 L4:
 000F BD007C            jsr _calc
 0012                 L5:
                       ; 
                       ;     while(1)
 0012 20FB              bra L4
 0014                 X0:
                       ;     {
                       ;           calc();
                       ;     }         /* Loop forever */
                       ; }
 0014                 L3:
 0014                   .dbline 0 ; func end
 0014 39                rts
 0015                 _initoc2::
                       ; 
                       ; void initoc2(void)
                       ; /* Initialize OC2 */
                       ; {
                       ;     TOC2 = TCNT;
                         ; vol
 0015 FC100E            ldd 0x100e
 0018 FD1018            std 0x1018
                       ;     TCTL1 = 0xC0;
 001B C6C0              ldab #192
 001D F71020            stab 0x1020
                       ;     TFLG1 = 0x40;
 0020 C640              ldab #64
 0022 F71023            stab 0x1023
                       ;     TMSK1 = 0x40;
 0025 C640              ldab #64
 0027 F71022            stab 0x1022
                       ;     INTR_ON();
 002A 0E                        cli
                       
                       ; }
 002B                 L7:
 002B                   .dbline 0 ; func end
 002B 39                rts
                       ;  lreg1 -> -4,x
                       ;  lreg2 -> -8,x
                       ;  IX -> 0,x
 002C                 _rtoc2::
 002C BD0000            jsr __enterb
 002F 82                .byte 0x82
                       ; 
                       ; void rtoc2(void)
                       ; /* Interrupt Service Routine for OC2 PWM */
                       ; {
                       ;     if ((PORTA & 0x40) == 0) 
 0030 18CE1000          ldy #0x1000
 0034 181F004002        brclr 0,y,#64,X1
 0039 2018              bra L9
 003B                 X1:
                       ;         TOC2 = TOC2 + OC2lo;
 003B 18CE0000          ldy #_OC2lo
 003F BD0000            jsr __ly2reg2
                         ; vol
 0042 FC1018            ldd 0x1018
 0045 BD0000            jsr __ud2lreg
 0048 BD0000            jsr __ladd
 004B BD0000            jsr __lreg2d
 004E FD1018            std 0x1018
 0051 2016              bra L10
 0053                 L9:
                       ;     else
                       ;         TOC2 = TOC2 + OC2hi;
 0053 18CE0004          ldy #_OC2hi
 0057 BD0000            jsr __ly2reg2
                         ; vol
 005A FC1018            ldd 0x1018
 005D BD0000            jsr __ud2lreg
 0060 BD0000            jsr __ladd
 0063 BD0000            jsr __lreg2d
 0066 FD1018            std 0x1018
 0069                 L10:
                       ;     TCTL1 = TCTL1 ^ 0x40;   /* invert OL2 to toggle next OC2 edge */
                         ; vol
 0069 F61020            ldab 0x1020
 006C C840              eorb #64
 006E F71020            stab 0x1020
                       ;     TFLG1 = 0x40;   /* Clear flag OC2F */
 0071 C640              ldab #64
 0073 F71023            stab 0x1023
                       ;     asm("   rti");  /* This is an interrupt service routine */
 0076 3B                        rti
                       
                       ; }
 0077                 L8:
 0077 08                inx
 0078 08                inx
 0079 35                txs
 007A 38                pulx
 007B                   .dbline 0 ; func end
 007B 39                rts
                       ;  lreg1 -> -4,x
                       ;  lreg2 -> -8,x
                       ;  IX -> 0,x
 007C                 _calc::
 007C BD0000            jsr __enterb
 007F 82                .byte 0x82
                       ; 
                       ; 
                       ; void calc(void)
                       ; {   
                       ;     OC2hi=(T_counts * per_on)/100;
 0080 18CE0000          ldy #_T_counts
 0084 BD0000            jsr __ly2reg
 0087 18CE0004          ldy #_per_on
 008B BD0000            jsr __ly2reg2
 008E BD0000            jsr __lmul
 0091 18CE00BF          ldy #L12
 0095 BD0000            jsr __ly2reg2
 0098 BD0000            jsr __ludiv
 009B 18CE0004          ldy #_OC2hi
 009F BD0000            jsr __lreg2y
                       ;     OC2lo=T_counts - OC2hi;
 00A2 18CE0000          ldy #_T_counts
 00A6 BD0000            jsr __ly2reg
 00A9 18CE0004          ldy #_OC2hi
 00AD BD0000            jsr __ly2reg2
 00B0 BD0000            jsr __lsub
 00B3 18CE0000          ldy #_OC2lo
 00B7 BD0000            jsr __lreg2y
                       ; }
 00BA                 L11:
 00BA 08                inx
 00BB 08                inx
 00BC 35                txs
 00BD 38                pulx
 00BE                   .dbline 0 ; func end
 00BE 39                rts
                         .area bss
 0000                 _OC2lo::
 0000                   .blkb 4
 0004                 _OC2hi::
 0004                   .blkb 4
                         .area text
 00BF                 L12:
 00BF 00000064          .word 0,100
