while(1)
{
if(KEY_1==1 && i<1000)
{
i=i+20;
}
else if(KEY_2==1 && i>0)
{
i=i-20;
}
else if(KEY_3==1)
{
i=800;
}
else if( KEY_4==1 )
{
while(1)
{
j=1;
i=0;
PWM1_Duty(i);
}
}
WHEN I PRESS KEY_4 DUTY CYCLE GO TO 0 AND PROGRAMME REMAIN IN THAT WHILE LOOP....TELL ME PLS HOW TO GET OUT FROM THAT SMALL WHILE LOOP WHEN I PRESS SAME KEY_4...........THIS IS FOR ON AND OFF PWM OUTPUT USING SAME KEY_4
PWM1_Duty(i);
__delay_ms(100);
}
I recommend to generate the PWM with the built in hardware. It generates fixed and clean timing for your PWM without processing power.
So your program just needs to check key status.
This solves your problem doing multiple timing critical tasks at once.
I presume the latest code already does with statement pwm1_duty().
First step should be to remove the inappropriate inner while(1) loop. Secondly implement a toggle variable which changes between 1 and 0 on consecutive KEY_4 presses.