Sine to rectangular

val1[i] is changing from 0 to 80, why : if (val1[i] >> 40) is not working, on PC13 I have 1.65V ?

//1.65V
#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);// = 0/80
  }
}

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

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

void loop()

{
  int i;
   if  (val1[i] >> 40) 
  
  {
   digitalWrite (PC13, HIGH);
  }
  else
  {
   digitalWrite (PC13, LOW);
  }
}

>> is the bitshift operator. Is that the intention?
Or do you want to check if the value is greater than 40?

> greater than
>= greater than or equal to

1.65v sounds like 3.3 volts with a 50% duty cycle.

But, since variable [i] is always zero in loop(), that’s not the problem... or is it?

  int i;
   if  (val1[i] >> 40) 

..and the value of i is . . . what?

(And the thing about the shift operator)

I fixed that = still nothing on PC13
, Thanks

We can't see your fixed code.

I forgot pin mode = PC13 = 0.05V sawtooth

pinMode(out2, OUTPUT);

It is not fixed because I don't have rectangles,
here is updated

//1.65V
#define SAMPLES 77

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

int flag1 = 0;
int out1 = PB7;
int out2 = PC13;

//++++++++++++++++++++++++++++++++++
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);// = 0/80
  }
}

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

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

void loop()

{
  int i;
   if  (val1[i] > 40) 
  
  {
   digitalWrite (PC13, HIGH);
  }
  else
  {
   digitalWrite (PC13, LOW);
  }
}

See reply #5

0 to 4096

Wrong.
Have another guess.

Hint: it should be in the range 0 to 76 inclusive.
But is it?

 val1[i] = 40 + amp * sin(stp * i);// = 0/80

regardless i , sin is varying between -1 and +1

sin(stp * i)

amp = 40
val1[i] = 40 - 40 = 0 = min val
val1[i] = 40 +40 = 80 = max val

I asked a very specific question about the value of the variable "i" within the function "loop()".

I agree with that

But what is it?

Why this is important, sin(i) can't be higher than 1 and lower than -1

It's important because an uninitialised variable used as an array index could point ANYWHERE in memory.

(And yes, C/C++ allows negative array indices)

I added this and on LCD i = 0

int i;
  lcd.setCursor(0, 0);
  lcd.print("i = ");
  lcd.print(i);

Congratulations! You got lucky.

Don't rely on it.

I know that, I did that for curiosity