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:
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:
LIST P=18F97J60 ; directive to define processor
#include <P18F97J60.INC> ; processor specific variable definition
config XINST = OFF
config FOSC = HS
config WDT = OFF
ORG 0x0000
ResetV goto MAIN ; Skip over interrupt vectors
ORG 0x0008
HPISR
retfie
ORG 0x0018
LPISR
retfie
ORG 0x0100 ; Suggest starting address for codes
count equ 0x00
cblock 0x70
Dly1
Dly2
endc
MAIN
movlw 0x00 ; 0 for output, 1 for input
movwf TRISJ,0 ; Configure all Port J pins as output
movwf TRISD,0
movwf TRISC,0
movlw 0x62
movwf OSCCON ; 4mhz
;;;SET PWM FREQUENCY;;;
MOVLW D'128' ;SET PR2 TO 128 , PWM FREQUENCY = 484Hz
MOVWF PR2
;;;SET PWM STARTING DUTY CYCLE;;;
CLRF CCPR1L
MOVLW B'00001100' ;SET PWM MODE, BITS 5 AND 4 ARE THE TWO LSBs OF THE 10BIT DUTY CYCLE REGISTER (CCPR1L:CCP1CON<5:4>)
MOVWF CCP1CON ;1100 = PWM mode; PxA, PxC active-high; PxB, PxD active-high
;1101 = PWM mode; PxA, PxC active-high; PxB, PxD active-low
;1110 = PWM mode; PxA, PxC active-low; PxB, PxD active-high
;1111 = PWM mode; PxA, PxC active-low; PxB, PxD active-low
;;;SET PWM PIN TO OUTPUT MODE;;;
BCF TRISC, 2 ;SET RC2 AS OUTPUT, TO USE FOR PWM
;;;SET TIMER 2 PRESCALE VALUE;;;
;PRESCALE = 16 SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
MOVLW B'00000010'
MOVWF T2CON
;;;CLEAR TIMER 2 MODULE;;;
CLRF TMR2
;;;ENABLE TIMER 2 MODULE;;;
BSF T2CON, TMR2ON
bsf TRISB,TRISB0,0 ;init RB0 as input
bcf TRISJ,TRISJ0,0 ;init RJ0 as output
bcf TRISC,TRISC3,0 ;init RC3 as output
again:
call BUZZERON
call DELAY
call off
CALL DELAY
bra again
off: movlw 0x00
movwf LATC,0 ;
return
BUZZERON: movlw 0x0f; Set 0xff to lw reg
movwf LATC,0 ; This is Port C Output register
return
DELAYONE
movlw .255
movwf count
LOOP2
DECFSZ count,F
bra LOOP2
call BUZZERON
nop
return
DELAY
movlw 0x05 ; 0 for output, 1 for input
movwf 0x02,0 ;
LDELAY1 decf 0x00,1,0 ; Inner Loop [ 1 cy ]
bnz LDELAY1 ; [ 2 cy ]
decf 0x01,1,0 ; Outer Loop [ 1 cy ]
bnz LDELAY1 ; [ 2 cy ]
decf 0x02,1,0 ; Outer Loop [ 1 cy ]
bnz LDELAY1 ;
return
END ; No code beyond this line, absolutely.