Go Down

Topic: ATMEGA1284P issue: restarting all the time with Serial1 and PCINT (Read 128 times) previous topic - next topic

liuzengqiang

I've been trying to figure how why my board keeps restarting after this program. What it does is to set up Serial and Serial1, then set up all analog pins with PCINT, count pulses on these pins. But the program keeps restarting. I had print statements before and after setting up the PCINT. Once PCINT is set up, any use of Serial1 will cause restart so the message in setup is repeated endlessly.

In a separate program I checked the correctness of the mask and PCICR values and I enabled the correct mask and PCINT group. Serial1 pins are on group 3 and I enabled group 0. Any idea what could have caused this?

Code: [Select]

const unsigned long hi_baud=2000000; // Serial
const unsigned long lo_baud=9600; // Serial1
const int total_digital=8; // Total digital inputs. Max=8
byte oldPINA=0; // Sensor port
volatile unsigned long cts[total_digital]; // Buffer for PCINT0 to count pulses between couting periods. This changes each time a new pulse is detected and is cleared once its values are read into cnts_meas.
volatile unsigned long cts_meas[total_digital]; // Buffer for PCINT3 to store pulse counts after each counting period expires. This changes once each period, not each time a new pulse is detected.

void pciSetup(byte pin)
{
    *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin));  // enable pin
    PCIFR  |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
    PCICR  |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}

ISR (PCINT0_vect) // handle pin change interrupt for A7 to A0
{
  static byte newPINA,changePINA;
  newPINA=PINA;
  changePINA=newPINA^oldPINA;

  for (byte i=0;i<8;i++)
  {
    cts[7-i]+=changePINA&1; // PIN number and Arduino A0-7 number are reverse of each other.
    changePINA=changePINA>>1;
  }
  oldPINA=newPINA;


void setup_pulse_pcint() // Sets up PCINT on all input pins for pulse input. Also clears pulse count arrays.
{
  for (byte i=A0;i<=A7;i++) // Enable PCINT on these pins
  {
    pinMode(i,INPUT_PULLUP);
    pciSetup(i);
    cts[1]=0;
    cts_meas[i]=0;
  }
  oldPINA=PINA;
}

void setup()
{
  Serial.begin(hi_baud);
  Serial1.begin(lo_baud);

  Serial.println("Hello1");
  Serial1.println("Hello1");
  delay(1000);
  setup_pulse_pcint();
  Serial.println("Hello2");
  Serial1.println("Hello2");
}

void loop() {
  // put your main code here, to run repeatedly:

}
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

liuzengqiang

Seems to be a memory bug. Found something similar here:

https://forum.arduino.cc/index.php?topic=474175.0

So I looked into the code and found that I went over the bounds of array cst_meas in the setup_pulse_pcint() function.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Budvar10

Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

liuzengqiang

Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Go Up