-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODE FOR SLAVE.c
269 lines (245 loc) · 5.66 KB
/
CODE FOR SLAVE.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <reg51.h>
//ACCEPTING DATA FROM ADC808 THROUGH PORT P1
#define output P1
//CHANNEL SELECTING PINS OF ADC808
sbit add_a = P2 ^ 0;
sbit add_b = P2 ^ 1;
sbit add_c = P2 ^ 2;
//CHANNEL ENABLING PIN OF ADC808
sbit al = P2 ^ 3;
//READING CONVERTING DATA FROM ANALOG TO DIGITAL OF ADC808
sbit rd = P2 ^ 4;
//START CONVERTING DATA OF ADC808
sbit wr = P2 ^ 5;
//WAITING FOR CONVERTED DATA TO FINISH PIN
sbit INTR = P2 ^ 6;
//PIN FOR STEPPER MOTOR
sbit winding1 = P3 ^ 4;
sbit winding2 = P3 ^ 5;
sbit winding3 = P3 ^ 6;
sbit winding4 = P3 ^ 7;
//PIN FOR MOTOR
sbit motor = P3 ^ 2;
//PROTOTYPING OF FUNCTIONS
void condition1(unsigned char);
void condition2(unsigned char);
void condition3(unsigned char);
unsigned char adc1();
unsigned char adc2();
unsigned char adc3();
//PROGRAM FUNCTIONS
void TimerDelay();
void rec() interrupt 4
{
float a, b, c;
if (RI == 1)
{
if (SBUF == 'V') //IF SBUF==V THAN READ DATA FROM CHANNEL 1 AND SEND IT TO MASTER
{
condition1(adc1());
}
if (SBUF == 'P') //IF SBUF==P THAN READ DATA FROM CHANNEL 2 AND SEND IT TO MASTER
{
c = adc2();
a = (5.0 / 256) *c; //PRESSURE FORMULA
b = ((a / 5.0) + 0.09) / 0.009 - 1; //PRESSURE FORMULA
condition2(b);
}
if (SBUF == 'T') //IF SBUF==T THAN READ DATA FROM CHANNEL 3 AND SEND IT TO MASTER
{
condition3(adc3());
}
RI = 0;
TR0 = 1;
}
//RECIEVINF DATA FROM MASTER
}
void TimerDelay() //Generating 65535 * 1.085us=71.1ms delay
{
TL0 = 00; //INITIAL VALUES ARE STARTING FROM ZERO
TH0 = 00;
TR0 = 1; //STARTING TIMER
while (TF0 == 0); //WAIT FOR OVER FLOWING TIMER
TR0 = 0; //STOPING TIMER
TF0 = 0; //FORCING OVER FLOWING
}
unsigned char adc1() //ANALOG TO DIGITAL CONVERTION OF VOLTAGE SENSOR FUNCTION
{
unsigned char x;
add_a = 0; //SELECTING CHANNEL 0
add_b = 0;
add_c = 0;
al = 1; //ENABLING CHANNEL 0
wr = 1; //STARTING CONVERSION
al = 0;
wr = 0; //STOPING CONVERSION
while (INTR == 1); //WAITING FOR CONVERSION TO STOP
while (INTR == 0);
rd = 1; //READING CONVERTED DATA
x = output; //COPING CONVERTED DATA TO VARIABLE X
rd = 0; //STOP READING CONVERTED DATA
return x;
} //RETURNING VARIABLE
unsigned char adc2() //ANALOG TO DIGITAL CONVERTION OF PRESSURE SENSOR FUNCTION
{
unsigned char x;
add_a = 1; //SELECTING CHANNEL 1
add_b = 0;
add_c = 0;
al = 1;
wr = 1;
al = 0;
wr = 0;
while (INTR == 1);
while (INTR == 0);
rd = 1;
x = output;
rd = 0;
return x;
}
unsigned char adc3() //ANALOG TO DIGITAL CONVERTION OF TEMPRATURE SENSOR FUNCTION
{
unsigned char x;
add_a = 0; //SELECTING CHANNEL 2
add_b = 1;
add_c = 0;
al = 1;
wr = 1;
al = 0;
wr = 0;
while (INTR == 1);
while (INTR == 0);
rd = 1;
x = output;
rd = 0;
return x;
}
void condition1(unsigned char
var) //CONDITIONS FOR VOLTAGE SENSOR
{
var = 5.02 *
var / 256; //FORMULA FOR VOLTAGE SENSOR
SBUF =
var +0x30;
while (TI == 0); //TRANSMITTING DATA TO MASTER
TI = 0;
}
void condition2(unsigned char
var) //CONDITIONS FOR PRESSURE SENSOR
{
unsigned char unite, tenth;
tenth =
var / 10; //RECEIVING DATA AND EXTRACTING TENTH DIGIT
tenth = tenth + 0x30; //CONVERTING TO ASCII
SBUF = tenth; //STORING INTO SBUF
while (TI == 0);
TI = 0;
TimerDelay();
TimerDelay();
TimerDelay();
unite =
var % 10; //RECEIVING DATA AND EXTRACTING UNITE DIGIT
unite = unite + 0x30; //CONVERTING TO ASCII
SBUF = unite; //STORING INTO SBUF
while (TI == 0);
TI = 0;
}
void condition3(unsigned char
var) //CONDITIONS FOR TEMPRATURE SENSOR
{
unsigned char unite, tenth;
var =
var *1.966; //FORMULA FOR TEMPRATURE SENSOR
tenth =
var / 10;
tenth = tenth + 0x30;
SBUF = tenth;
while (TI == 0);
TI = 0;
TimerDelay();
TimerDelay();
TimerDelay();
unite =
var % 10;
unite = unite + 0x30;
SBUF = unite;
while (TI == 0);
TI = 0;
}
void condition3a(unsigned char
var) //CONDITIONS FOR TEMPRATURE SENSOR
{
unsigned char tenth;
var =
var * 2; //FORMULA FOR TEMPRATURE SENSOR
tenth =
var / 10;
if (tenth > 3)
{
motor = 1;
}
else
{
motor = 0;
}
}
void condition2a(unsigned char
var) //CONDITIONS FOR TEMPRATURE SENSOR
{
float g, h, tenth;
g = (5.0 / 256) *
var; //PRESSURE FORMULA
h = ((g / 5.0) + 0.09) / 0.009 - 1; //FORMULA FOR TEMPRATURE SENSOR
tenth = h / 10;
if (tenth >= 4 && tenth < 5) //IF PRESSURE IS below 50 PASCAL ROTATE STEPPER MOTOR 0 DEGREE CONDITION
{
winding1 = 1;
winding2 = 0;
winding3 = 0;
winding4 = 1;
}
if (tenth >= 5 && tenth < 6) //IF PRESSURE IS ABOVE 50 PASCAL ROTATE STEPPER MOTOR 45 DEGREE CONDITION
{
winding1 = 0;
winding2 = 0;
winding3 = 0;
winding4 = 1;
}
if (tenth >= 6 && tenth < 7) //IF PRESSURE IS ABOVE 60 PASCAL ROTATE STEPPER MOTOR 45 DEGREE CONDITION
{
winding1 = 0;
winding2 = 0;
winding3 = 1;
winding4 = 1;
}
if (tenth >= 7 && tenth < 8) //IF PRESSURE IS ABOVE 70 PASCAL ROTATE STEPPER MOTOR 135 DEGREE CONDITION
{
winding1 = 0;
winding2 = 0;
winding3 = 1;
winding4 = 0;
}
if (tenth >= 8 && tenth < 9) //IF PRESSURE IS ABOVE 80 PASCAL ROTATE STEPPER MOTOR 180 DEGREE CONDITION
{
winding1 = 0;
winding2 = 1;
winding3 = 1;
winding4 = 0;
}
}
void main() //MAIN FUNCTION
{
//DECLARING VARIABLE
TimerDelay(); //TIMER DELAY
INTR = 1; //INITIALIING ALL VALUES
rd = 0;
wr = 0;
al = 0;
TMOD = 0x21; //TIMER1 IN MODE 2(8 BIT AUTORELOAD) AND TIMER 1 IN MODE 1(16BIT)
TH1 = 0xFD; //BAUD RATE OF 9600
SCON = 0x50; //SCON REGISTER WITH MODE 2(8 DATA BITS AND 2 STOP AND START BIT)
IE = 0x90;
TR1 = 1; //STARTING TIMER 1
condition2a(adc2());
condition3a(adc3());
}