title: How do I set up the speaker on a HCS12 microcontroller?
body: <p>Basically, my microcontroller is supposed to play a sound based on the push buttons (PTH) I press and light up the LED (PORTB). What I am stuck on is how to set up the speaker to output sound when the push buttons are pressed. A lot of the code is based off code from here: <a href="http://www.microdigitaled.com/HCS12/Hardware/Dragon12-Plus-Support.htm" rel="nofollow">http://www.microdigitaled.com/HCS12/Hardware/Dragon12-Plus-Support.htm</a></p>
  
  <p>Specifically the link about the buzzer. Also when I use the code provided I am able to hear sound coming from my microcontroller; however not when I try to include the push button into the code. </p>
  
  <p>I would greatly appreciate any hints, comments, tips, or suggestions. Thank you so much!</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>;----------------------USE $1000-$2FFF for Scratch Pad
  R1      EQU     $1001
  R2      EQU     $1002
  R3      EQU     $1003
  R4      EQU     $1004
  
  ;code section
      ORG   $2000     ;Flash ROM address for Dragon12+
  Entry:
        LDS     #$2000    ;Stack
        BSET    DDRT,%00100000     ;PTT5 as Output pin for buzzer
        BSET    DDRP,#$0F   ; Set Port P pins 0-3 to output
        BSET    PTP, #$0F     ; Disable 7-Segment Display
        ; LED
        BSET    DDRB,$FF
        BSET    DDRJ,$02
        BCLR    PTJ,$02
  
        ; PBs
        BCLR    DDRH,$0F
        BCLR    PTH,$FF       ;PORTB as Output
  
  ;-------Sound the Buzzer at PTT5
  
  BACK          
  ;-------Get data from DIP switches connected to PORTH and send it to LEDs of PORTB
  
   ;how is the speaker supposed to be set up? because when I try the code the speaker does not output any sound.
  
        LDAA PTH        ;Get data from DIP Switches of PTH
        STAA PORTB      ;and send it to PORTB
        BSET PTT,%00100000     ;PTT5=1
        JSR DELAY        
        BCLR PTT,%00100000     ;PTT5=0
        JSR DELAY        
        BRA BACK              ;Keep toggling buzzer    
  </code></pre>
  
  <p>Is my delay okay? Am I on the right track or is there a more convenient way of changing the notes?</p>
  
  <pre><code>;----------DELAY
  DELAY
      COMA
      TSTA
      CMPA    #$01
      BNE     no_note1
      PSHA        ;Save Reg A on Stack
      LDAA    #7        ;Change this value to hear  
      STAA    R3        ;different Buzzer sounds
  ;--1 msec delay. The Serial Monitor works at speed of 48MHz with XTAL=8MHz on Dragon12+ board
  ;Freq. for Instruction Clock Cycle is 24MHz (1/2 of 48Mhz).
  ;(1/24MHz) x 10 Clk x240x10=1 msec. Overheads are excluded in this calculation.       
  L3      LDAA    #10
      STAA    R2    
  L2      LDAA    #200
      STAA    R1
  L1      NOP         ;1 Intruction Clk Cycle
      NOP         ;1
      NOP         ;1
      DEC     R1  ;4
      BNE     L1  ;3
      DEC     R2  ;Total Instr.Clk=10
      BNE     L2
      DEC     R3
      BNE     L3
      PULA            ;Restore Reg A
  no_note1:        
      CMPA    #$02
      BNE     no_note2
      PSHA        ;Save Reg A on Stack
      LDAA    #3        ;Change this value to hear  
      STAA    R3        ;different Buzzer sounds
  ;--1 msec delay. The Serial Monitor works at speed of 48MHz with XTAL=8MHz on Dragon12+ board
  ;Freq. for Instruction Clock Cycle is 24MHz (1/2 of 48Mhz).
  ;(1/24MHz) x 10 Clk x240x10=1 msec. Overheads are excluded in this calculation.       
  L9        LDAA    #10
      STAA    R2    
  L8        LDAA    #210
      STAA    R1
  L7      NOP         ;1 Intruction Clk Cycle
      NOP         ;1
      NOP         ;1
      DEC     R1  ;4
      BNE     L7  ;3
      DEC     R2  ;Total Instr.Clk=10
      BNE     L8
      DEC     R3
      BNE     L9
      PULA            ;Restore Reg A
  no_note2:
      CMPA    #$04
      BNE     no_note3
      PSHA        ;Save Reg A on Stack
      LDAA    #2        ;Change this value to hear  
      STAA    R3        ;different Buzzer sounds
  ;--1 msec delay. The Serial Monitor works at speed of 48MHz with XTAL=8MHz on Dragon12+ board
  ;Freq. for Instruction Clock Cycle is 24MHz (1/2 of 48Mhz).
  ;(1/24MHz) x 10 Clk x240x10=1 msec. Overheads are excluded in this calculation.       
  L4        LDAA    #15
      STAA    R2    
  L5        LDAA    #240
      STAA    R1
  L6      NOP         ;1 Intruction Clk Cycle
      NOP         ;1
      NOP         ;1
      DEC     R1  ;4
      BNE     L6  ;3
      DEC     R2  ;Total Instr.Clk=10
      BNE     L5
      DEC     R3
      BNE     L4
      PULA            ;Restore Reg A
  no_note3:        
      CMPA    #$08
      BNE     no_note4
      PSHA        ;Save Reg A on Stack
      LDAA    #9        ;Change this value to hear  
      STAA    R3        ;different Buzzer sounds
  ;--1 msec delay. The Serial Monitor works at speed of 48MHz with XTAL=8MHz on Dragon12+ board
  ;Freq. for Instruction Clock Cycle is 24MHz (1/2 of 48Mhz).
  ;(1/24MHz) x 10 Clk x240x10=1 msec. Overheads are excluded in this calculation.       
  L30     LDAA    #16
      STAA    R2    
  L20     LDAA    #249
      STAA    R1
  L10     NOP         ;1 Intruction Clk Cycle
      NOP         ;1
      NOP         ;1
      DEC     R1  ;4
      BNE     L10  ;3
      DEC     R2  ;Total Instr.Clk=10
      BNE     L20
      DEC     R3
      BNE     L30
  ;--------------        
      PULA            ;Restore Reg A
  no_note4:
      RTS
  ;-------------------
  
  ;----------------------------------------------------
  ; Interrupt Vector
  ;----------------------------------------------------
        ; Port H Vector
        ; $3E4C = $FFCC - $C180
        ORG     $3E4C
        FDB     DELAY
  FINISH:
  NOP
      END
  </code></pre>