• 0

Script Editor: Change duty ratio of a clock pattern every cycle


Question

Hello, I am a new user of Analog Discovery 2 device. I would like to output a pattern at DIO1 (for example) wherein the duty ratio is selected from a look-up table. I am trying to use the Script Editor. However, I am unsure what function/variable I should use to increment the index for the look-up table such that at the beginning of every cycle, the clock has the updated duty ratio. Here is a snapshot of my dummy code:

clear()

if(!('Patterns' in this)) throw "Please open a Patterns instrument";

var values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95];

Patterns.run();

for(var idx = 0; idx < 10; idx++){

	var d = values[idx];

	wait(FOR AN EDGE-TRIGGER OR SOME SIMILAR CLOCK EVENT) // I am unsure if wait is the correct choice. 

	Patterns.Channels.DIO1.Duty.value = d;

	idx = idx+1;

}

// Assuming that DIO1 has a set frequency of 1kHz, the duty should change from 10% to 95% every 1kHz in steps as listed in variable 'values'.

Any insights would be helpful.

 

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Hi @gupta

You can create a custom pattern with such duty factors.

With the default configuration of AD2 you have 1ki samples for such custom pattern but with the 4th device configuration up to 16ki.

The pattern can be created in WaveForms Pattern Custom Editor, external spreadsheet editor exported/imported csv, or with the Script tool like this:

var duty = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95];
var data = []
const res = 20
duty.forEach(function(d){ for(var i = 0; i < res; i++) data.push(i>=res*d/100?0:1)})
Patterns.Channels.DIO1.custom = data

if(Patterns.Channels.DIO1.custom.length!=data.length) print("too many samples",data.length)
else print("done", data.length)

image.thumb.png.0bf447a2104d25b81af0b84bb1e6b69b.png

image.png.bcc9a6f4bc12cacae0f8a5c17c4f8c9f.png

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now