1. 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.
    Dismiss Notice

For loop help.

Discussion in 'Microcontrollers' started by be80be, Oct 21, 2017.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    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 (text):

     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: Oct 21, 2017
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    If I just do this loop it just sits here full on
    Code (text):

    while (1)
        {
         uint16_t load;  
         uint16_t dutycycle;

          for (load = 0x03FF; load > 0x03FF; load--)
          {
            dutycycle = load;
         PWM1_LoadDutyValue(dutycycle);  
          }
        }
     
     
  3. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,294
    Likes:
    647
    Location:
    Derbyshire, UK
    ONLINE
    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.
     
    • Like Like x 1
    • Agree Agree x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE

    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: Oct 21, 2017
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,083
    Likes:
    326
    Location:
    Brisbane Australia
    ONLINE
    Assuming you want the loop to go from 0 to 0x3ff then your for loop should be,
    Code (text):

        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 (text):

        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 (text):

        for(load=0x3ff;load!=0xffff;load--){
     
     
    Last edited: Oct 21, 2017
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    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
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,250
    Likes:
    911
    Location:
    Rochdale UK
    Uint16 cannot be lower than 0... You need it to be a signed int to use " load>0 " Mikes alternative is better for Unsigned...
     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    I'm i was just testing the load be from
    0 to 1023 from the adc
     
  10. Beau Schwabe

    Beau Schwabe Member

    Joined:
    Jun 4, 2017
    Messages:
    32
    Likes:
    5
    Shouldn't the second one just read ...

    Code (text):


          for (load = 0x03FF; load > 0; load--)
          {
            dutycycle = load;
         PWM1_LoadDutyValue(dutycycle);
          }

     
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,083
    Likes:
    326
    Location:
    Brisbane Australia
    ONLINE
    That will only execute from 0x3ff down to 1 - not 0 as required.

    Mike.
     
  12. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,083
    Likes:
    326
    Location:
    Brisbane Australia
    ONLINE
    For anyone struggling with for loops in C,

    Try going to https://www.onlinegdb.com/
    and copy this into the page,
    Code (text):

    #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: Oct 22, 2017
    • Thanks Thanks x 1
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,829
    Likes:
    138
    Location:
    morristown,tn
    ONLINE
    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
     

Share This Page