Driving a piezo with pwm

Status
Not open for further replies.

Mosaic

Well-Known Member
Hi all:
I want to drive a piezo (9khz resonant) with a pwm pin in a 16f882.

I plan to use it for alarms and for button press beeps/chirps.

So far, It seems I have to set up prescale/Tmr2/PR2 registers to dictate the frequency of the PWM.

I am not sure what the necessary PW is though. would 50% work? Also, How do I switch on and off the PWM signal, by TRIS?

Lastly, are there elegant ways to map a couple bits from one register to another?

If I want to map bits 2,3 in a register to bits 6,7 in another register, there should be a simple elegant way to do this without btfss/bsf/bcf structures? Some combination of logical operations & bitmasking?
 
I believe that setting and clearing T2CON,TMR2ON will start and stop the PWM. I would start with 50% and use trial and error to find the ideal value.

For mapping bits you could use rlf etc. but for two bits it's quicker to use bsf etc.

Mike.
 
I am still learning asm, but wouldn't it be possible to load W with the first register, then use swapf, then copy it back it the second register.
 
Something like this then?
Code:
;
;  copy source[b3:b2] to target[b7:b6]
;
        movlw   b'00111111'     ;
        andwf   target,F        ; clear target b7:b6
        swapf   source,W        ;
        andlw   b'11000000'     ;
        iorwf   target,F        ;
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…