Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic18f4520 pwm

Status
Not open for further replies.
Hi,

I think this little snippet in the interrupt routine might be causing you grief:

Code:
	if(Duty_cycle == 0)
	{
		PWM1_OUT = 0;
		}

Try commenting it out and test the result.
 
Hi,

Try this as your interrupt routine:

Code:
#pragma interrupt ccpisr_hand
void ccpisr_hand()
{
if(PIR1bits.CCP1IF == 1)
    
    if(PWM1_OUT == 1 )                      //Will be 1 if we are at end of pulse
    {
        PWM1_OUT = 0;
        CCPR1 = 5000 - temp;                //Off time
        }

    else
    {
        if(Duty_cycle > 0 && Duty_cycle < 97)
        {
            PWM1_OUT = 1;
            temp = 50 * (int) Duty_cycle;
            CCPR1 = temp;                   //On time
            }

    else PWM1_OUT = 0;
    }

    PIR1bits.CCP1IF = 0;                    //Clear int flag

}
 
Status
Not open for further replies.

Latest threads

Back
Top