-
Notifications
You must be signed in to change notification settings - Fork 0
/
dacControl.adb
111 lines (84 loc) · 2.97 KB
/
dacControl.adb
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
with STM32.Device; use STM32.Device;
with STM32.Board; use STM32.Board;
with STM32.GPIO; use STM32.GPIO;
with Ada.Real_Time; use Ada.Real_Time;
with STM32.DAC; use STM32.DAC;
package body dacControl is
Resolution : constant DAC_Resolution := DAC_Resolution_12_Bits;
-- Arbitrary, change as desired. Counts will automatically adjust.
Max_Counts : constant Word := (if Resolution = DAC_Resolution_12_Bits
then Max_12bit_Resolution
else Max_8bit_Resolution);
procedure dacInit is
begin
if isInit then
return;
end if;
isInit := True;
end dacInit;
function DacCodeFromVoltage (setVoltage : OutputVoltage_t) return DacCode_t is
DacVoltage : Float := 0.0;
DacPercent : Float := 0.0;
begin
DacVoltage := (((setVoltage + 13.82) / 8.455));
DacPercent := DacVoltage / 3.0;
return Word (Float'Rounding (DacPercent * Float (Max_Counts)));
end DacCodeFromVoltage;
task body dacTask is
type upOrDown is (Up, Down);
use type Word;
Period : constant Time_Span := Milliseconds (100); -- arbitrary
-- subtype counts is Integer range 0 .. 10;
-- count : counts := 0;
Next_Release : Time := Clock;
Output_Channel : constant DAC_Channel := Channel_2;
procedure ConfigureDAC_GPIO (Output_Channel : DAC_Channel);
procedure ConfigureDAC_GPIO (Output_Channel : DAC_Channel) is
Output : constant GPIO_Point := (if Output_Channel = Channel_1
then DAC_Channel_1_IO
else DAC_Channel_2_IO);
Config : GPIO_Port_Configuration;
begin
Enable_Clock (Output);
Config.Mode := Mode_Analog;
Config.Resistors := Floating;
Configure_IO (Output, Config);
end ConfigureDAC_GPIO;
Value : Word := 0;
K : Word := 0;
upDown : upOrDown := Up;
begin
ConfigureDAC_GPIO (Output_Channel);
Enable_Clock (DAC_1);
Reset (DAC_1);
Select_Trigger (DAC_1, Output_Channel, Software_Trigger);
Enable_Trigger (DAC_1, Output_Channel);
Enable (DAC_1, Output_Channel);
Next_Release := Next_Release + Period;
delay until Next_Release;
loop
Toggle (TestPoint);
if upDown = Up then
K := K + 1;
if K >= 88 then
upDown := Down;
end if;
else
K := K - 1;
if K = 12 then
upDown := Up;
end if;
end if;
Value := DacCodeFromVoltage (setVoltage);
Set_Output
(DAC_1,
Output_Channel,
Value,
Resolution,
Right_Aligned);
Trigger_Conversion_By_Software (DAC_1, Output_Channel);
Next_Release := Next_Release + Period;
delay until Next_Release;
end loop;
end dacTask;
end dacControl;