Code does not work a all when there is an array of volatile int with atmega328p

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

why in the world is this code not working at all (!) - not even the setup - when i put in the assignment at line 60????

#include "pins_arduino.h"
#include "arduino.h"
#include <Toby.h>

#define MAX_COUNT 1000

#define TIMING_TEST_PIN 6
#define BUZZER_PIN 7

volatile int count=0;
volatile int values[MAX_COUNT];
volatile int maxValue=0;
volatile int minValue=9999;
volatile int value = 0;

volatile char hasValue=0;



void fullSetup(){
	Serial.begin(250000);//set the serial communication baudrate
	Serial.println("Setup starting!");

	tobe.initADC_forProg();
	ADCSRA &= ~(1 << ADPS0);
	delay(10); //Delay for the measurements to become accurate

	pinMode(TIMING_TEST_PIN, OUTPUT);
	pinMode(BUZZER_PIN, OUTPUT);

	tone(BUZZER_PIN,200);
	delay(600);
	noTone(BUZZER_PIN);

	Serial.println("Setup done!");
}

ISR(ADC_vect){
	PORTD |= (1<<TIMING_TEST_PIN);
	value = ADCL | (ADCH << 8);
	hasValue = 1;
	if(value > maxValue)
		maxValue = value;

	if(value < minValue)
		minValue = value;
	PORTD &= ~(1<<TIMING_TEST_PIN);
}


void setup(){
	fullSetup();
}

int anotherCount = 0;
char toggle = 0;

void loop(){
	if(hasValue == 1){
		values[count] = value;
		hasValue=0;
		count++;
	}

	if(anotherCount >= 10){
		delayMicroseconds(50);
		Serial.print("maxValue: ");
		Serial.print(maxValue);
		Serial.print(" minValue: ");
		Serial.print(minValue);
		Serial.print(" Vpp: ");
		if((maxValue - minValue) > 0)
			Serial.println(maxValue - minValue);
		else
			Serial.println(0);
		anotherCount = 0;

		tone(BUZZER_PIN,400);
		delayMicroseconds(5);
		noTone(BUZZER_PIN);

	}

	if(count >= MAX_COUNT){
		count=0;
		maxValue=0;
		minValue=9999;
		anotherCount++;
	}

}

The code of the function "initADC_forProg()":

void Toby::initADC_forProg(){

    SREG |= (1<< I);                        // set Global Interrupt Enable
    //Serial.print("SREG");
    //printByteString(SREG);

    PRR &= ~(1<< PRADC);                    // disable shutDown (set bit to 0 to enable ADC)
    //Serial.print("PRR");
    //printByteString(PRR);
        
    ADMUX = 0;                              //reset (when nothing is set, AREF is used!)
    ADCSRA = 0;
    ADCSRB = 0; // Reset and free running mode

    ADMUX = ADC0;                           //channel 0

   // ADMUX |= (1 << REFS1) | (1 << REFS0);	//1.1V internal reference

    ADCSRA |= (1 << ADATE);                 //Automatic Trigger
    ADCSRA |= (1 << ADIE);                  //Interrupt enable
    ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);  //PS_32 (ADPS2 und ADPS0 = 1) / PS_64 (ADPS2 und ADPS1 = 1)
    ADCSRA |= (1 << ADEN);                  //Enable
    ADCSRA |= (1 << ADSC);                  //start conversion !!!

    //Serial.print("ADCSRA");
    //printByteString(ADCSRA);
    

}

I dont even know which words to use for searching with google...

This topic has a solution.

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

Last Edited: Sat. Mar 25, 2017 - 05:53 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2
volatile int values[MAX_COUNT];    //2000 bytes from ATmega328's 2048 bytes of SRAM

I would expect Arduino to whinge when building.   Things like Serial have their own buffers for interrupt driven comms.   You need SRAM for hardware stack and local storage.

 

If you are building on AS7,  it might not be clever enough to notice your SRAM usage.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

2000 bytes of array leaves no room for the stack. I have a hard time imagining anything that would be run on a M328 that would tolerate as little as 48 stack bytes. Further, there is no way to prevent the stack from corrupting the array or the array from corrupting the stack. There is NO run-time protection between the two. Stack is not just call-and-return stuff - some variables (locals?) are also implemented on the stack.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Mar 25, 2017 - 04:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use eclipse and there was no warning.

 

That helped me.

Thank you!

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler has no way to know about  stack size. Well, not gcc, anyway. So, it does not warn.

 

JIm

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net