-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainTestAdda.c
104 lines (85 loc) · 2.76 KB
/
mainTestAdda.c
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
/**
* @file mainTestAdda.c
* @author Boris Bocquet <borisboc@free.fr>
* @date May, 2018
* @brief Testing AD-DA-WS-RPI lib
*
* @details Usefull to perform Analog to Digital(ADC, using ADS1256 circuit) and Digital to Analog (DAC, using DAC8552).
* @todo More code especially for DAC8552.
*/
// This Source Code Form is subject to the terms of the
// GNU Lesser General Public License V 3.0.
// If a copy of the GNU GPL was not distributed
// with this file, You can obtain one at https://www.gnu.org/licenses/lgpl-3.0.en.html
#include "AD-DA-WS-RPI/AD-DA-WS-RPI.h"
/**
* @brief Simple code to get the voltage on each 8 inputs (single ended) using the ADC1256 and write the input 1 value to output 1 using DAC8552.
* You can use the jumper of the waveshare board to connect to 5V (power supply and vref), and connect ADO to ADJ (the potentiometer) and DAC to LEDA.
*
* @return int
*/
int main(void)
{
printf("Starting Test ADDA\r\n");
int initSpi = spi_init();
if (initSpi != 1)
{
printf("SPI init failed with code %d\r\n", initSpi);
return -55;
}
printf("SPI initialized\r\n");
//Prepare an array with the 8 single ended inputs to read [0,1,2,3,4,5,6,7]
int NbChannels = 8;
int MainLoop = 0;
int RetCode = 0;
uint8_t *Channels = malloc(NbChannels * sizeof(uint8_t));
uint8_t Ch;
for (Ch = 0; Ch < NbChannels; Ch++)
{
*(Channels + Ch) = Ch;
}
//Write 0 to ouputs A and B
DAC8552_Write(channel_A, Voltage_Convert(5.0, 0));
DAC8552_Write(channel_B, Voltage_Convert(5.0, 0));
while (1 == 1)
{
printf("ADC_DAC_Init\r\n");
int Id = 0;
int Init = ADC_DAC_Init(&Id, ADS1256_GAIN_1, ADS1256_100SPS);
if (Init != 0)
{
RetCode = -1;
break;
}
printf("init done !\r\n");
int Loop;
for (Loop = 0; Loop < 100; Loop++)
{
int32_t *AdcValues = NULL;
ADS1256_ReadAdcValues(&Channels, NbChannels, SINGLE_ENDED_INPUTS_8, &AdcValues);
double *volt = ADS1256_AdcArrayToMicroVolts(AdcValues, NbChannels, 1.0 / 1000000.0);
printf("0 : %f V 1 : %f V 2 : %f V 3 : %f V 4 : %f V 5 : %f V 6 : %f V 7 : %f V \r\n",
volt[0], volt[1], volt[2], volt[3], volt[4], volt[5], volt[6], volt[7]);
DAC8552_Write(channel_A, Voltage_Convert(5.0, volt[0]));
free(AdcValues);
free(volt);
bsp_DelayUS(1000);
}
//Write 0 to ouputs A and B
DAC8552_Write(channel_A, Voltage_Convert(5.0, 0));
DAC8552_Write(channel_B, Voltage_Convert(5.0, 0));
printf("ADC_DAC_Close\r\n");
int CloseCode = ADC_DAC_Close();
if (CloseCode != 0)
{
RetCode = -2;
break;
}
MainLoop++;
//This loop proves that you can close and re-init pacefully the librairie. Prove it several times (e.g. 3) and then finish the code.
if (MainLoop >= 3)
break;
}
printf("Test ADDA finished with returned code %d\r\n", RetCode);
return RetCode;
}