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

what's the difference between main() and loop()?

Discussion in 'AVR' started by mikewax, Jan 11, 2017.

  1. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    Here's a little mystery that has cost me about 6 days of grief. the following code uses pwm to fade an LED up and down. when i organize the code using the "main(void){....}" declaration, it works as written. But when i divide the code into two segments with "void setup(){....}" and "void loop(){....}" the LED doesn't fade in and out. It just comes on 100% and stays that way.
    any ideas about why this is? thanx

    #include <avr/io.h>
    // fastPWM on timer1, non-invert mode, no prescale, output on pin B2, Duty cycle = (x mod 50)/50
    volatile unsigned int t, u, x = 0;
    void wait(unsigned int base);

    //void setup() {
    int main(void) {
    ICR1 = 50;
    TCCR1A |= (1 << COM1B1) | (1 << WGM11);
    TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10);
    DDRB |= (1 << DDB2);
    //} void loop() {
    while(1){ x += 4; OCR1B = x%50; wait(600); }
    return 0;
    }

    void wait(unsigned int base){ for (t = 0; t < base; t++){ for (u = 0; u < base; u++){} } }
     
    Last edited: Jan 11, 2017
  2. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,618
    Likes:
    110
    Location:
    Nuneaton, Warwickshire, UK
    I'm not a programmer, but I think you have to have main, and any other functions are called from within there. So when you just have the setup and loop functions they don't run because there's no main. Presumably the output which goes to the LED defaults to a high state.
    That's about my limit, hope it helps!
     
  3. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,680
    Likes:
    37
    when using arduino IDE programmer i use setup and loop, I have only seen this program do that
    when I program pic...or windows i use main

    ... whatever it is, it needs to be what ever the compiler identifies with as the "main"
     
    Last edited: Jan 11, 2017
  4. dave

    Dave New Member

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


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    8,508
    Likes:
    809
    Location:
    Rochdale UK

    When using C or C++ there must always be a main()... When using the Arduino development system the skeletal program is the same...

    Code (c):

    void main(void)
       {
       setup();
       loop();
       }
     
    You only ever get to see the two functions inside the main()..
     
  6. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    when i run arduino 1.6.12 compiler and i click file...new, it opens a new editor window and here is the default text in the window:

    void setup() {
    // put your setup code here, to run once:
    }

    void loop() {
    // put your main code here, to run repeatedly:
    }


    and that structure is what i've always used. setup and loop. and i've never had a problem with it until now. but timer1 won't operate with the setup loop declarations. it will only work when i use int main(). so i changed my program to use the int main() declaration. and guess what? timer2 won't work now. timer1 will only work when i use int main and timer2 will only work when i use setup loop. whatthehell did i do to deserve this?
     
  7. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,618
    Likes:
    110
    Location:
    Nuneaton, Warwickshire, UK
    Maybe it's time to delete the file or even the project and do it over. Just copy the code into temporary files so you can copy and paste it back in to the new files.

    I encountered a similar thing with mplabx recently, the problem was due to the template I'd used, in combination with lack of knowledge of how to put it right. Choosing a different template made the problem go away.
     
  8. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    wow..... i had not thought of that. start with a new file. ok we'll see
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    8,508
    Likes:
    809
    Location:
    Rochdale UK
    I may not appear to be clear on this...

    The Arduino environment only uses setup() and loop()... When a sketch is compiled, the pre compiler wraps your code into a main() function and then uses the main compiler to create a hex... This is so the environment is as easy as can be..

    You do not see this step but it is done none the less... If you include a main() within the Arduino environment you will inevitably cause issues...
     
  10. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    oh so that's their way of sorta dumbin it down. ok well i'll check out the compiled files and maybe find out what's up.
    thanx

    damn it just occured to me. i just gotta get a different compiler. do you know of any good ones?
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    8,508
    Likes:
    809
    Location:
    Rochdale UK
    Atmel studio 7 comes with one of the best... Free!!!
     
  12. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    thanx i'm there :cool:
     
  13. Willen

    Willen Well-Known Member

    Joined:
    May 22, 2012
    Messages:
    1,264
    Likes:
    111
    Location:
    Nepal (GMT +5:45)
    Hi, I got a copy of Atmel 7 from a person. But during installation it ask for Windows Update (two files). problem is I do not have internet. Just have expensive and poor GPRS in cellphone. I am using Windows7. Do anyone think that using Windows 8 or 10 do not ask for update?
     
  14. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    i could email you the files if you have a way of retrieving them.
     
  15. Willen

    Willen Well-Known Member

    Joined:
    May 22, 2012
    Messages:
    1,264
    Likes:
    111
    Location:
    Nepal (GMT +5:45)
    Hi, are you talking about the windows update two files I was talking about? If yes then send me here please: nepal.nepaliman(at)gmail.com
    (at) = @
    I will try to download.
     
    Last edited: Jan 12, 2017
  16. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    ok i sent them but one of the files is 44MB so it might be quite difficult to retrieve. i hope you succeed.:)
     
  17. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,680
    Likes:
    37
    I have come to really like the visual micro plugin for my visual studio 2013 and atmega2560

    2013 is good size for my computer , and the plugin includes an interesting style of debugging (considering its for a chip that does not typically support debug)
     
  18. mikewax

    mikewax Member

    Joined:
    Nov 6, 2016
    Messages:
    47
    Likes:
    2
    Location:
    los angeles
    ONLINE
    that's funny when i installed atmel studio7 it downloaded several versions of visual studio while it was installing. and visual studio works for atmel too?
     

Share This Page