-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADCBBB.c
251 lines (194 loc) · 7.19 KB
/
ADCBBB.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <fcntl.h>
#include <math.h>
#include <util/util.h>
#include <errno.h>
#define SYSFS_ADC_DIR "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"
#define SYSFS_ADC_DIR2 "/sys/bus/iio/devices/iio:device0/in_voltage6_raw"
#define MAX_BUFF 64
//check that the character is numeric
int caracter_valido(const char cad)
{
return cad >= '0' && cad <='9';
}
// converts an string into a integer
int mi_atoi(const char *cad, int tam)
{
int i=tam-1, entero=0, pos=1;
while (i>=0)
{
while (caracter_valido(cad[i])){
entero=entero+(cad[i]-48)*pos;
pos=pos*10;
i--;
}
pos=1;
i--;
}
return entero;
}
int main(void) {
int value;
int fd;
int value2;
int fd2;
char buf[MAX_BUFF];
char ch[5]; //Update
char ch2[5]; //Update
ch[4] = 0; //Update
int GPIOPin=60, /* GPIO1_28 or pin 12 on the P9 header */
printf("\nStarting GPIO output program\n");
FILE *myOutputHandle = NULL;
char setValue[4], GPIOString[4], GPIOValue[64], GPIODirection[64];
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
// Export the pin
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set direction of the pin to an output
if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
printf("Unable to open direction handle\n");
return 1;
}
strcpy(setValue,"out");
fwrite(&setValue, sizeof(char), 3, myOutputHandle);
fclose(myOutputHandle);
// export pin 2
GPIOPin=50;
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
// Export the pin
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set direction of the pin to an output
if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
printf("Unable to open direction handle\n");
return 1;
}
strcpy(setValue,"out");
fwrite(&setValue, sizeof(char), 3, myOutputHandle);
fclose(myOutputHandle);
while(1) {
//read adc ch0
snprintf(buf, sizeof(buf), SYSFS_ADC_DIR);
fd = open(buf, O_RDONLY);
read(fd,ch,4);
close(fd);
value= mi_atoi(ch, strlen(ch));
value= (value*100)/2667;
sleep(0.1);
//read adc ch 6
snprintf(buf, sizeof(buf), SYSFS_ADC_DIR2);
fd2 = open(buf, O_RDONLY);
read(fd2,ch2,4);
close(fd2);
value2= mi_atoi(ch2, strlen(ch));
// if the solar power is high and the current from the motor is low, then shut down the fuel cell and turn the solar charger on, otherwise the fuel cell is always on.
if (value > 80 && value2 < 1000){
GPIOPin=60;
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set output to low
if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
printf("Unable to open value handle\n");
return 1;
}
strcpy(setValue, "0"); // Set value low
fwrite(&setValue, sizeof(char), 1, myOutputHandle);
fclose(myOutputHandle);
sleep(0.5); // wait for 1 sec
GPIOPin=50;
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set output to high
if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
printf("Unable to open value handle\n");
return 1;
}
strcpy(setValue, "1"); // Set value high
fwrite(&setValue, sizeof(char), 1, myOutputHandle);
fclose(myOutputHandle);
sleep(0.5); // wait for 1 sec
}else{
GPIOPin=50;
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set output to low
if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
printf("Unable to open value handle\n");
return 1;
}
strcpy(setValue, "0"); // Set value low
fwrite(&setValue, sizeof(char), 1, myOutputHandle);
fclose(myOutputHandle);
sleep(0.5); // wait for 1 sec
GPIOPin=60;
sprintf(GPIOString, "%d", GPIOPin);
sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to export GPIO pin\n");
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char), 2, myOutputHandle);
fclose(myOutputHandle);
// Set output to high
if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
printf("Unable to open value handle\n");
return 1;
}
strcpy(setValue, "1"); // Set value high
fwrite(&setValue, sizeof(char), 1, myOutputHandle);
fclose(myOutputHandle);
sleep(0.5); // wait for 1 sec
}
printf("potencia solar: %d \n",value);
printf("corriente del motor %d \n",value2);
}
return 0;
}