1

I wanted to make a simple led on/off program using USART interface. When the code doesn't have any functions other than main it runs fine, however Arduino keeps resetting when one is included. At this point I have no clue what could I be doing wrong. Here's the code:

#include <avr/io.h>
#include <util/delay.h>

#define BAUD 19200
#define BAUDRATE ((F_CPU / 16 / BAUD) - 1)

// void init()
// {
//     // Keeps resetting when uncommented, runs fine when commented
// }

int main() {
    // Initialize USART
    UBRR0H = (BAUDRATE >> 8);
    UBRR0L = BAUDRATE;
    UCSR0B = (1 << RXEN0);
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
    // Set bit 3 to output and pull it high
    DDRD |= 0b00001000;
    PORTD = 0b00001000;

    char char_arr[128];

    while(1)
    {
        int i;
        // Read USART
        for(i = 0; i < 128; i++){
            loop_until_bit_is_set(UCSR0A, RXC0);
            char c = UDR0;
            if(c == '\r')
                break;
            char_arr[i] = c;
        }
        // Handle received characters
        for(int j = 0; j < i; j++){
            switch(char_arr[j]){
                case '1': 
                    PORTD = 0b00001000; 
                    break;
                case '0': 
                    PORTD = 0b00000000; 
                    break;
            }
            _delay_ms(100);
        }
    }
}
3
  • Does this happen if the function has a name other than init()? Commented Mar 6, 2023 at 9:55
  • @EdgarBonet yes, function name doesn't matter Commented Mar 6, 2023 at 10:22
  • the compiler should remove that function during optimization, because the function is not used .... maybe the problem is in the compiler command line Commented Mar 6, 2023 at 15:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.