Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

4 square wave generation using PIC18F4520

Status
Not open for further replies.

vinayakdabholkar

Advanced Member level 4
Full Member level 1
Joined
Nov 29, 2009
Messages
114
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
goa
Activity points
2,195
Hello
I been have been reading the PIC18F4520 datasheet regarding the CCP section. For some reason i cant seem to understand how to start generation of PWM. I am highly confused.
I want to generate 4 square waves of same frequency and phase shifted by 90 to drive MOSFETS in a synchronous buck converter. The PWMs must be at 500K.
So to get the fatest operation from the MCU at what frequency must it be operated ? 20M looks optimimum or 40M ?
some one please guide me in this regard

- - - Updated - - -

I am using MIKRO C PRO
 

The PWM period =(PR2 +1) * 4 *Tosc *(TMR2 Prescale value)
On what basis to select this Prescale value ?
I want to generate a PWM at 500K with Fosc at 20M
 

What is the max pwm frequency that a PIC18F4520 can generate ?
 

so far i have realised it is not possible to generate 4 pwm signals from the CCP of PIC18F4520
So i am trying using timers. I have started with a basic code
Code:
//Language: C
//Compiler: mikroC v8.20
//Programmer: vinayak

void main (void){
     TRISC = 0;
     PORTC = 0;
     INTCON=0xA4; //GIE=1;TMR0IE=1;TMR0IF=1
     T0CON=0xC0; //Prescalar of 1:2
     while(1)
     {
     if(INTCON.TMR0IF==1)
     {
     LATC.RC0=1;

     }
     }
}

Please guide me here to generate a 500Khz PWM signal. The PIC is running at 20Mhz
 

What is the best way to implement four 500K phase shifted square waves in PIC18F4520 CCP PWM ? or SW PWM using timer ?
 

will this code generate an interrupt after 2us ?
Code:
//Program to generate 500Khz PWM using TIMER0
//Microcontroller: Microchip PIC184520
//Language: C
//Compiler: mikroC v8.20
//Programmer: vinayak

void main (void){
     TRISC = 0;
     PORTC = 0;
     INTCON=0b00100100; //GIE=0;TMR0IE=1;TMR0IF=1
     T0CON=0b11001000; //Prescalar of 1:1
     while(1)
     {
     void interrupt();

     }



}

void interrupt (void) {

                        if (INTCON & (1<<INTCON.TMR0IF))  // T0 overflowed ?
                        {
                        INTCON &= ~(1<<INTCON.TMR0IF);    // clear timer0 overflow bit.
                        }
                        // Fosc/4 x (Prescale) x (count to overflow) = repeat rate.
                        TMR0L = 0xF6;
                        TMR0L++; // Count 1us period
                        
                       }

- - - Updated - - -

I edited the code
PLease help me here
Code:
//Program to generate 500Khz PWM using TIMER0
//Microcontroller: Microchip PIC184520
//Language: C
//Compiler: mikroC v8.20
//Programmer: vinayak

void main (void){

     TRISC = 0;
     PORTC = 0;
     TRISC.RC0=0;
     INTCON=0b00100100; //GIE=0;TMR0IE=1;TMR0IF=1
     T0CON=0b11001000; //Prescalar of 1:1
     while(1)
     {
     void interrupt();

     }



}

void interrupt (void) {
int i;

                        if (INTCON & (1<<INTCON.TMR0IF))  // T0 overflowed ?
                        {
                        INTCON &= ~(1<<INTCON.TMR0IF); // clear timer0 overflow bit.
                        TRISC.RC0=1;
                        }
                        // Fosc/4 x (Prescale) x (count to overflow) = repeat rate.
                        TMR0L = 0xF6;
                        for(i=0;i<256-TMR0L;i++)
                        {
                        TMR0L++; // Count 1us period
                        }
                        
                       }
 

i again tweeked the code and got the interrupt working but i get a error in proteus saying stack overflow is causing MCU to reset
What should i do here ?
Code:
//Program to generate 500Khz PWM using TIMER0
//Microcontroller: Microchip PIC184520
//Language: C
//Compiler: mikroC v8.20
//Programmer: vinayak

void main (void){

     TRISC = 0;
     PORTC = 0;
     LATC.RC0=1;
     INTCON=0b10100100; //GIE=0;TMR0IE=1;TMR0IF=1
     T0CON=0b11001000; //Prescalar of 1:1
     //TMR0L = 0xFF+1;
     while(1)
     {
              int i;
                        if (INTCON & (1<<INTCON.TMR0IF))  // T0 overflowed ?
                        {
                        INTCON &= ~(1<<INTCON.TMR0IF); // clear timer0 overflow bit.
                        LATC.RC0=0;
                        }
                        TMR0L=0xF6;
                        for(i=0;i<255-TMR0L;i++)
                        {
                        TMR0L++;
                        }

     }


}
 

Attachments

  • over.png
    over.png
    219.5 KB · Views: 129

By the looks of this thread you will struggle forever and never get it right.
My advice would be to go to the mikro web site and have a look at their online book.
If I remember rightly they have an example online that may also help you.
As you are using mikro C you might also find it helpful to type PWM into the code window and press F1
There is very comprehensive PWM library and help built in.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top