Generator amplitude

Hi
This is a program for sine generator and AC voltmeter, how to make amplitude of the generator the same as measured ?
By changing the number of - amp = 35 - I can change amplitude of thegenerator manualy.
I was tryimg to use amp = volt, but is not working, between forward slash lines is the part related to amplitude.
Any help ?
Thanks.

#define SAMPLES 100
#include <libmaple/dma.h>
#include <LiquidCrystal.h>                            // TM
#include "EmonLib.h"
#include <math.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
EnergyMonitor emon1, emon2, emon3;                           //TM

float volt, avolt[50],  sumv, volt2, avolt2[50],  sumv2, powerFactor, apowerFactor[50], sumPF, catchPF, cur, acur[50], sumc, volt3;
char symbPF = 223;
dma_tube_config dma_cfg, dma_cfg2;
int flag1 = 0;
int out1 = PB7;
int val1[SAMPLES];

int16 shift = 0;
///////////////////////////////////
int amp = 35;
//int amp = volt;

/////////////////////////////////////
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 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_enable(DMA1, DMA_CH4);
  timer_resume(dev1);
}
///////////////////////////////////////////////////
void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 50 + amp * sin(stp * i);

  }
}
//////////////////////////////////////////////////////////
void setup() {
  int i;
  pinMode(out1, PWM);
  pinMode(PA7, INPUT);    // U

  lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
  emon1.voltage(PA6, 3000, 1.7);   // Voltage: input pin, calibration, phase_shift  U1
  emon2.voltage(PA7, 3000, 1.7);
  emon1.current(PA7, 111.1);         // Current

  Serial.begin(9600);
  timer_conf();
  dma_conf();
  dma_start();
  init_wave();
}

void loop() {
  sumv = 0;

  for (int i = 0; i < 50; i++)
  {
    emon1.calcVI(20, 200);         // Calculate all. No.of half wavelengths (crossings), time-out    TM
    emon2.calcVI(20, 200);
    avolt[i] = emon1.Vrms; // holds volt value
  }
  for (int i = 0; i < 50; i++)
  {
    sumv += avolt[i];
  }
  volt = sumv / 50;
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(volt);
}

I was tryimg to use amp = volt, but is not working,

It IS working. It is doing EXACTLY what you told it to do.

If what it actually does is not what you expect, it is your expectations that are wrong.

So, what does it actually do, and what did you expect it to do?

When measured amplitude is changing amplitude of the generator is not.

This appears to be sending samples via DMA to a DAC. Which hardware is it using and specifically what is the size of the DAC in bits and its maximum voltage range?

Pete

Once amp is assigned a value, the value is never changed. Why would you expect anything different from what you are seeing?

The = operator does not bind the value in a variable to that in another variable.

Because operator doesnt know how to do it.
STM32, 3V amplitude

This determines the amplitude of the wave

   val1[i] = 50 + amp * sin(stp * i);

The sin() function returns a value between plus and minus one. Therefore each val1[i] will be set to a value between 50-amp and 50+amp. Amp is defined to be 35, so the values in the val1 array will range from 15 to 85.
This is why I asked for the number of bits in the DAC. The value of 50 should be the centre range of the DAC. E.g. a 10-bit DAC would have a centre value of 511. This will then allow you to get the maximum range of values out of the DAC if you need the full range of voltage from 0 to 3V3. Then set amp to whatever gives you the actual voltage you need.
The peak-to-peak digital range will be 2amp and (in theory) this will correspond to a P2P output voltage of 3.3/(2amp) volts.
Correction: 3.32amp/1024. where 1024 is the full range of the DAC.

Pete

Thanks for explanation, but my problem is that amplitude of generator is not reacting to changes of measurements

my problem is that amplitude of generator is not reacting to changes of measurements

Shouldn't it be the other way round?

What changes have you tried?

Pete

amp = volt - only this one, I don't know other options.

What changes did you make to the value of amp or to the 50? Those are what will change the output of the DAC.

I'll assume for now that the DAC is 12 bits, in which case the centre value is 2047.
Use this in the for loop:

   val1[i] = 2047 + amp * sin(stp * i);

and change the value of amp from 35 to 512. The output from this should be about 3.32amp/4096 (note that I've corrected this formula in my msg #6). Thus, it should be about 0.825V peak-to-peak.

Pete

PaulS pointed out the (a) problem in #4, but I'll simplify it: you don't ever change the value of amp once you set it. If you want amp to change when you take a measurement, then you have to change amp.

@christop: Make your mind up.

don't ever change the value of amp once you set it

but

If you want amp to change when you take a measurement, then you have to change amp

Which is it? "don't ever change it", or "you have to change amp"?

Anyway, there is a problem in the code that generates the digital sine wave. It could generate a digital signal to the DAC with a maximum range from 0 to 100 (if amp was defined as 50), which would give an output of about 80mVpp if the DAC is 12 bits. If amp was defined to be any more than 50 the output would be very distorted. I've explained in #10 how to fix it.

Pete

christop:
then you have to change amp.

I can do that manually but I want the program to do that.

I want.
When measured signal is 1V I want the program to change amplitude of the generator to 1V, when measurement is 3V the program will make amplitude of the generator also 3V

The tests will shows there is DAC issue or not.

el_supremo:
@christop: Make your mind up.but
Which is it? "don't ever change it", or "you have to change amp"?

You didn't quote my whole statement. I said "you don't ever change the value of amp", which is an observation, not an imperative.

ted:
I can do that manually but I want the program to do that.

Arduino can't read your mind. You have to tell the program how and when to change amp. If you want amp to change when volt changes, just add this line:

amp = volt;

below this line:

volt = sumv / 50;

this

//int amp = 35;
int amp = volt;

and
}
volt = sumv / 50;
amp = volt;
lcd.setCursor(0, 1);
lcd.print("U=");
lcd.print(volt);
}

Is not working, LCD displays voltage correctly, amplitude of generator is not changing

which is an observation

OK. Even with "you" in there I read it as an imperative. Now I see what you meant.

amplitude of generator is not changing

However, @ted needs to fix the code which initializes the val1 array and calibrate values of 'amp' to output voltage as I've described in #10. Then start meddling with making the program handle the 'volt' input.

Pete

I made this changes

 // val1[i] = 50 + amp * sin(stp * i);
     val1[i] = 50 + volt * sin(stp * i);

What I should do here

int amp = 35;
//int amp = volt;
int volt = ???;

maybe something here

float volt, avolt[50], sumv;