vpos
vpos

Reputation: 31

C program for AVR ATmega328P restarts after interrupts (Proteus 8)

The program is written in C and compiled for AVR microcontroller ATmega328P. The program is being debugged in Proteus 8. After all interrupts are executed, the program restarts from the initialization section instead of entering the main loop. The program is expected to enter an infinite loop in the main function after initialization.

Could someone point out what the problem might be and what to look for?

P.S. It happens not all the time, but often.

main.c

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
#include <string.h>
#include "lic328p_gpio.h"
#include "lic_menuio.h"
#include "battery.h"
#include "lic_18650h.h"
#include "lic_uart.h"
#include "lic_adc.h"

static volatile tx_data tx_package;
static volatile adc_channel channel;
static volatile batlist batstat;

ISR(ADC_vect)
{
  uint16_t data = adc();
  batt_data_handling(data, &batstat);
  fill_tx_package(&batstat, &tx_package);
  UCSR0B |= (1 << UDRIE0);      
}

ISR(USART_RX_vect)
{
  // Selecting the Menu Mode
  setMode(&menu, UDR0);
}

ISR(USART_UDRE_vect)
{
    if(tx_package.index < tx_package.length) {
        UDR0 = tx_package.data[tx_package.index];
        tx_package.index++;
    } else {
        tx_package.inprocess = false;
        UCSR0B &= ~(1 << UDRIE0);
    };
}

ISR(USART_TX_vect) {

}

int main(void)
{
  port_init();
  uart_init(MYUBRR);
  timer_init();
  adc_init();

  init_adcChan(&channel);
  init_battData(&batstat);
  init_tx_data(&tx_package);

  sei();

  while(1) {
  };

  return 0;
}

enter image description here

Upvotes: 0

Views: 119

Answers (0)

Related Questions