Pages: [1]   Go Down
Author Topic: main() vs. setup() + loop()  (Read 1157 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What happens in setup() that causes things to work, but when using "int main( void )" nothing happens?

On the Uno, I learned that I needed to explicitly invoke sei() or Serial.print*() wouldn't work. In the SAM3X8E context and code files, I see something called SystemInit() in one file, but that doesn't have much effect either.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What happens in setup() that causes things to work, but when using "int main( void )" nothing happens?

On the Uno, I learned that I needed to explicitly invoke sei() or Serial.print*() wouldn't work. In the SAM3X8E context and code files, I see something called SystemInit() in one file, but that doesn't have much effect either.

Well in the context of the Uno board nothing happens in setup() except what you plug into it. in fact the following example sketch from the IDE is valid and will compile and upload, while doing nothing.
Code:
void setup() {
  // put your setup code here, to run once:

}

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

 What you are probably think of is the main() function the IDE 'wraps' you sketch around, as it has an init() function that sets up timer0 and enables it's interrupt so that  the millis() function can well..function.  smiley-wink
Logged

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 139
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the main function:
Code:
int main( void )
{
init();

delay(1);

#if defined(USBCON)
USBDevice.attach();
#endif

setup();

for (;;)
{
loop();
if (serialEventRun) serialEventRun();
}

return 0;
}
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Pages: [1]   Go Up
Jump to: