Spark Logic

[SOLVED] VCO

ted Wed Sep 05, 2018 1:32 pm

Hi
I am trying to make a VCO based on voltmeter and generator programs, what I need is control Overflow of the timer by voltage applied to PA7.

pwmtimer3.setOverflow(1000 - 1);

Replies

ted Thu Sep 06, 2018 3:55 pm

I did averaging, the last digit on LCD is stable, however this not affect yet quality of the sound.
//http://www.stm32duino.com/viewtopic.php?f=19&t=4082
//edogaldo

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

int ain;
int ovfDiv2;
float volt;
unsigned int total; // can hold max 64 readings

void setup() {
pinMode(PA6, INPUT_ANALOG);
lcd.begin(16, 2);
pinMode(PB0, PWM);
Timer3.setPrescaleFactor(72);
}

void loop() {
////////////////////
for (int x = 0; x < 64; x++) { // multiple analogue readings for averaging
total = total + analogRead(PA6); // add each value to a total
}
volt = (total / 64.0) * 3.3; // convert readings to volt
//////////////////////
ain = analogRead(PA6);
//ovfDiv2 = map(ain,0,50,4095,5000); // freq between 10KHz and 100Hz
//ovfDiv2 = map(ain,0,1000,10000,0); // freq between 100Hz and 10kHz: ain=0 -> freq=100Hz - ain=4095 -> freq=10kHz
// ovfDiv2 = map(total,0,1000,10000,0);
// ovfDiv2 = ain/5; // freq between 10KHz and 100Hz
ovfDiv2 = map( total,0,50,4095,5000); // freq between 10KHz and 100Hz

Timer3.pause();
Timer3.setOverflow(2*ovfDiv2-1);
Timer3.setCompare(TIMER_CH3, ovfDiv2);
Timer3.refresh();
Timer3.resume();
// volt = (ain * 3.3) / 4095.0;
volt = (total /64.0)* 3.3 / 4095.0;
lcd.setCursor(0, 0);
lcd.print(volt);
total = 0; // reset value
delay(10);
}

ted Thu Sep 06, 2018 4:19 pm

for Uin 0V -1V = 100 Hz, changing the voltage is changing the pulse width not frequency, then frequency is doubled (1V-2V) and the same only pulse with is responding to voltage changes and so on. Frequency jumping steps are decreasing after 2V.
//http://www.stm32duino.com/viewtopic.php?f=19&t=4082
//edogaldo

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

int ain;
int ovfDiv2;
float volt;

void setup() {
pinMode(PA6, INPUT_ANALOG);
lcd.begin(16, 2);
pinMode(PB0, PWM);
Timer3.setPrescaleFactor(72);
}

void loop() {
ain = analogRead(PA6);
// ovfDiv2 = map(ain,0,4095,50,5000); // freq between 100Hz and 10kHz: ain=0 -> freq=100Hz - ain=4095 -> freq=10kHz
ovfDiv2 = 5050 - map(ain,0,4095,50,5000); // freq between 100Hz and 10kHz: ain=0 -> freq=100Hz - ain=4095 -> freq=10kHz
Timer3.pause();
Timer3.setOverflow(2*ovfDiv2-1);
Timer3.setCompare(TIMER_CH3, ovfDiv2);
Timer3.refresh();
Timer3.resume();
volt = (ain * 3.3) / 4095.0;
lcd.setCursor(0, 0);
lcd.print(volt);
delay(10);
}

edogaldo Thu Sep 06, 2018 4:41 pm

Try to explain the relationship you would expect between the voltage and the frequency.

ted Thu Sep 06, 2018 5:06 pm

@stevestrong
100Hz all the time, pulse duty is changing smoothly from 50% to 0%, nice and stable signal, now I need to change this to make frequency changes not the width ?

ted Thu Sep 06, 2018 5:09 pm

[edogaldo โ€“ Thu Sep 06, 2018 4:41 pm] โ€“
Try to explain the relationship you would expect between the voltage and the frequency.

smooth frequency changes

stevestrong Thu Sep 06, 2018 5:22 pm

int ovfDiv = 5050 - ain;
Timer3.pause();
Timer3.setOverflow(ovfDiv-1);
Timer3.setCompare(TIMER_CH3, ovfDiv/2);
Timer3.refresh();
Timer3.resume();

ted Thu Sep 06, 2018 5:53 pm

from 0V to 0.5V = 10000 Hz โ€“ 800 Hz, frequency is changing not bad, from 0.5V to 3.3V frequency is changing slower and the pulse width is more effective by voltage changes.

ted Thu Sep 06, 2018 6:07 pm

from 0V to 1V only the width is changing, after that frequency is changing not bad ( 300 Hz-1000hz.)

edogaldo Thu Sep 06, 2018 6:54 pm

try this:
ovfDiv2 = 500000/map(ain,0,4095,100,10000); // freq between 100Hz and 10kHz: ain=0 -> freq=100Hz - ain=4095 -> freq=10kHz
Timer3.pause();
Timer3.setOverflow(2*ovfDiv2-1);
Timer3.setCompare(TIMER_CH3, ovfDiv2);
Timer3.refresh();
Timer3.resume();

ted Thu Sep 06, 2018 9:14 pm

@ edogaldo
Perfect, 100Hz-10 000Hz, starts responding @ 0.05V = very sensitive and accurate.
@ 0.05V , the first pulse is stable, next 3 pulses are shaking a little bit and the 4th is again stable, for another voltages similar, this is giving sound not very pure bat acceptable.
thanks guys for helping.

ted Thu Sep 06, 2018 9:24 pm

what is it the magic number 500 000 ?

edogaldo Thu Sep 06, 2018 10:23 pm

You set the timer clock to 1MHz so your period (in us) is 1000000/freq and half period (50% duty cycle) is (1000000/freq)/2=500000/freq.

Sign in to reply