PDA

View Full Version : Half-bridge PWM with a 16F684 ?



Byte_Butcher
- 17th January 2010, 18:15
I'm trying to set up the half-bridge PWM outputs on a 16F684. Not having much luck...

I've read section 11 of the data sheet (41202F) about a million times now. The individual words make sense to me... but I still can't seem to grasp the concept of how to make this thing work. :o

I *think* I've done the setup according to these instructions in the data sheet, but now I don't know what to do next to make it get up and run.

Here's the setup instructions from Data Sheet:
11.3.7 SETUP FOR PWM OPERATION

The following steps should be taken when configuring
the CCP module for PWM operation:

1. Disable the PWM pin (CCP1) output driver by
setting the associated TRIS bit.

2. Set the PWM period by loading the PR2 register.

3. Configure the CCP module for the PWM mode
by loading the CCP1CON register with the
appropriate values.

4. Set the PWM duty cycle by loading the CCPR1L
register and CCP1 bits of the CCP1CON register.

5. Configure and start Timer2:
• Clear the TMR2IF interrupt flag bit of the
PIR1 register.

• Set the Timer2 prescale value by loading the
T2CKPS bits of the T2CON register.

• Enable Timer2 by setting the TMR2ON bit of
the T2CON register.

6. Enable PWM output after a new PWM cycle has
started:

• Wait until Timer2 overflows (TMR2IF bit of
the PIR1 register is set).

• Enable the CCP1 pin output driver by clearing
the associated TRIS bit.


Here's the little bit of setup code I've got so far. Not sure what to do next.
Could one of you good folks please help point me in the right direction?


</i></font><b>INCLUDE </b><font color="#FF0000">&quot;MODEDEFS.BAS&quot; </font><font color="#000080"><i>'Include Shiftin/out modes
</i></font><b>DEFINE </b>OSC 8 <font color="#000080"><i>'8MHz clock

</i></font>TRISA= %00000000 <font color="#000080"><i>'Set 'em all to outputs
</i></font>TRISC= %00000000 <font color="#000080"><i>'Set 'em all to outputs
</i></font>ANSEL = %00000000 <font color="#000080"><i>'Turn off that analog crap
</i></font>OSCCON = %01110001 <font color="#000080"><i>'8MHz




</i></font>TRISC.5 = 1 <font color="#000080"><i>'disable CCP1
</i></font>PR2 = $65 <font color="#000080"><i>'load a value into PR2
</i></font>CCP1CON= %10001100 <font color="#000080"><i>'half bridge, all PWM outputs active high
</i></font>CCPR1L= %11111111 <font color="#000080"><i>'set the PWM duty cycle
</i></font>PIR1.1 = 0 <font color="#000080"><i>'clear the TMR2IF interrupt flag
</i></font>T2CON.0 = 0 <font color="#000080"><i>'set prescaler to 1...
</i></font>T2CON.1 = 0 <font color="#000080"><i>'...set prescaler to 1
</i></font>T2CON.2 = 1 <font color="#000080"><i>'turn timer 2 on
</i></font>TRISC.5 = 0 <font color="#000080"><i>'enable CCP1

'Now what?


</i></font><b>END
</b></code></pre><!--EndFragment--></body>
</html>

Any help would sure be appreciated..

Thanks!

steve

Archangel
- 17th January 2010, 18:32
Hi Steve, Darrel has some goodies here:
http://www.pbpgroup.com/modules/wfsection/index.php?category=2

Byte_Butcher
- 17th January 2010, 19:28
Thanks Joe,
Darrels stuff is always cool. But I don't see what I'm looking for there.

I'm trying to make the 16F684's internal "Half-Bridge" mode work, with it's adjustable dead-band.

steve

Archangel
- 17th January 2010, 19:43
MeLabs: http://www.melabs.com/resources/samples/pbp/767pwm.bas

Byte_Butcher
- 17th January 2010, 20:06
Hmm. That still doesn't get me there. What I'm after is to output 2 waveforms that are 180 degrees out of phase and have a delay or "dead band" where neither is on.
Looks like this:

http://sweetwatergems.weirdstuffwemake.com/electronics/images/half_bridge_b.gif


steve

Bruce
- 17th January 2010, 21:27
Steve,

What you have should work once you reduce CCPR1L < PR2. 51 in CCPR1L should give you
~50% duty cycle.

Bruce
- 17th January 2010, 21:58
Give this a shot;


@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF

DEFINE OSC 8 '8MHz clock

SYMBOL TMR2IF = PIR1.1 ' TMR1IF flag bit
TRISA = %00000000 ' Set 'em all to outputs
PORTC = 0
TRISC = %00110000 ' RC4 and RC5 left as inputs until ready
ANSEL = %00000000 ' Turn off that analog crap
CMCON0 = 7
OSCCON = %01110000 ' 8MHz internal

PR2 = 101 ' load a value into PR2 for ~19.6kHz
CCP1CON = %10001100 ' half bridge, all PWM outputs active high
CCPR1L = 50 ' set the PWM duty cycle ~50%
PWM1CON = %10000011' set auto restart & deadband
TMR2IF = 0
T2CON = %00000100 'set prescaler to 1, prescaler to 1, timer 2 on

WHILE TMR2IF = 0 ' wait for TMR2IF flag bit to set
WEND
TRISC = %11001111 ' now set RC4 & RC5 as outputs

Main:
GOTO Main

END

Byte_Butcher
- 17th January 2010, 22:18
Ah! Of course... CCPR1L must be < PR2 or it ain't gonna run.

Sometimes I just need someone to spin me around 2 or 3 times and pull the blindfold off my head. :)

It works great now. Thank you SO much!


steve