PDA

View Full Version : PWM (ECCP) - piezo-sounder directly connected to half-bridge on PIC



flotulopex
- 5th October 2015, 20:31
Hi there,

I'm finishing my cooking-timer project and first time tried the PWM (ECCP) half-bridge functionality (when pin P1A is HIGH, P1B is LOW, next cycle inverts and so forth) on my PIC 16f690.

The piezo-sounder I have works best (= is the loudest) @ 4kHz.

The idea (not new at all) is to augment my piezo-sounder's loudness by inverting its power polarity @ 4kHz (kind of 10V peak-to-peak). As a matter of fact, it works very well :)

Since my piezo-sounder draws not more than 1,5mA, I have connected it directly to the respective PIC's pins P1A & P1B.

Is this way of doing okay or am I going to have a surprise in a certain period of time?

ruijc
- 6th October 2015, 07:44
Hello Roger,

I have done this many times ( because it works very well on getting a higher sound with 2 pins inverted ) and never had any problems.
The only difference is that I use a small resistor ( 100 ohms ) between the pin and the piezo ( one for each pin ).
The frequency depends on the piezo so it's best to test to see which is the optimal frequency ( as you did ).

Regards
Rui

fratello
- 6th October 2015, 10:35
augment ... piezo-sounder's loudness by inverting its power polarity @ 4kHz (kind of 10V peak-to-peak).
Please, can you post the code for this particular thing ? Thanks !

flotulopex
- 6th October 2015, 21:55
That's the code, well... it's just THE part of the cooking-timer code to make that "half-bridge" PWM work:

'-------------------------------------------------------------------------------
' PIC 16F690 Fuses (MPASM)
@ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF

'-------------------------------------------------------------------------------
' Registers 76543210
OPTION_REG = %10000110 ' Pull-Ups OFF, Prescaler TMR0 1:128
ANSEL = %00000000 ' Select analog inputs Channels 0 to 7
ANSELH = %00000001 ' Select analog inputs Channels 8 to 11
WPUA = %00000000 ' Select weak pull-ups
WPUB = %00000000 ' Select weak pull-ups
ADCON0 = %10000000 ' AD Module
ADCON1 = %00000000 ' AD control register
CM1CON0 = %00000000 ' Comparator1 Module
CM2CON0 = %00000000 ' Comparator2 Module
INTCON = %10100000 ' INTerrupts CONtrol; GIE=1, T0IE=1
TRISA = %00000000 ' Select Input/Output (0 to 5)
PORTA = %00000000 ' Set High/Low (0 to 5)
TRISB = %00110000 ' Select Input/Output (4 to 7)
PORTB = %00000000 ' Set High/Low (4 to 7)
TRISC = %01000000 ' Select Input/Output (0 to 7)
PORTC = %00000000 ' Set High/Low (0 to 7)

' HPWM registers
T2CON = %00000101 ' Timer2 = ON (bit2), prescaler (bits1:0) 00=1, 01=4, 1x=16
CCP1CON = %10001100 ' Select PWM Mode (%00001100 = PWM module ON)
CCPR1L = 62 ' Dutycycle set to 50% (= percentage of PR2)
PR2 = 124 ' Freq 4kHz

Ioannis
- 8th October 2015, 13:36
Just as a precaution I would recommend to have the outputs at the same logic level when you do not want the sounder work. Thats because piezo do not like DC voltage.

Ioannis

fratello
- 8th October 2015, 17:05
Thanks Mr.Flotulopex and Mr.Ioannis !
It's verry usefull.

flotulopex
- 9th October 2015, 05:38
...I would recommend to have the outputs at the same logic level...
When the souder is not used, I set CCP1CON register to 0.

Is this okay?

flotulopex
- 9th October 2015, 06:08
....hum, sorry for my silly question :o

Just measured the ports and they need to be reset.

Thanks for noticing Ioannis ;)

Ioannis
- 9th October 2015, 16:02
As you observed Roger, you must be in luck to have both ports at the same level. They will be in a random state.

Ioannis