Continue to Site

Do While loop not working.

swapan

Full Member level 4
Full Member level 4
Joined
Feb 20, 2009
Messages
209
Helped
27
Reputation
54
Reaction score
24
Trophy points
1,298
Location
Kolkata
Activity points
2,883
Hi guys,
I am trying to implement PWM MC of dsPIC30F2010. PWM1L and PWM1H has been used in complementary mode. 2 LEDs (Green and Red) has been connected to PWM1L (Red) and PWM1H (Green) respectively. Please see my code. on powering up, intensity of the Red LED reduces gradually whereas the same of Green LED increases gradually simultaneously. Then the Green LED (PWM1H) remains in full glow instead of reversing the process as desired in code.

C:
unsigned int duty;
void main() {
TRISD = 0x0;
duty = 0;
PWM1_MC_Init(5000, 0, 0x11, 0);
PWM1_MC_Set_Duty(duty, 1);
PWM1_MC_Start();

 do     {
 do {
 duty++;
 PWM1_MC_Set_Duty(duty, 1);
 delay_ms(1);
    } while (duty < 32767);

 do {
 duty--;
 PWM1_MC_Set_Duty(duty, 1);
 delay_ms(1);
    } while (duty > 0);
        } while (1);
}
 
I don't see any LEDs in your code.

the little snipped you've shown should work. Have you tried debugging it? Simulation?
 
I don't see any LEDs in your code.
Actually, I have not assigned any I/O for LEDs. Those are used in two PWM output bits (PWM1L and PWM1H) out of six PWM output bits. I have not yet run simulation. It seems only the first do/while loop works. I have tried using a "LED on" (connected to PORT D ) command between two do/while loops. But the LED doesn't lit.
Code:
unsigned int duty;
void main() {
TRISD = 0x0;
duty = 0;
PWM1_MC_Init(5000, 0, 0x11, 0);
PWM1_MC_Set_Duty(duty, 1);
PWM1_MC_Start();

 do     {
 do {
 duty++;
 PWM1_MC_Set_Duty(duty, 1);
 delay_ms(1);
    } while (duty < 32767);
LATD = 1;
 do {
 duty--;
 PWM1_MC_Set_Duty(duty, 1);
 delay_ms(1);
    } while (duty > 0);
        } while (1);
}
 
LEDs can´t be switchoe ON or OFF using code, unless you define what ON and OFF means.

* usually you can set a PORT output HIGH or LOW
* but to do so you first need to define the port as an output.
* and you need to define (depending on your LED wiring) whether LOW or HIGH means ON

Also PWM is not the best method to debug a code, because
* the human eye can´t barely differentiate between a constant 50% and 100% duty cycle.
* the PWM module will (may) be running even if the main() is stalled in an endless while(1) loop. . or even if the microcontroler is in idle mode ...

Klaus
 
What is the maximum value for duty? It is returned by the PWMx_Mc_Init function.
How long does it take for the green LED to go to 100%?
It should take 32.767 seconds before the second loop is executed
If you wait 66 seconds, you should see the green LED go to 0% again.
The first loop should test for the maximum value of duty if you want a continuous change of duty cycle.
 
Instead of stopping at 32767, let the duty cycle reach 65535 and see if the problem still persists.
Code:
unsigned int duty;
void main() {
    TRISD = 0x0;  // Set PORTD as output
    duty = 0;

    PWM1_MC_Init(5000, 0, 0x11, 0);
    PWM1_MC_Set_Duty(duty, 1);
    PWM1_MC_Start();

    while (1) {
        // Increase brightness
        while (duty < 65535) {
            duty++;
            PWM1_MC_Set_Duty(duty, 1);
            delay_ms(5);  // Slightly longer delay for smooth transition
        }

        // Decrease brightness
        while (duty > 0) {
            duty--;
            PWM1_MC_Set_Duty(duty, 1);
            delay_ms(5);
        }
    }
}
 


Write your reply...

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top