                        .module Speaker.c
                        .area memory(abs)
                        .org 0x3e64
 3E64           _IRQ_vector::
 3E64 00B8              .word _OC5_ISR
                        .area data
 0000           _score::
 0000                   .blkb 2
                        .area idata
 0000 0D51              .word 3409
                        .area data
 0002                   .blkb 2
                        .area idata
 0002 09FA              .word 2554
                        .area data
 0004                   .blkb 2
                        .area idata
 0004 0D51              .word 3409
                        .area data
 0006                   .blkb 2
                        .area idata
 0006 0B33              .word 2867
                        .area data
 0008                   .blkb 2
                        .area idata
 0008 0D51              .word 3409
                        .area data
 000A           _dur::
 000A                   .blkb 2
                        .area idata
 000A 0014              .word 20
                        .area data
 000C                   .blkb 2
                        .area idata
 000C 0014              .word 20
                        .area data
 000E                   .blkb 2
                        .area idata
 000E 0014              .word 20
                        .area data
 0010                   .blkb 2
                        .area idata
 0010 0014              .word 20
                        .area data
 0012                   .blkb 2
                        .area idata
 0012 0014              .word 20
                        .area data
                        .area text
 0000           _main::
 0000 2003              bra L5
 0002           L4:
 0002 160008            jsr _Speaker
 0005           L5:
 0005           ; /*
 0005           ; Matt Hobson
 0005           ; ELEN 599
 0005           ; Spring 2006
 0005           ; File Name: Speaker.c
 0005           ; File Purpose: Initialize PTT (Timer) channel 5 for Output Compare to the 
 0005           ;               speaker aboard the MiniDragon+, and play a tune.  Timer channel
 0005           ; 			  0 serves as a delay counter.
 0005           ; 
 0005           ; 			  
 0005           ; modified by Dan Kohn
 0005           ; 
 0005           ; Made a stand alone program.			  
 0005           ; */
 0005           ; 
 0005           ; //********************************** INCLUDE ***********************************
 0005           ; 
 0005           ; #include <mc9s12dp256.h>
 0005           ; 
 0005           ; //********************************** DEFINE ************************************
 0005           ; 
 0005           ; #define  G3  7653
 0005           ; #define  B3  6074
 0005           ; #define  C4	 5733
 0005           ; #define  C4S 5412
 0005           ; #define  D4	 5108
 0005           ; #define  E4	 4551
 0005           ; #define  F4	 4295
 0005           ; #define  F4S 4054
 0005           ; #define  G4	 3827
 0005           ; #define  A4	 3409
 0005           ; #define  B4F 3218
 0005           ; #define  B4	 3037
 0005           ; #define  C5	 2867
 0005           ; #define  D5	 2554
 0005           ; #define  E5	 2275
 0005           ; #define  F5	 2148
 0005           ; #define  notes  6
 0005           ; #define  toggle 0x04
 0005           ; 
 0005           ; //General 
 0005           ; #define CLI asm("CLI")
 0005           ; #define SEI asm("SEI")
 0005           ; //******************************* PROTOTYPES ***********************************
 0005           ; 
 0005           ; void delayby10ms(int);  //Prototype for OC0 delay function
 0005           ; void OC5_ISR(void);
 0005           ; 
 0005           ; 
 0005           ; //OC5 Interrupt Setup
 0005           ; void OC5_ISR();
 0005           ; #pragma interrupt_handler OC5_ISR()
 0005           ; #pragma abs_address: 0x3E64
 0005           ; void (*IRQ_vector[])()={OC5_ISR};
 0005           ; #pragma end_abs_address
 0005           ; 
 0005           ; //******************************* VARIABLES ************************************
 0005           ; 
 0005           ; int j;
 0005           ; int i;
 0005           ; int dela;
 0005           ; unsigned int score[5] = {A4,D5,A4,C5,A4};
 0005           ; unsigned int dur[5] = {20,20,20,20,20};
 0005           ; 
 0005           ; //**************************** Test Main ***************************************
 0005           ; void main(void)
 0005           ; {
 0005           ;    while(1)
 0005 20FB              bra L4
 0007           X0:
 0007           ;    {
 0007           ;    Speaker();
 0007           ;    }
 0007           ; }
 0007           L3:
 0007                   .dbline 0 ; func end
 0007 3D                rts
 0008           _Speaker::
 0008           ;    
 0008           ; 
 0008           ; //**************************** SPEAKER FUNCTION ********************************
 0008           ; 
 0008           ; void Speaker (void)
 0008           ; {
 0008           ;    TSCR1 = 0x90;   //Enable TCNT, fast timer flag clear
 0008 C690              ldab #144
 000A 7B0046            stab 0x46
 000D           ;    TSCR2 = 0x03;   //Set TCNT prescaler to 8
 000D C603              ldab #3
 000F 7B004D            stab 0x4d
 0012           ;    TFLG1 = 0xFF;   //Clear all TxF flags
 0012 C6FF              ldab #255
 0014 7B004E            stab 0x4e
 0017           ;    TIE  |= 0x20;   //Enable TC5 interrupt
 0017 1C004C20          bset 0x4c,#32
 001B           ;    TIOS |= 0x20;   //Enable OC5 function
 001B 1C004020          bset 0x40,#32
 001F           ;    TCTL1 = toggle; //Select toggle option as OC5 pin action
 001F C604              ldab #4
 0021 7B0048            stab 0x48
 0024           ;    CLI;          //Global enable of interrupts
 0024 10EF                      CLI
 0026           
 0026           ;    j = 0;
 0026 CC0000            ldd #0
 0029 7C0004            std _j
 002C           ;    dela = score[0];
 002C FC0000            ldd _score
 002F 7C0000            std _dela
 0032           ;    TC5 = TCNT + dela;      //Play the first note
 0032                   ; vol
 0032 FC0044            ldd 0x44
 0035 F30000            addd _dela
 0038 7C005A            std 0x5a
 003B 2025              bra L9
 003D           L8:
 003D FC0004            ldd _j
 0040 59                lsld
 0041 C30000            addd #_score
 0044 B7C6              xgdy
 0046 EC40              ldd 0,y
 0048 7C0000            std _dela
 004B FC0004            ldd _j
 004E 59                lsld
 004F C3000A            addd #_dur
 0052 B7C6              xgdy
 0054 EC40              ldd 0,y
 0056 160077            jsr _delayby10ms
 0059 FC0004            ldd _j
 005C C30001            addd #1
 005F 7C0004            std _j
 0062           L9:
 0062           ;    while(j < notes)
 0062 FC0004            ldd _j
 0065 8C0006            cpd #6
 0068 2DD3              blt L8
 006A           ;    {
 006A           ;       dela = score[j];     //Play the jth note
 006A           ;         delayby10ms(dur[j]);
 006A           ;         j++;
 006A           ;    }
 006A           ;    TIE  &= ~0x20;   //Disable TC5 interrupt
 006A 1D004C20          bclr 0x4c,#0x20
 006E           ;    TSCR1 = 0x00;   //Turn off the Timer System
 006E 790046            clr 0x46
 0071           ;    PTT = 0x00;     //Blank Port T
 0071 790240            clr 0x240
 0074           ;    SEI;          //Disable interrupts
 0074 1410                      SEI
 0076           
 0076           ; }
 0076           L7:
 0076                   .dbline 0 ; func end
 0076 3D                rts
 0077           ;             kk -> 2,x
 0077           _delayby10ms::
 0077 3B                pshd
 0078 34                pshx
 0079 B775              tfr s,x
 007B           ; 
 007B           ; //************************** DELAYBY10MS FUNCTION ******************************
 007B           ; 
 007B           ; void delayby10ms(int kk)
 007B           ; {
 007B           ;    TIOS |= 0x80;  //Enable OC7
 007B 1C004080          bset 0x40,#128
 007F           ;    TC7 = TCNT + 30000;  //Start one OC7 operation
 007F                   ; vol
 007F FC0044            ldd 0x44
 0082 C37530            addd #30000
 0085 7C005E            std 0x5e
 0088           ;    for(i=0;i<kk;i++)
 0088 CC0000            ldd #0
 008B 7C0002            std _i
 008E 2017              bra L15
 0090           X1:
 0090           ;    {
 0090           L16:
 0090           L17:
 0090 1F004E80FB        brclr 0x4e,#128,L16
 0095                   ; vol
 0095 FC005E            ldd 0x5e
 0098 C37530            addd #30000
 009B 7C005E            std 0x5e
 009E           L13:
 009E FC0002            ldd _i
 00A1 C30001            addd #1
 00A4 7C0002            std _i
 00A7           L15:
 00A7 FC0002            ldd _i
 00AA AC02              cpd 2,x
 00AC 2DE2              blt L17
 00AE           ;       while(!(TFLG1 & 0x80));
 00AE           ;         TC7 += 30000;
 00AE           ;    }
 00AE           ;    TIOS &= ~0x80;
 00AE 1D004080          bclr 0x40,#0x80
 00B2           ; }   
 00B2           L11:
 00B2 B757              tfr x,s
 00B4 30                pulx
 00B5 1B82              leas 2,sp
 00B7                   .dbline 0 ; func end
 00B7 3D                rts
 00B8           _OC5_ISR::
 00B8           ;     
 00B8           ; //******************************** OC5_ISR *************************************
 00B8           ; 
 00B8           ; #pragma interrupt_handler OC5_ISR()
 00B8           ; void OC5_ISR ()
 00B8           ; {
 00B8           ;    TC5 += dela;
 00B8           	; vol
 00B8 FC005A            ldd 0x5a
 00BB F30000            addd _dela
 00BE 7C005A            std 0x5a
 00C1           ; }
 00C1           L19:
 00C1                   .dbline 0 ; func end
 00C1 0B                rti
                        .area bss
 0000           _dela::
 0000                   .blkb 2
 0002           _i::
 0002                   .blkb 2
 0004           _j::
 0004                   .blkb 2
