1

Тема: регулировка частоты генераторов

Здравствуйте
Пытаюсь использовать кнопки для изменения частоты генераторов, но не получается, может кто то знает как это сделать ?

/* 

*/
#include <EEPROM.h>
#include <libmaple/dma.h>
#include <LiquidCrystal.h>
//LiquidCrystal lcd(PB9,PA11,PB15,PB14,PB13,PB12);
LiquidCrystal lcd(PB9, PC14, PB15, PB14, PB13, PB12);


//#define SAMPLES_TX 64// ++++++++++++++++++++++++++++++++++++++++++++++++++++


#define dt (1.0/SAMPLES_TX)
#define F(x)(0.5*(1+sin(TWO_PI*x)))

HardwareTimer Timer_4(4);
HardwareTimer Timer_2(2);
HardwareTimer Timer_3(3);


int Faza_b;
unsigned int micros_1, micros_2;
unsigned int millis_1, millis_2;
int adc_data_1, adc_data_2;
int val_TX[SAMPLES_TX];

const int LED_LCD = PA8;
int V_tic = 0;
int V_bat = 0;
int MENU_index = 1;

#define eeprom_adr 1
#define num_prof 4
#pragma pack(push, 1);
struct strMem
{
  float amp_TX[num_prof];
  int F_TX[num_prof];
  int Threshold[num_prof];
  int Comp_Phase[num_prof];
  int need_phase[num_prof];
  int Adjustment_Phase[num_prof];

  int Prof;
} Mem;

const int OUT_TX = PB8;
dma_tube_config dma_cfg_TX, dma_cfg2;
timer_dev *dev_TX = PIN_MAP[OUT_TX].timer_device;//Timer4
uint8 cc_channel_TX = PIN_MAP[OUT_TX].timer_channel;//Channel3

void TX_conf()
{
  Timer_4.pause();
  Timer_3.pause();
  Timer_3.setCount(0);
  Timer_4.setCount(0);

  set_sin_tab(Mem.Comp_Phase[Mem.Prof], Mem.amp_TX[Mem.Prof]);


  timer_dma_set_base_addr(dev_TX,  TIMER_DMA_BASE_CCR3);
  timer_dma_set_burst_len(dev_TX, 1);
  timer_dma_enable_req(dev_TX, cc_channel_TX);
  timer_set_reload(dev_TX, Mem.F_TX[Mem.Prof] );

  timer_set_prescaler(dev_TX, 0);
  Timer_4.setMasterModeTrGo(TIMER_CR2_MMS_COMPARE_OC3REF);

  dma_init(DMA1);
  dma_disable(DMA1, DMA_CH5);
  dma_cfg_TX.tube_dst = &(dev_TX->regs.gen->DMAR);
  dma_cfg_TX.tube_dst_size = DMA_SIZE_32BITS;
  dma_cfg_TX.tube_src = val_TX;
  dma_cfg_TX.tube_src_size = DMA_SIZE_32BITS;
  dma_cfg_TX.tube_nr_xfers = SAMPLES_TX;
  dma_cfg_TX.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
  dma_cfg_TX.tube_req_src = DMA_REQ_SRC_TIM4_CH3;
  dma_cfg_TX.target_data = 0;
  dma_tube_cfg(DMA1, DMA_CH5, &dma_cfg_TX);
  dma_enable(DMA1, DMA_CH5);



  Timer_3.setPrescaleFactor(1);
  Timer_3.setOverflow((SAMPLES_TX / 2) - 1);
  timer_oc_set_mode(TIMER3, 3, TIMER_OC_MODE_TOGGLE, TIMER_OC_PE);
  timer_oc_set_mode(TIMER3, 4, TIMER_OC_MODE_TOGGLE, TIMER_OC_PE);
  Timer_3.setCompare(TIMER_CH3, 4);
  Timer_3.setCompare(TIMER_CH4, 20);
  Timer_3.setSlaveFlags( TIMER_SMCR_TS_ITR3 | TIMER_SMCR_SMS_EXTERNAL  );

  Timer_3.refresh();
  Timer_4.refresh();
  Timer_3.resume();
  Timer_4.resume();
}



//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//SAMPLES_TX = frequency
void set_sin_tab(int faze, float sin_amp)
{

  float y;
  float maxy = 0;

  for (int i = 0; i < (SAMPLES_TX); i++) if ((y = F(dt * i)) > maxy) maxy = y;
  for (int i = 0; i < (SAMPLES_TX); i++) val_TX[i] = 1 + sin_amp * ((Mem.F_TX[Mem.Prof] - 1) / maxy) * F(dt * (i + faze)) ;

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void setup()
{
  pinMode(OUT_TX, PWM); //
  pinMode(PB0, PWM); //Yy
  pinMode(PB1, PWM); //Xy
  pinMode(PB12, INPUT_PULLUP); // +
  pinMode(PB13, INPUT_PULLUP); // -
  pinMode(LED_LCD, OUTPUT);

  lcd.begin(16, 2);
  init_eeprom_mem();
  TX_conf();
}


void loop()
{
  if (digitalRead(PB12) == LOW)
  {
    SAMPLES_TX++;
    delay(100);
  }
  if (digitalRead(PB13) == LOW)
  {
    SAMPLES_TX--;
    delay(100);
  }
  lcd.setCursor(0, 0);
  lcd.print(" Hz ");
  lcd.print(Hz );
}

void init_eeprom_mem()
{
  for (int i = 0; i < num_prof; i++)
  {
    Mem.amp_TX[i] = 1.0;
    Mem.F_TX[i] = 160;
  }
  Mem.Prof = 0;

}