This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51:how to write asm code for PWM speaker?

i am a noob and learning 8051 coding.
i want to know how can i use PWM to write a speaker which can emit diff sound (do,re,mi,etc.) by using 2 pins only.
thanks.

Parents Reply Children
  • his 'advice' has always been questionable, now his English(?) is unreadable as well.

  • HI.

    i no people will complain bout my style but here is code with my comments

    
    ;===================================================
    ;
    ; Name       : DoNote
    ;
    ; Purpose    : Produce a sound on P1.0
    ;
    ; Parameters : int           Cycles (R6:R7)
    ;              unsigned char Delay  (R5)
    ;
    ; Result     : void
    ;
    ;===================================================
    
    
    ?PR?DoNote?sound segment code
    
              rseg ?PR?DoNote?sound
    
    DoNote:
    
              clr   a            ; Get 0-'Cycles'
              clr   c            ; Bcoz code uses up counter
              subb  a,r7         ; And store to dptr
              mov   dpl,a
              clr   a
              subb  a,r6
              mov   dph,a
    
              mov   dpl,a
    
    Dn_1:     cpl   p1.0         ; Toggle the port pin
    
              mov   a,r5         ; Get 'Delay'
              mov   r2,a         ; Loop for 'Delay'
              djnz  R2,$
    
              inc   dptr         ; Update cycle counter
              mov   a,dpl        ; While count is not overflowed
              or    a,dph
              jnz   Dn_1         ; Loop
    
              ret
    

    in yo're c do this

    extern void DoNote ( int Cycles, char delay );
    
    
    void DoNote ( void ) { DoNote ( }

    so there is a donation more than others.

    Always yo're freind.

    Zeusti.

  • HI.

    two many things to do here!

    extern void DoNote ( int Cycles, char delay );
    
    void foo ( void )
    {
      DoNote ( 10000, 10 );
    }
    

    Always yo're freind.

    Zeusti.

  • Zeusti,

    I'll make no comments about your comments, but I noticed a little error in your code:

    ;===================================================
    ;
    ; Name       : DoNote
    ;
    ; Purpose    : Produce a sound on P1.0
    ;
    ; Parameters : int           Cycles (R6:R7)
    ;              unsigned char Delay  (R5)
    ;
    ; Result     : void
    ;
    ;===================================================
    
    
    ?PR?DoNote?sound segment code
    
              rseg ?PR?DoNote?sound
    
    DoNote:
    
              clr   a            ; Get 0-'Cycles'
              clr   c            ; Bcoz code uses up counter
              subb  a,r7         ; And store to dptr
              mov   dpl,a
              clr   a
              subb  a,r6
              mov   dph,a
    
    ;          mov   dpl,a    <<< I think this should not be here
    
    Dn_1:     cpl   p1.0         ; Toggle the port pin
    
              mov   a,r5         ; Get 'Delay'
              mov   r2,a         ; Loop for 'Delay'
              djnz  R2,$
    
              inc   dptr         ; Update cycle counter
              mov   a,dpl        ; While count is not overflowed
              or    a,dph
              jnz   Dn_1         ; Loop
    
              ret
    

  • HI.

    you are write. sorry.

    so so many things to do here!

    Always yo're freind.

    Zeusti.

  • thanks all very much.
    i can now handle the software part of my purpose,
    but for the hardware, i've got some difficulty.
    i dont know how to amplify the sound of the 8ohm speaker.
    should i use any op-amp?
    or it can be done by varying the voltage?

  • This has nothing to do with the 8051 or Keil tools.

    You need some books on basic electronics!

    eg, www.kpsec.freeuk.com/trancirc.htm

  • HI.

    the page from master neil is good.

    look at the para "Choosing a suitable NPN transistor"

    u can simly have the speeker for the load.

    Always yo're freind.

    Zeusti.