1. We will be in Read Only mode (no new threads, replies, registration) starting at 9:00 EDT for a number of hours as we migrate the forums to upgraded software.

Generating different tones with PIC18 Buzzer - Assembly

Discussion in 'Homework Help' started by jriko, Jul 27, 2017.

  1. jriko

    Thread Starter New Member

    Jan 2, 2016
    1
    0
    Hello everybody,
    I need to generate different tones for PIC18 written in assembly.
    The Buzzer is connected to PORT RC3 and I've managed to turn on and off the buzzer, However I'm having problems with generating different tones.

    I thought of using PWM to generate different sounds but I'm having problems on how exactly to use PWM in assembly.
    Any advice/help would be greatly appreciated.
    Thanks.

    Here is the code that I currently have:
    Code (Text):
    1.  
    2.         LIST P=18F97J60            ; directive to define processor
    3.         #include <P18F97J60.INC>    ; processor specific variable definition
    4.         config XINST = OFF
    5.         config FOSC = HS
    6.         config WDT = OFF
    7.        
    8.                
    9.         ORG        0x0000
    10. ResetV    goto    MAIN            ; Skip over interrupt vectors
    11.          
    12.  
    13.         ORG        0x0008
    14. HPISR  
    15.         retfie              
    16.  
    17.         ORG        0x0018
    18. LPISR
    19.         retfie
    20.  
    21.         ORG        0x0100    ; Suggest starting address for codes
    22.        
    23.    
    24.         count equ 0x00
    25.         cblock 0x70                
    26.         Dly1
    27.         Dly2
    28.        
    29.         endc
    30.  
    31. MAIN
    32.         movlw    0x00        ; 0 for output, 1 for input
    33.         movwf TRISJ,0    ; Configure all Port J pins as output
    34.         movwf TRISD,0
    35.         movwf TRISC,0
    36.        
    37.         movlw   0x62      
    38.         movwf   OSCCON ; 4mhz
    39.        
    40.         ;;;SET PWM FREQUENCY;;;
    41.    
    42.     MOVLW D'128' ;SET PR2 TO 128 , PWM FREQUENCY = 484Hz
    43.     MOVWF PR2
    44.    
    45.        
    46.     ;;;SET PWM STARTING DUTY CYCLE;;;
    47.     CLRF CCPR1L
    48.  
    49.     MOVLW B'00001100' ;SET PWM MODE, BITS 5 AND 4 ARE THE TWO LSBs OF THE 10BIT DUTY CYCLE REGISTER (CCPR1L:CCP1CON<5:4>)
    50.     MOVWF CCP1CON       ;1100 = PWM mode; PxA, PxC active-high; PxB, PxD active-high
    51.                ;1101 = PWM mode; PxA, PxC active-high; PxB, PxD active-low
    52.                ;1110 = PWM mode; PxA, PxC active-low; PxB, PxD active-high
    53.                ;1111 = PWM mode; PxA, PxC active-low; PxB, PxD active-low
    54.    
    55.     ;;;SET PWM PIN TO OUTPUT MODE;;;
    56.    
    57.     BCF TRISC, 2 ;SET RC2 AS OUTPUT, TO USE FOR PWM
    58.  
    59.    
    60.     ;;;SET TIMER 2 PRESCALE VALUE;;;
    61.     ;PRESCALE = 16 SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
    62.     MOVLW B'00000010'
    63.     MOVWF T2CON
    64.    
    65.    
    66.     ;;;CLEAR TIMER 2 MODULE;;;
    67.     CLRF TMR2
    68.    
    69.     ;;;ENABLE TIMER 2 MODULE;;;
    70.     BSF T2CON, TMR2ON
    71.  
    72.         bsf TRISB,TRISB0,0 ;init RB0 as input
    73.         bcf TRISJ,TRISJ0,0 ;init RJ0 as output
    74.         bcf TRISC,TRISC3,0 ;init RC3 as output
    75.    
    76. again:  
    77.     call BUZZERON
    78.     call DELAY
    79.     call off
    80.     CALL DELAY
    81.  
    82.        
    83.     bra again
    84.        
    85.        
    86. off:          movlw 0x00
    87.         movwf LATC,0 ;
    88.         return
    89.        
    90.  
    91.        
    92. BUZZERON:      movlw    0x0f; Set 0xff to lw reg
    93.         movwf    LATC,0    ; This is Port C Output register
    94.         return
    95.        
    96.        
    97.        
    98. DELAYONE
    99.     movlw   .255
    100.     movwf   count
    101.  
    102.     LOOP2
    103.          DECFSZ   count,F  
    104.  
    105.     bra  LOOP2
    106.     call BUZZERON
    107.     nop
    108.     return
    109.      
    110.        
    111. DELAY  
    112.         movlw    0x05        ; 0 for output, 1 for input
    113.         movwf    0x02,0    ;      
    114.    
    115.        
    116. LDELAY1    decf    0x00,1,0    ; Inner Loop         [ 1 cy ]
    117.         bnz   LDELAY1    ;                   [ 2 cy ]
    118.         decf  0x01,1,0    ; Outer Loop         [ 1 cy ]
    119.         bnz    LDELAY1    ;                       [ 2 cy ]
    120.         decf  0x02,1,0    ; Outer Loop         [ 1 cy ]
    121.         bnz    LDELAY1    ;  
    122.         return
    123.        
    124.  
    125.         END            ; No code beyond this line, absolutely.
    126.  
    127.  
     
Loading...