EEPROM.read before void setup()

So, does it compile now and if so, does it work as required ?

The error still the same.

What code ?
What error ?

post #18

int val1[SAMPLES];

Why are you still trying to declare an array with a non const variable as the size parameter, particularly as the same array is also declared with a const as its size parameter ?

int val[MaxArraySize];
int val1[MaxArraySize];

[quote="79galinakorczak, post:26, topic:865703, full:true"]
Here is a cleaned program, no more "val"

///////////////////////
#define SAMPLES 77
////////////////////////

#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;

int flag1 = 0;
int out1 = PB7;

//++++++++++++++++++++++++++++++++++
int val1[SAMPLES];
//+++++++++++++++++++++++++++++++++++


int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */

  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;

  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{

  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{
  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{


}
int val1[SAMPLES];

Are you kidding ?

this is working original, what is wrong with that ?

Where is your
(EEPROM.read(0) << 8) | EEPROM.read(1);
you've been asking about all the time?

It only works because SAMPLES is #defined rather than being a variable. We have just gone round in a 28 step circle

To be fair, at least on my browser, the #define is the same colour as the comment above and below - at first glance I thought it was a /* */ comment, and the #define was inside.

For anything other than single bytes, you'd use EEPROM.get, surely?

1 Like

To hold a value which will never be above 100, you would not use a 16-bit data type anyway.

So you have decided that you don't want SAMPLES to be a variable that you read from EEPROM?

No,
I want the value to be read from EEPROM, there was some confusion in post #25 so I cleaned up the program.

This is the last version, with error

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>


int S;
int p = PB14;
int n = PB12; // right +
int ap = PB13; //left, -
int an = PB15;
int E = PB11;

///////////////////////
//#define SAMPLES 77

const int MaxArraySize = 100;
uint16_t SAMPLES = 77; // Must be <= MaxArraySize
int val1[MaxArraySize];

////////////////////////

#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;

int flag1 = 0;
int out1 = PB7;

//++++++++++++++++++++++++++++++++++
int val1[SAMPLES];
//+++++++++++++++++++++++++++++++++++


int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */

  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;

  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{

  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{
  lcd.begin(16, 2);
  pinMode(p, INPUT_PULLUP);
  pinMode(n, INPUT_PULLUP);
  pinMode(ap, INPUT_PULLUP);
  pinMode(an, INPUT_PULLUP);
  pinMode(E, INPUT_PULLUP);
  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{

  if (digitalRead(p) == LOW)

  {
    (S++ );
  }
  if (digitalRead(n) == LOW)
  {
    (S-- );
  }
  if (digitalRead(E) == LOW)
  {
    EEPROM.write(0, S);
  }
  lcd.setCursor(0, 0);
  lcd.print("S ");
  lcd.print(S);
  delay (10);

}

sample_reset__4:29:17: error: array bound is not an integer constant before ']' token
int val1[SAMPLES];

uint16_t SAMPLES = 77; 

An integer

const uint16_t SAMPLES = 77; 

An integer constant.

///////////////////////
//#define SAMPLES 77

const int MaxArraySize = 100;
//uint16_t SAMPLES = 77; // Must be <= MaxArraySize
const uint16_t SAMPLES = 77;
int val1[MaxArraySize];

////////////////////////

exit status 1
conflicting declaration 'int val1 [77]'
from this line

int val1[SAMPLES];

No errors = testing

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>


int S;
int p = PB14;
int n = PB12; // right +
int ap = PB13; //left, -
int an = PB15;
int E = PB11;

///////////////////////
//#define SAMPLES 77

const int MaxArraySize = 100;
//uint16_t SAMPLES = 77; // Must be <= MaxArraySize
const uint16_t SAMPLES = 77;
int val1[MaxArraySize];

////////////////////////

#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;

int flag1 = 0;
int out1 = PB7;

//++++++++++++++++++++++++++++++++++
//int val1[SAMPLES];
//+++++++++++++++++++++++++++++++++++


int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */

  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;

  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{

  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{
  lcd.begin(16, 2);
  pinMode(p, INPUT_PULLUP);
  pinMode(n, INPUT_PULLUP);
  pinMode(ap, INPUT_PULLUP);
  pinMode(an, INPUT_PULLUP);
  pinMode(E, INPUT_PULLUP);
  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{

  if (digitalRead(p) == LOW)

  {
    (S++ );
  }
  if (digitalRead(n) == LOW)
  {
    (S-- );
  }
  if (digitalRead(E) == LOW)
  {
    EEPROM.write(0, S);
  }
  lcd.setCursor(0, 0);
  lcd.print("S ");
  lcd.print(S);
  delay (10);

}

Adding EEPROM.read
error
exit status 1
assignment of read-only variable 'SAMPLES'

#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>


int S;
int p = PB14;
int n = PB12; // right +
int ap = PB13; //left, -
int an = PB15;
int E = PB11;

///////////////////////
//#define SAMPLES 77

const int MaxArraySize = 100;
//uint16_t SAMPLES = 77; // Must be <= MaxArraySize
const uint16_t SAMPLES = 77;
int val1[MaxArraySize];

////////////////////////

#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;

int flag1 = 0;
int out1 = PB7;

//++++++++++++++++++++++++++++++++++
//int val1[SAMPLES];
//+++++++++++++++++++++++++++++++++++


int16 shift = 0;

int amp = 40;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev1 = PIN_MAP[out1].timer_device;
uint8 cc_channel1 = PIN_MAP[out1].timer_channel;

void fun1()
{
  flag1++;
}

void timer_conf()
{
  timer_dma_set_base_addr(dev1, TIMER_DMA_BASE_CCR2);
  timer_dma_set_burst_len(dev1, 1);
  timer_dma_enable_req(dev1, cc_channel1);
  timer_set_reload(dev1, 102);
  timer_set_prescaler(dev1, 0);
}

void dma_conf()
{
  dma_init(DMA1);
  /* T4C2 DMA C4 */

  dma_cfg.tube_dst = &(dev1->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;

  dma_cfg.tube_src = val1;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH1;
  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM4_CH2;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH4, &dma_cfg);
}

void dma_start()
{

  dma_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
  dma_enable(DMA1, DMA_CH1);
  dma_enable(DMA1, DMA_CH2);

}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 40 + amp * sin(stp * i);
  }
}

void setup()
{
  //////////////////////////////
 
  SAMPLES = (EEPROM.read(0) << 8) | EEPROM.read(1);
 // SAMPLES = EEPROM.read(0, (uint16*)&S);
  
  /////////////////////////////////
  lcd.begin(16, 2);
  pinMode(p, INPUT_PULLUP);
  pinMode(n, INPUT_PULLUP);
  pinMode(ap, INPUT_PULLUP);
  pinMode(an, INPUT_PULLUP);
  pinMode(E, INPUT_PULLUP);
  pinMode(out1, PWM);

  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{

  if (digitalRead(p) == LOW)

  {
    (S++ );
  }
  if (digitalRead(n) == LOW)
  {
    (S-- );
  }
  if (digitalRead(E) == LOW)
  {
    EEPROM.write(0, S);
  }
  lcd.setCursor(0, 0);
  lcd.print("S ");
  lcd.print(S);
  delay (10);

}

Right there.

(The compiler already told you that, but you chose not to share)

Look,just size the array for a 100 samples, but only use SAMPLES of them.
All you need to do then is make sure that SAMPLES never exceeds 100.

1 Like