-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathFunctionGeneratorExtensions.cs
125 lines (112 loc) · 5.39 KB
/
FunctionGeneratorExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
////
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
////
using System;
namespace Iot.Device.Dac63004
{
/// <summary>
/// Extension methods for <see cref="Dac63004"/> class to generate functions.
/// </summary>
public static class FunctionGeneratorExtensions
{
/// <summary>
/// Generates a triangular wave with the specified parameters.
/// </summary>
/// <param name="dac">The <see cref="Dac63004"/> instance.</param>
/// <param name="slewRate">The slew rate configuration for X function register.</param>
/// <param name="stepSize"> The step size configuration from X function register.</param>
/// <param name="lowMargin">The low margin of the wave. Value for DAC. From 0 to 4096.</param>
/// <param name="highMargin">The high margin of the wave.Value for DAC. From 0 to 4096.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="lowMargin"/> or <paramref name="highMargin"/> are out of range.</exception>
/// <remarks>
/// More information about the function generator can be found in the datasheet section "7.4.5.2.1 Triangular Waveform Generation".
/// </remarks>
public static void GenerateTriangularWave(
this Dac63004 dac,
SlewRate slewRate,
CodeStep stepSize,
int lowMargin,
int highMargin)
{
if (lowMargin < 0
|| lowMargin > 4096
|| highMargin < 0
|| highMargin > 4096)
{
throw new ArgumentOutOfRangeException();
}
byte[] writeBuffer = new byte[2];
// write DAC-X-MARGIN-HIGH
writeBuffer[0] = (byte)(highMargin >> 4);
writeBuffer[1] = (byte)(highMargin << 4);
dac.WriteToRegister(Register.Reg01_DACX_MarginHigh, writeBuffer);
// write DAC-X-MARGIN-LOW
writeBuffer[0] = (byte)(lowMargin >> 4);
writeBuffer[1] = (byte)(lowMargin << 4);
dac.WriteToRegister(Register.Reg02_DACX_MarginLow, writeBuffer);
// write DAC-X-FUNC-CONFIG
ushort funcConfig = (ushort)((ushort)Wave.Triangular | (ushort)slewRate | (ushort)stepSize);
writeBuffer[0] = (byte)(funcConfig >> 8);
writeBuffer[1] = (byte)funcConfig;
dac.WriteToRegister(Register.Reg06_DACX_FunctionConfig, writeBuffer);
}
/// <summary>
/// Generates a sine wave with the specified parameters.
/// </summary>
/// <param name="dac"> The <see cref="Dac63004"/> instance.</param>
/// <param name="slewRate">The slew rate configuration for X function register.</param>
/// <param name="stepSize">The step size configuration from X function register.</param>
/// <param name="lowMargin">The low margin of the wave. Value for DAC. From 0 to 4096.</param>
/// <param name="highMargin">The high margin of the wave. Value for DAC. From 0 to 4096.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="lowMargin"/> or <paramref name="highMargin"/> are out of range.</exception>
public static void GenerateSawtoothWave(
this Dac63004 dac,
SlewRate slewRate,
CodeStep stepSize,
int lowMargin,
int highMargin)
{
if (lowMargin < 0
|| lowMargin > 4096
|| highMargin < 0
|| highMargin > 4096)
{
throw new ArgumentOutOfRangeException();
}
byte[] writeBuffer = new byte[2];
// write DAC-X-MARGIN-HIGH
writeBuffer[0] = (byte)(highMargin >> 4);
writeBuffer[1] = (byte)(highMargin << 4);
dac.WriteToRegister(Register.Reg01_DACX_MarginHigh, writeBuffer);
// write DAC-X-MARGIN-LOW
writeBuffer[0] = (byte)(lowMargin >> 4);
writeBuffer[1] = (byte)(lowMargin << 4);
dac.WriteToRegister(Register.Reg02_DACX_MarginLow, writeBuffer);
// write DAC-X-FUNC-CONFIG
ushort funcConfig = (ushort)((ushort)Wave.Sawtooth | (ushort)slewRate | (ushort)stepSize);
writeBuffer[0] = (byte)(funcConfig >> 8);
writeBuffer[1] = (byte)funcConfig;
dac.WriteToRegister(Register.Reg06_DACX_FunctionConfig, writeBuffer);
}
/// <summary>
/// Generates a sine wave with the specified parameters.
/// </summary>
/// <param name="dac"> The <see cref="Dac63004"/> instance.</param>
/// <param name="slewRate">The slew rate configuration for X function register.</param>
/// <remarks>
/// More information about the function generator can be found in the datasheet section "7.4.5.2.1 Triangular Waveform Generation".
/// </remarks>
public static void GenerateSineWave(
this Dac63004 dac,
SlewRate slewRate)
{
byte[] writeBuffer = new byte[2];
// write DAC-X-FUNC-CONFIG
ushort funcConfig = (ushort)((ushort)Wave.Sine | (ushort)slewRate);
writeBuffer[0] = (byte)(funcConfig >> 8);
writeBuffer[1] = (byte)funcConfig;
dac.WriteToRegister(Register.Reg06_DACX_FunctionConfig, writeBuffer);
}
}
}