Регулировка частоты

Нет ответов
antonx
Offline
Зарегистрирован: 29.12.2019

Здравствуйте 

Программа генератора , частоту  можна менять в ручную меняя число SAMPLES


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

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

#define SAMPLES 88

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

#include <libmaple/dma.h>

dma_tube_config dma_cfg;

int flag1 = 0;
int out1 = PB7;
int val1[SAMPLES];


int amp = 50;
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()
{
  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_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_CH2);//
  //  timer_resume(dev2);
}

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

  }
}


void setup() {


  lcd.begin(16, 2);

  int i;
  //int j;

  pinMode(out1, PWM);
  pinMode(PB13, INPUT_PULLDOWN);
  pinMode(PB12, INPUT_PULLDOWN);


  timer_conf();
  dma_conf();
  dma_start();

  init_wave();
}

void loop()
{

  lcd.setCursor(0, 1);
  lcd.print("Samples = ");
 //lcd.print(Samples);

  delay(100);

}

пытаюсь сделать с  помощью  кнопок но на PB7 нет сигнала 


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


///////////////////////////
#define MAX_SAMPLES 200
int Samples = 90;
///////////////////////////

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

int flag1 = 0;
int out1 = PB7;
int val1[MAX_SAMPLES];


int amp = 50;
int cnt = 0;
int time_track = 0;

/////////////////////////////////////////////////
float stp = 6.2831 / MAX_SAMPLES;
////////////////////////////////////////////////

int ret = 17;


timer_dev *dev1 = PIN_MAP[out1].timer_device;


uint8 cc_channel1 = PIN_MAP[out1].timer_channel;




void fun()
{
  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 =  MAX_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_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_CH2);//
  //  timer_resume(dev2);
}


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

void setup()
{
  lcd.begin(16, 2);


  pinMode(out1, PWM);
  pinMode(PB13, INPUT_PULLDOWN);
  pinMode(PB12, INPUT_PULLDOWN);


  timer_conf();
  dma_conf();
  dma_start();


  init_wave();
}


void loop()
{
  ReadButtons();
  dma_conf();  // Update the number of samples

  lcd.setCursor(0, 1);
  lcd.print("Samples = ");
  lcd.print(Samples);

 delay(100);
}

//////////////////////////////////
void ReadButtons()
{
  if (digitalRead(PB12) == HIGH)
  {
    if (Samples < MAX_SAMPLES)
    {
      Samples++;
    }
  }


  if (digitalRead(PB13) == HIGH)
  {
    if (Samples > 0)
    {
      Samples--;
    }
  }
}

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