-
Notifications
You must be signed in to change notification settings - Fork 1
/
gas.c
102 lines (82 loc) · 1.96 KB
/
gas.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
#include <wiringPi.h>
#include <stdio.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define ADC_CS 0
#define ADC_CLK 1
#define ADC_DIO 2
#define BEEP 3
uchar get_ADC_Result(void)
{
//10:CH0
//11:CH1
uchar i;
uchar dat1=0, dat2=0;
digitalWrite(ADC_CS, 0);
digitalWrite(ADC_CLK,0);
digitalWrite(ADC_DIO,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,0);
digitalWrite(ADC_DIO,1); delayMicroseconds(2); //CH0 10
digitalWrite(ADC_CLK,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,0);
digitalWrite(ADC_DIO,0); delayMicroseconds(2); //CH0 0
digitalWrite(ADC_CLK,1);
digitalWrite(ADC_DIO,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,0);
digitalWrite(ADC_DIO,1); delayMicroseconds(2);
for(i=0;i<8;i++)
{
digitalWrite(ADC_CLK,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,0); delayMicroseconds(2);
pinMode(ADC_DIO, INPUT);
dat1=dat1<<1 | digitalRead(ADC_DIO);
}
for(i=0;i<8;i++)
{
dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i);
digitalWrite(ADC_CLK,1); delayMicroseconds(2);
digitalWrite(ADC_CLK,0); delayMicroseconds(2);
}
digitalWrite(ADC_CS,1);
pinMode(ADC_DIO, OUTPUT);
return(dat1==dat2) ? dat1 : 0;
}
int main(void)
{
uchar tmp;
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
pinMode(ADC_CS, OUTPUT);
pinMode(ADC_CLK, OUTPUT);
pinMode(BEEP, OUTPUT);
int iter = 0;
while(1){
if(!(iter < 100)) {
iter = 0;
printf("Test? (J/N)\n");
char input[1];
scanf("%s",input);
if(input[0] != 'J') {
break;
}
}
pinMode(ADC_DIO, OUTPUT);
tmp = get_ADC_Result();
printf("%d\n",tmp);
if(tmp > 100){
digitalWrite(BEEP, 0);
delay(500);
digitalWrite(BEEP, 1);
delay(500);
}else{
digitalWrite(BEEP, 0);
}
iter++;
delayMicroseconds(2*60*1000);
}
digitalWrite(BEEP,0);
return 0;
}