Need help with sine wave generation | LabJack
 

Need help with sine wave generation

2 posts / 0 new
Last post
Vamsi Krishna Surya
vamsikrishnasurya's picture
Need help with sine wave generation

 

trying to create a sine wave form using T7 , previously i generated the same using U3 now i have kept all the code same and need to edit the T7 labjack commands which i am finding hard to figure out. can you please let me know how to reset pins of DAC to 0 initially and then how to write data to the DAC0 using what command?

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabJack;
using System.Diagnostics;
using System.Threading;

namespace t7_sine
{
    public partial class MainForm : Form
    {
        private Task _task10Hz = null;
        private CancellationTokenSource _task10HzToken = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnDac10_Click(object sender, EventArgs e)
        {
            try
            {
                if (_task10Hz == null)
                {
                    _task10HzToken = new CancellationTokenSource();
                    _task10Hz = Task.Factory.StartNew(() => { SineWaveGenerator(10, 1000); }, _task10HzToken.Token);
                    btnDac10.BackColor = Color.Green;
                }
                else
                {
                    _task10HzToken.Cancel();
                    _task10Hz.Wait();
                    _task10Hz = null;
                    _task10HzToken.Dispose();
                    _task10HzToken = null;
                    btnDac10.BackColor = SystemColors.Control;
                }
            }
            catch (LJM.LJMException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void SineWaveGenerator(double freq, double sampleRate)
        {
            int handle = 0;
            int devType = 0;
            int vout = 0;
            try
            {
                // Connection through USB
                LJM.OpenS("ANY", "ANY", "ANY", ref handle);

                LJM.eWriteName(DeviceDpi, LJM.ResetLog, 0, 0, 0);

                LJM.eWriteNames("DAC0", vout);

                var stopwatch = new Stopwatch();
                double rads = 0;
                double radstep = 4 * freq / sampleRate;
                double delay = 1000.0 / sampleRate;

                while (!_task10HzToken.IsCancellationRequested)
                {
                    stopwatch.Start();

                    // Calculate voltage
                    var value = Math.Sin(rads) * 32768 + 32768;
                    rads += radstep;

                    // Output to DAC0
                    LJM.eWriteNames(devType,handle, 0, value, 1, 0, 0);

                    // Delay
                    while (stopwatch.ElapsedMilliseconds < delay)
                    {
                        Thread.SpinWait(1);
                    }

                    stopwatch.Stop();
                }
            }
            catch (LJM.LJMException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}
 

LabJack Support
labjack support's picture
If you just want to do an

If you just want to do an update of DAC0 at any time, eWriteName() would be logical:

https://labjack.com/support/software/api/ljm/function-reference/ljmewritename

 

LJMError = LJM_eWriteName(handle, "DAC0", 2.5);

 

Let us know if you need more help and what programming language you are using.

 

Also see this:

https://labjack.com/support/app-notes/waveform-generation