CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM generation using PIC 16F877A MCU
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PWM generation using PIC 16F877A MCU
PostPosted: Mon Mar 31, 2014 11:21 am     Reply with quote

Hey,
I wanted to trigger four mosfet to create a DC-DC converter circuit. The pulses must be 10 micro seconds apart and the frequency must be 50Khz. However, when i tried it showed only around 42Khz in the proteus simulation. I have programmed it in such a way as to draw four PWM sgnals from the pins 19 to 22. Please suggest any way to make the PWM osciilate at 50Khz with 50% duty cycle. I am attaching the code:
Code:

#if defined(__PCB__)
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#elif defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

void main()
{

while(1)
  {
   output_d(0x55);
   delay_us(10);
   output_d(0x00);
   delay_us(2);
   output_d(0xaa);
   delay_us(10);
   output_d(0x00);
   delay_us(2);
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Mon Mar 31, 2014 12:52 pm     Reply with quote

You have to understand that everything takes time.
The output instructions themselves take several instructions (4). The jump back to the start of the loop takes a couple of instruction times(3). Result, the loop time will be longer than your delays.
It is possible to do what you want by shortening the delays (use 'delay_cycles', rather than delay_us), but as soon as your code wants to do anything else, times will vary again. Each delay_cycle, takes 0.2uSec.
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Mon Mar 31, 2014 1:03 pm     Reply with quote

Ttelmah wrote:
You have to understand that everything takes time.
The output instructions themselves take several instructions (4). The jump back to the start of the loop takes a couple of instruction times(3). Result, the loop time will be longer than your delays.
It is possible to do what you want by shortening the delays (use 'delay_cycles', rather than delay_us), but as soon as your code wants to do anything else, times will vary again. Each delay_cycle, takes 0.2uSec.

Thank you for your reply, now i can change the switching frequency to the desired value. However, how could i vary the duty cycle of the pulse too ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Mon Mar 31, 2014 1:19 pm     Reply with quote

I've already told you what to do. Use delay_cycles. Multiply the times you want by 5. Subtract the times for the instructions you want between the delays.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Mar 31, 2014 2:27 pm     Reply with quote

Your chosen PIC has a hardware PWM generator. You could use that instead of messing about with delays.

Once set the PWM continues to run whilst main() does something else.

It's all in the manuals and example code library.

Mike

PS Throw Proteus/ISIS away, use real hardware.
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Mon Mar 31, 2014 9:55 pm     Reply with quote

Hi,
I am not sure about the PWM module built in or how to use the delay_cycle() to reach the desired freq,since this is my first attempt at PIC programming and i am really running short of time .Could you tell me how to achieve the required pulses freq and duty cycle by modifying the delay_us() function from the existing code Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 12:22 am     Reply with quote

I'll give you one example:
Code:

   output_d(0x55);
   delay_us(10);
//Will take 10.8uSec (since the output takes 4 machine cycles)
//Each machine cycle is 4 clock cycles = 0.2uSec
//delay_cycles counts in machine cycles
   output_d(0x55);
   delay_cycles(46);
//will now take 10uSec, since the delay is now 4 cycles shorter


Remember that the last delay before the loop, has to correct both for the output time, and the loop time.

The point about the PWM, is that it will generate accurate timings, even if the processor is doing other things. In your case if you start adding code, to change pulse widths etc., then the loop times will go completely wrong....
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 4:50 am     Reply with quote

thebenman wrote:
Hi,
I am not sure about the PWM module built in ............
The CCS example EX_PWM.C even shows how to use the module for an '877!!!!

Mike
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 8:05 am     Reply with quote

Mike Walne wrote:
thebenman wrote:
Hi,
I am not sure about the PWM module built in ............
The CCS example EX_PWM.C even shows how to use the module for an '877!!!!

Mike


I did look into that example code. However, my circuit is designed to draw the PWM pulses from the pin 19 to 26 to drive the eight mosfets. When i use the CCP module i get the output only from CCP1 and CCP2 pins of the PIC MCU.
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 8:10 am     Reply with quote

Ttelmah wrote:
I'll give you one example:
Code:

   output_d(0x55);
   delay_us(10);
//Will take 10.8uSec (since the output takes 4 machine cycles)
//Each machine cycle is 4 clock cycles = 0.2uSec
//delay_cycles counts in machine cycles
   output_d(0x55);
   delay_cycles(46);
//will now take 10uSec, since the delay is now 4 cycles shorter


Remember that the last delay before the loop, has to correct both for the output time, and the loop time.

The point about the PWM, is that it will generate accurate timings, even if the processor is doing other things. In your case if you start adding code, to change pulse widths etc., then the loop times will go completely wrong....


Thanks for the example, it certainly made it easier. Just to be sure if i needed a delay of 2 microseconds then i would have to use delay_cycles(6). 0.8 micro seconds for the output_d instruction and the rest from delay_cycles(6)....Am i correct ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 8:56 am     Reply with quote

Yes.

However I'd have to say since you are turning half the MOSFETs all on at once, why not just drive them all from the CCP pins.

Remember that if this is a power switching circuit, logic outputs on the PIC are not suitable to directly drive MOSFETs. The typical gate capacitance of a power MOSFET, is over 1000pF. As such they need significant current to switch them quickly. This is why MOSFET driver IC's have 'instantaneous' ratings like 1.5A. The slow switching you will get directly driving from a PIC, doesn't matter terribly at a low frequency like a couple of KHz, but at 50KHz, losses will shoot up....
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 9:17 am     Reply with quote

Ttelmah wrote:
Yes.

However I'd have to say since you are turning half the MOSFETs all on at once, why not just drive them all from the CCP pins.

Remember that if this is a power switching circuit, logic outputs on the PIC are not suitable to directly drive MOSFETs. The typical gate capacitance of a power MOSFET, is over 1000pF. As such they need significant current to switch them quickly. This is why MOSFET driver IC's have 'instantaneous' ratings like 1.5A. The slow switching you will get directly driving from a PIC, doesn't matter terribly at a low frequency like a couple of KHz, but at 50KHz, losses will shoot up....


I do not drive the MOS Gate from the PIC output directly, i use a optocoupler IC MCT2e. Wish i could attach the schematic but i don't see such a opinion here...btw is there such an option ? Anyway i was wondering if it would be possible to drive any number of MOSFET from a single CCP1 pin of the PIC ? unlike what i have done here Smile
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 10:16 am     Reply with quote

thebenman wrote:

I do not drive the MOS Gate from the PIC output directly, i use a optocoupler IC MCT2e. Wish i could attach the schematic but i don't see such a opinion here...btw is there such an option ? Anyway i was wondering if it would be possible to drive any number of MOSFET from a single CCP1 pin of the PIC ? unlike what i have done here Smile


You've only just got round to telling us you're driving a power circuit.
Would you care to provide more detail so we don't have to second guess exactly what you're doing.
(Simple ASCII art will suffice.)

Also, in what way will you eventually be wanting to modulate the PWM?

Mike
thebenman



Joined: 31 Mar 2014
Posts: 9
Location: India

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 10:27 am     Reply with quote

Hey,

I have actually build (that is almost !!) a multiple input multiple output DC-DC converter. What it does is it basically takes the two voltage sources and converts them to a different level such as addition, subtraction of the two circuits etc. Each seprate circuit (addition, subtraction) has two mosfets each in them. The mosfets needs to be switched on/off alternatively at a switching frequency of 50Khz.

I sorry i do not know much about ASCII charts, although i only have a .doc file of the circuit schematic :(
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Apr 01, 2014 2:45 pm     Reply with quote

No, I said ASCII art not chart.

You can do a schematic / block-diagram with text:-
Code:
-----WWW----

 --------
|        |
|        |
 --------

Some sort of picture of what you're doing will avoid further confusion.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group