Entire Forum This board This topic Members Entire Site
 Pages: [1]
 Author Topic: Setup 16F690 (or another PIC) for Full-bridge PWM (P-N channel mosfets) - How to  (Read 2760 times) 0 Members and 1 Guest are viewing this topic.
thunderer
Junior Member

Offline

Posts: 56

Thank You
-Given: 19

I try to be patient

 « on: February 10, 2013, 06:15:17 18:15 »

Note 1: code written for Mikroelektronika MikroBasic PRO for PIC

Note 2: any other PIC with hardware PWM module can be setup as listed below.

H-bridge hardware setup (see page 137 of the 16F690 datasheet)

How to calculate PWM frequency? (see page 131 of the 16F690 datasheet)
The formula is
PWM period = (PR2+1)*4*TMR2prescale value / Fosc

Where:
PR2 = 0 to 255 (0x00 to 0XFF)
TMR2prescale value = 1, 4 or 16
Fosc = PIC frequency in Hz (not MHz!).

Example:
PR2 = 0xFF (=255)
TMR2prescale value = 16
Fosc = 20,000,000Hz.

Result:
Fpwm = 1,220.703Hz

Want 20kHz? Change PR2 = 15 and the resulted Fpwm = 19,531.250Hz

Setup the PWM in the PIC for Full-bridge (4 pins already designated will be allocated to the H-bridge driving)

Initialise PWM
Code:
CCP1CON =   0X4E               ' CCP ON mode, set in Forward (chosen as default)
' P-N channel H-bridge: P1A/C active "L", modulated P1B/D active "H"
CCPR1L    =   0                    ' Set DC = 0%
T2CON     =   0x05               ' TMR2 ON, 1:4 prescaler
PR2         =   0xFA               ' f=498.008Hz (!!!Using a 2MHz Fosc!!!)
TMR2       =   0                   ' clear TMR2

Explanation CCP1CON (under the bit number is the choice):
bit------7-----6-----5-----4-----3-----2-----1-----0
Values:
--------0-----1 Forward Mode OR
--------1-----1 Reverse Mode
---------------------x-----x LSb of CCPR1L (linked to the Duty Cycle = DC)
----------------------------------1-----1-----0-----0 Setup for N channel H-bridge
----------------------------------1-----1-----1-----0 Setup for P-N channel H-bridge

Setup for direction and DC update:
REVERSE

Code:
CCPR1L = "value"                   ' Set DC = calculated value
SetBit(CCP1CON,7)                ' Put the H-bridge in Reverse mode

FORWARD
Code:
CCPR1L = "value"                   ' Set DC = calculated value
ClearBit(CCP1CON,7)              ' Put the H-bridge in Forward mode

STOP
Code:
CCPR1L = 0x00                     ' Set DC = 0%
ClearBit(CCP1CON,7)             ' Put the H-bridge in Forward mode
' you can omit this, as it makes no difference where the H-bridge is at 0%

NB: this post is made because I considered this subject may be useful for others (including me, I may not master completely the PWM  ). Hope you'll appreciate and provide comments as needed, or ask questions if any.

NB2: yes, for those that will say they've seen this post someplace else, it is the same me that made that post.
 « Last Edit: February 10, 2013, 06:17:19 18:17 by thunderer » Logged

Interested and hopefully helpful in: DC brushed motor control (mainly R/C - PPM/PWM), analog audio, PIC (mikrobasic PRO). Feel free to ask, and if I can, I will help. But only on forum topics, any started private conversation will continue in a public topic.
 Pages: [1]