For loop help.

Status
Not open for further replies.

be80be

Well-Known Member
I did this last night the first for loop works the second one doesn't.
I can't for the life of me figure it out.
Code:
 PWM1_Initialize();
    while (1)
    {
     uint16_t load;   
     uint16_t dutycycle;
     for (load = 0; load < 0x03FF; load++)
     {
     __delay_ms (500);
     dutycycle = load;
     PWM1_LoadDutyValue(dutycycle);
     }

      for (load = 0x03FF; load > 0x03FF; load--)
      {
        dutycycle = load;
     PWM1_LoadDutyValue(dutycycle);   
      }
    }
 
Last edited:
If I just do this loop it just sits here full on
Code:
while (1)
    {
     uint16_t load;   
     uint16_t dutycycle;

      for (load = 0x03FF; load > 0x03FF; load--)
      {
        dutycycle = load;
     PWM1_LoadDutyValue(dutycycle);   
      }
    }
 
Your variable test is wrong - you're testing for it been greater than 0x3ff, and it never is as it starts at that, and then decreases.
 
I kind of was thinking that but I tried the => it didn't work too. I'm going to test for less
thanks this c stuff is not that easy i looked up for loops nothing in xc8 about going less
lust ++ lol
 
Last edited:
Assuming you want the loop to go from 0 to 0x3ff then your for loop should be,
Code:
    for(load=0;load<0x400;load++){
To go backwards is a little tricky with unsigned integers as you can't test for -1. So a test for above 0x400 will detect when it's gone negative.
Code:
    for(load=0x3ff;load<0x400;load--){
The above loop will terminate when load is 0xffff and therefore no longer less than 0x400.

Hope that makes sense.

Mike.
Edit, it would make the code much more readable to test for not equal to oxffff;
Code:
    for(load=0x3ff;load!=0xffff;load--){
 
Last edited:
Yep the 0 test don't seem to work and the arduino type test don't work with xc8 no errors just don't do nothing.
>= didn't work Im going to try it your way mike thanks
 
Uint16 cannot be lower than 0... You need it to be a signed int to use " load>0 " Mikes alternative is better for Unsigned...
 
Shouldn't the second one just read ...

Code:
      for (load = 0x03FF; load > 0; load--)
      {
        dutycycle = load;
     PWM1_LoadDutyValue(dutycycle);
      }
 
For anyone struggling with for loops in C,

Try going to https://www.onlinegdb.com/
and copy this into the page,
Code:
#include <stdio.h>
#include <stdint.h>

int main(){
    uint16_t i,j;
    for (i = 0x03FF; i != 0xffff; i--){
        j=i;
    }
    printf("Value of i is %d (0x%x) and j is %d (0x%x)",i,i,j,j);
    return 0;
}
Because the test (i!=0xffff) is done after the decrement the value of J is zero and i is 0xffff.
You can set breakpoints at various points and see whats happening.
Happy playing.

Mike.
 
Last edited:
Mike thats a great site I been looking for just a way to do that writing files and testing with C on linux works but that is easy put code and see what happens cool Thinks Mike
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…