Pages: [1]   Go Down
Author Topic: Can you use main() in a regular Arduino program?  (Read 1445 times)
0 Members and 1 Guest are viewing this topic.
Great Smokey Mountains
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On another forum someone says they used the following program with the Arduino IDE and that it compiled and uploaded properly:
Code:
/*
    5-10-07
    Copyright Spark Fun Electronics© 2007
    Nathan Seidle
    nathan at sparkfun.com
  
    ATmega168
  
   Example Blink
   Toggles all IO pins at 1Hz
*/

#include <avr/io.h>

//Define functions
//======================
void ioinit(void);      //Initializes IO
void delay_ms(uint16_t x); //General purpose delay
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults

    while(1)
    {
      PORTC = 0xFF;
      PORTB = 0xFF;
      PORTD = 0xFF;
      delay_ms(500);

      PORTC = 0x00;
      PORTB = 0x00;
      PORTD = 0x00;
      delay_ms(500);
    }
  
    return(0);
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0b11111111; //All outputs
    DDRC = 0b11111111; //All outputs
    DDRD = 0b11111110; //PORTD (RX on PD0)
}

//General short delays
void delay_ms(uint16_t x)
{
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 90 ; y++){
      for ( z = 0 ; z < 6 ; z++){
        asm volatile ("nop");
      }
    }
  }
}

I responded that this won't work because it has the main() function in it and would cause the Arduino IDE to generate this error:
Quote
In function 'int main()':
error: redefinition of 'int main()"

To which the person responded:
Quote
As far as I'm aware, it only adds it the main() function if there isn't one present: e.g. it will make up for a number of mistakes only if they are present.

Am I missing something fundamental? I don't think so, but I thought I'd check here first.

Smiley
Logged

FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smiley

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You win this round. I copied and pasted the code into the IDE (17), and got exactly the error message you described.
Logged

Great Smokey Mountains
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The fellow said he was using Arduino 0018, so I just tried it and it works. I posted the following:
Quote
Holy Poop! It does compile in Arduino 0018.

This is not a quibble because it doesn't compile in earlier versions and this is major news to me. I need to investigate this further, but if the Arduino has done away with their earlier way of doing things such that regular C code compiles okay, then I'm in deep doo for reasons irrelevant to this thread. I know this isn't your issue, but tomorrow I'll try to see how this uploads and behaves on my Arduino and maybe I can help you with the specific issues you mention.

BTW - I've seen your error messages before when I didn't properly sync pressing the reset button with starting AVRDude. What I do is get the AVRDude script ready to run, press the reset button on the Arduino then immediately hit the return button to run AVRDude. The Arduino board sends a pulse on the DTR line to reset the Arduino just before it calls AVRDude so that happens automatically, but since you are building your own board, you'll need to do your own reset.

Sorry, for the confusion.

Smiley

I hope somebody here knows more about this. It seems I've missed a big issue.

Smiley
« Last Edit: February 02, 2010, 11:48:46 pm by JoePardue » Logged

FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smiley

Wuppertal/Germany
Offline Offline
God Member
*****
Karma: 1
Posts: 895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
sometimes I am really surprised how creative are people in misusing tools/software/whatever.  8-)

Ok, there is a little problem with this. Replacing/overwriting main() has a few sideeffects. The Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code are not initialzed. Put a normal delay(1000) call into the sketch-code and it will stop working at this point.
The setup() and loop() functions are also not called automatically any more.

So, it compiles but isn't of much use.

Eberhard

Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There are many non-Arduino programs made for ATmega architecture (no setup, no loop, just a main) that I would like to use in the Arduino IDE.  Being able to replace the built-in main() is a good thing, but as noted, there are some minor issues.

As I recall, the built-in main() is just this:

Code:
void main() {
    init();
    setup();
    while (1)
        loop();
}

So if you wanted to support Arduino library features like delay(), but don't have the setup/loop structure in your ATmega project, you have two options:

(1) call the built-in init() in your main() before you use Arduino library features, or

(2) change your main() to be called setup() and provide a dead, empty loop() function.
« Last Edit: February 03, 2010, 12:55:16 pm by halley » Logged

Great Smokey Mountains
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, finally got the word from the horse's mouth.

This is an accident and not a feature of Arduino.

From David Mellis:
Quote:
>
> The reason it's not supported is *because* it might change. It
> happened as accidental by-product of other changes and could disappear
> in the same way.
>

This means that if you are going to use the Arduino you should only use code in the Arduino format. That is no main() function and you use setup() and loop() instead. The main function is added to your code and has an ioinit() function before calling setup() and loop(). If you use this accidental bypass then the Arduino ioinit() won't get called and the Arduino library functions might mess up in unpredictable way.

Bottom line:
1. Don't use code with the int main(void) function in it.
2. Do read the Arduino introductory materials.
3. Only use code in the Arduino Sketch format.

Smiley
Logged

FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smiley

Pages: [1]   Go Up
Jump to: