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);
}
}
}
default
init()
?