-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadADC24bit.c
89 lines (77 loc) · 1.76 KB
/
readADC24bit.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
// 24 bit ADC
//gcc -o 2readADC readADClog.c minIni.c -l wiringPi
#include <wiringPi.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "minIni.h"
#define CLK 14
#define DATA 13
const char inifile[] = "/home/pi/config.ini";
int main() {
int i;
int value;
int twoCompVal;
int x =0;
float zeroweight;
float weight;
int y = 0;
int z = 0;
float calibVal;
wiringPiSetup ();
pinMode (CLK, OUTPUT);
pinMode (DATA, INPUT);
digitalWrite (CLK, 0);
//Calibrate based on current load
/* delay(500);
for(x=0;x<20;x++)
{
if (digitalRead (DATA) == 0)
{
zeroweight = (zeroweight + rawADC(twoCompVal));
delay(50);
}
else{x=x-1;}
}
zeroweight = zeroweight/(x);
*/
zeroweight = ini_getl("dataVars", "tare", -1283217, inifile);
//zeroweight = -1283217;
calibVal =ini_getl("dataVars", "calibrationVal", 38500, inifile);
//Read the raw ADC value and average out over 10 reads
while(weight ==0)
{ weight=0;
if (digitalRead (DATA) == 0)
{printf("b");
for(y = 0; y<10; y++)
{if (digitalRead (DATA) == 0)
{
printf("c");
weight = weight + ((rawADC(twoCompVal) - zeroweight)/calibVal);
}
else {y = y-1;
printf("d");
}
}
weight = weight/(y);
printf("%.2f \n", weight);
}
}
}
int rawADC(value, twoCompVal, i)
{
if (digitalRead (DATA) == 0)
{
value = 0;
for (i = 0 ; i < 25 ; ++i)
{
digitalWrite (CLK, 1) ;
delayMicroseconds (1) ;
value = (value << 1) | digitalRead (DATA) ;
digitalWrite (CLK, 0) ;
delayMicroseconds (1) ;
}
if (value >= (1<<23)) {twoCompVal=(value<<8)>>8;}
else {twoCompVal=value;}
}
}