Generator ON/OFF

Hi This is a sine generator,

#define SAMPLES 77
#include <libmaple/dma.h>
dma_tube_config dma_cfg, dma_cfg2;
int flag = 0;
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 fun()
{
  flag++;
}

void timer_conf()
{
  timer_dma_set_burst_len(dev1, 1);
  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_attach_interrupt(DMA1, DMA_CH4, fun);
  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(LED_BUILTIN, OUTPUT);

  int i;
  pinMode(out1, PWM);

  // pinMode(out1, OUTPUT);
  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop()

{

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
  
  if (LED_BUILTIN, HIGH)
  {
    digitalWrite(PB7, 0);
  }


}


I want to switch it ON/OFF the same rate as LED is blinking, this is not working.

  ///////////////////////////
  if (LED_BUILTIN, HIGH)
  {
    digitalWrite(PB7, 0);
  }
  /////////

I suspect you cannot stop DMA once you have started it, so make all the DMA code function only if LED_BUILTIN is high.
Paul

Never seen this syntax before:

  if (LED_BUILTIN, HIGH)
  {
    digitalWrite(PB7, 0);
  }

Do you mean?

  if (digitalRead(LED_BUILTIN) == HIGH)
  {
    digitalWrite(PB7, 0);
  }

I am trying to modulate output on PB7

thanks

Why should it work? It seems like a wild guess.

Start from beginning


void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PB7, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);

  ////////////////////
  if (digitalRead(LED_BUILTIN) == HIGH)
  {
    digitalWrite(PB7, HIGH);
  }
  else
  {
    digitalWrite(PB7, LOW);
  }
  /////////
}

this part is not working ?

  ////////////////////
  if (digitalRead(LED_BUILTIN) == HIGH)
  {
    digitalWrite(PB7, HIGH);
  }
  else
  {
    digitalWrite(PB7, LOW);
  }
  /////////

I'm asking you, why are you expecting it to work? How would it work? Explain. You always set it LOW in loop() before testing it.

Your code:

loop {
turn LED on
wait 1 sec
turn LED off
wait 1 sec
test the LED and do some stuff
}

Why are you even testing LED? You already know the value because you just finished setting it. Set the LED and the port at the same time.

How about:

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(PB7, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(PB7, LOW);
  delay(1000);
}

It appears you or someone has gone to great lengths to automatically generate a sine wave.

You are going to have to go to equal lengths to make it start and stop at the rate of your LED there.

Look at the matlib code.

Just writing to that pin is very likely being nearly instantaneously changed by the process running on interrupts. At best, you might see little glitches, probably not.

a7

this is working, thanks

{
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(out1, PWM);
  delay(1000);

  digitalWrite(LED_BUILTIN, LOW);
  pinMode(out1, OUTPUT);
  delay(1000);
}