Can Serial.begin be used with int main(void)?

hi

I’ve wrote a simple sketch to blink a LED when there is incoming serial data. I’m not using Arduino syntax exclusively because I want to learn to program without it.

This is the version that works:

#include <avr/io.h>     //Include the headers that handles Input/Output function for AVR chips
#include <util/delay.h> //Include the headers that allow for a delay function

#define F_CPU 16000000UL //Define the speed the clock is running at.  Used for the delay.h functions
#define LED 5
                 
uint8_t midi_msg = 0; 



void setup() {
  Serial.begin(31250);
  DDRB = (_BV(5));
}

void loop() {
  
  while (Serial.available()) {
    midi_msg = Serial.read();
    
    PORTB ^= (_BV(LED)); //toggles the led
    _delay_ms(500);
    PORTB ^= (_BV(LED)); //toggles the led
    _delay_ms(500);
     
  } //END WHILE
}

now when I replace void Setup and void Loop with void init_io and int main(void) the sketch does not work anymore:

#include <avr/io.h>     //Include the headers that handles Input/Output function for AVR chips
#include <util/delay.h> //Include the headers that allow for a delay function

#define F_CPU 16000000UL //Define the speed the clock is running at.  Used for the delay.h functions
#define LED 5

uint8_t midi_msg = 0; 

void init_io(void)
{  
  DDRB = (_BV(5));
}

int main(void)
{
    init_io(); 
    Serial.begin(31250);
    
    for (;;) {  //Loop forever

      while (Serial.available()) {
        midi_msg = Serial.read();
 
        PORTB ^= (_BV(LED));
        _delay_ms(500);
        PORTB ^= (_BV(LED));
        _delay_ms(500);  
      } //END WHILE
      
    } //END for(;;)    
}

I’ve also tried it with Serial.begin inside of void init_io(void), doesn’t work either.

Could it be something to do with the Serial library? Can it be used as above?

thanks

You need to look at the cpp file produced from your ino file. The IDE adds a main() function that calls init(), setup(), and loop() in an endless loop. If you expect hardware, and that includes the hardware serial port, to work, you MUST call init().

PaulS:
If you expect hardware, and that includes the hardware serial port, to work, you MUST call init().

call init() from within int main(void)? I thought I did that with init_io().

Here is the generated .cpp file of the second (non-working) sketch:

#line 1 "_328p_midi_clk_to_jamman_03_.ino"
#include <avr/io.h>     //Include the headers that handles Input/Output function for AVR chips
#include <util/delay.h> //Include the headers that allow for a delay function

#define F_CPU 16000000UL //Define the speed the clock is running at.  Used for the delay.h functions
#define LED 5

#include "Arduino.h"
void init_io(void);
int main(void);
#line 7
uint8_t midi_msg = 0; 

void init_io(void) 
{  
  DDRB = (_BV(5));
}

int main(void)
{
    init_io(); 
    Serial.begin(31250);
    
    for (;;) {  //Loop forever

      while (Serial.available()) {
        midi_msg = Serial.read();
 
        PORTB ^= (_BV(LED));
        _delay_ms(500);
        PORTB ^= (_BV(LED));
        _delay_ms(500);  
      } //END WHILE
      
    } //END for(;;)    
}

naut: call init() from within int main(void)? I thought I did that with init_io().

Here's all you need to code without the stupid "setup" and "loop" things:

int main (void)
{
    init();
    Serial.begin (115200);
    Serial.print ("Hello there\r\n");
    while (1);
}

I thought I did that with init_io().

I can't imagine why you thought that.

Krupski: Here's all you need to code without the stupid "setup" and "loop" things:

int main (void)
{
    init();
    Serial.begin (115200);
    Serial.print ("Hello there\r\n");
    while (1);
}

Thanks!