-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsr.c
368 lines (307 loc) · 8.26 KB
/
fsr.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* File: Minicode.c
* Author: ColdFusionX
*
* Created on March 6, 2017, 1:50 PM
*/
#include <p18f4550.h>
#define LCD_EN LATCbits.LC7
#define LCD_RS LATCbits.LC6
#define LCDPORT PORTD
void lcd_delay(unsigned long int time)
{
unsigned int i , j ;
for(i = 0; i < time; i++)
{
for(j=0;j<100;j++);
}
}
void SendInstruction(unsigned char command)
{
LCD_RS = 0; // RS low : Instruction
lcd_delay(10);
LCD_EN = 1; // EN High
lcd_delay(10);
LCDPORT = command; // Command
lcd_delay(10);
LCD_EN = 0; // EN Low
lcd_delay(10);
}
void SendData(unsigned char lcddata)
{
LCD_RS = 1; // RS HIGH : DATA
lcd_delay(10);
LCD_EN = 1; // EN High
lcd_delay(10);
LCDPORT = lcddata; // DATA
lcd_delay(10);
LCD_EN = 0; // EN Low
lcd_delay(10);
}
void LCDDisplayStr(unsigned char *String)
{
while(*String)
{
SendData(*String);
String++;
}
}
void SetLineNumber(unsigned char linenum)
{
if(linenum == 1)
{
SendInstruction( 0x80 );
}
else
{
SendInstruction( 0xC5 );
}
}
void InitLCD(void)
{
SendInstruction(0x38); //8 bit mode, 2 line,5x7 dots
lcd_delay(5);
SendInstruction(0x06); // entry mode
lcd_delay(5);
SendInstruction(0x0C); //Display ON cursor OFF
lcd_delay(5);
SendInstruction(0x01); //Clear display
lcd_delay(5);
}
void lcd_clear(void)
{
SendInstruction(0x01);
}
void ADCInit(void)
{
TRISEbits.RE2 = 1; //ADC channel 7 input
ADCON1 = 0b00000111; //Ref voltage Vdd & Vss;AN0-AN7 channels
ADCON2 = 0b10101110; //Right justified; Acquisition time 4T;Conversion clock Fosc/64
}
unsigned short Read_ADC(unsigned char Ch)
{
ADCON0 = 0b00011101 ; //ADC on; Select channel 7;
GODONE = 1; //Start Conversion
while(GO_DONE == 1 ); //Wait till A/D conv. is complete
return ADRES; //Return ADC result
}
unsigned long DisplayResult(unsigned short ADCVal)
{
unsigned short tempv;
int i;
unsigned long resistance,conductance,force;
tempv = ADCVal;
ADCVal = (5000/1024)*tempv; //Convert binary data to mV; 1bit <=> (5000/1024)mV
resistance=5000-ADCVal;
resistance *= 10000; // 10K resistor
resistance /= ADCVal; // FSR resistance in ohms.
conductance = 1000000; //We measure in microMhos.
conductance /= resistance; //Conductance in microMhos.
if(conductance<=1000)
force = conductance*100 / 80;
else
{
force=conductance-1000;
force/=30;
}
return force;
}
/* for(i=0;i<5;i++)
}
void main(void)
{
/*TRISB = 0x00; //set data port as output
TRISCbits.RC0 = 0; //EN pin
TRISCbits.RC1 = 0; // RS pin
InitLCD();
while(1) //Forever loop
{ SetLineNumber(1);
LCDDisplayStr("force=");
SendInstruction(0x86);
LCDDisplayStr("");
SendInstruction(0x89);
LCDDisplayStr("newton");
SetLineNumber(2);
LCDDisplayStr("pressure=");
SendInstruction(0xCA);
LCDDisplayStr("");
SendInstruction(0xCC);
LCDDisplayStr("pa");
}*/
/* int i;
unsigned short Ch_result,Ch_result1;unsigned long resistance,conductance,force1,force2,pressure,temp;
TRISD = 0x00; //PORTD connected to LCD is output
ADCInit();
InitLCD();
TRISCbits.RC6 = 0; //EN pin
TRISCbits.RC7 = 0; // RS pin
do
{
Ch_result = Read_ADC(7);
lcd_delay(20);
Ch_result1=Read_ADC(7);
//for 1st result
temp =Ch_result ;
temp = (5000/1024)*temp; //Convert binary data to mV; 1bit <=> (5000/1024)mV
resistance=5000-temp;
resistance *= 10000; // 10K resistor
resistance /= temp; // FSR resistance in ohms.
conductance = 1000000; //We measure in microMhos.
conductance /= resistance; //Conductance in microMhos.
force1 = conductance / 80;
//for 2nd result
temp =Ch_result1 ;
temp = (5000/1024)*temp; //Convert binary data to mV; 1bit <=> (5000/1024)mV
resistance=5000-temp;
resistance *= 10000; // 10K resistor
resistance /= temp; // FSR resistance in ohms.
conductance = 1000000; //We measure in microMhos.
conductance /= resistance; //Conductance in microMhos.
force2 = conductance / 80;
} while(force2<force1);
Ch_result = Read_ADC(7);
DisplayResult(Ch_result);
lcd_delay(20);
lcd_delay(20);
}
*/
void main()
{
short fsrReading,fsrVoltage,fsrResistance,fsrForce,fsrReading1,fsrVolt,fsrResistance1,fsrForce1;
long fsrConductance,fsrConductance1;
unsigned char text[16],i;
// unsigned short Ch_result,Ch_result1;unsigned long resistance,conductance,force1,force2,pressure,temp;
TRISD = 0x00; //PORTD connected to LCD is output
TRISB = 0x00;
TRISEbits.RE1 = 0;
TRISCbits.RC6 = 0; //RS pin
TRISCbits.RC7 = 0; // EN pin
ADCInit();
InitLCD();
while(1) {
fsrReading = Read_ADC(7);
fsrReading1 =fsrReading;
SetLineNumber(1);
LCDDisplayStr("Analog reading = ");
SetLineNumber(2);
for( i=10 ; i>0 ; i--) //Display string on LCD
{
text[i]=(fsrReading1%10)+'0';
fsrReading1/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
lcd_clear();
lcd_delay(2000);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage =(5500/1024)*fsrReading;
fsrVolt=fsrVoltage;
SetLineNumber(1);
LCDDisplayStr("Voltage in mV");
SetLineNumber(2);
for( i=10 ; i>0 ; i--) //Display string on LCD
{
text[i]=(fsrVolt%10)+'0';
fsrVolt/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
lcd_delay(2000);
lcd_clear();
if (fsrVoltage == 0) {
SetLineNumber(1);
LCDDisplayStr("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V yay math!
fsrResistance = 5000 - fsrVoltage; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance *= 10000; // 10K resistor
fsrResistance /= fsrVoltage;
fsrResistance1 =fsrResistance;
SetLineNumber(1);
LCDDisplayStr("FSR-(ohms)=");
SetLineNumber(2);
for( i=10; i>0 ; i--) //Display string on LCD
{
text[i]=(fsrResistance1%10)+'0';
fsrResistance1/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
lcd_clear();
lcd_delay(2000);
fsrConductance = 1000000; // we measure in micromhos so
fsrConductance /= fsrResistance;
fsrConductance1 =fsrConductance ;
SetLineNumber(1);
LCDDisplayStr("Conduc.(uMhos)");
SetLineNumber(2);
for( i=10; i>0 ; i--) //Display string on LCD
{
text[i]=( fsrConductance1%10)+'0';
fsrConductance1/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
lcd_clear();
lcd_delay(2000);
// Use the two FSR guide graphs to approximate the force
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
SetLineNumber(1);
LCDDisplayStr("Force in N: ");
SetLineNumber(2);
for( i=10 ; i>0 ; i--) //Display string on LCD
{
text[i]=( fsrForce%10)+'0';
fsrForce/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
SetLineNumber(1);
LCDDisplayStr("Force in N ");
SetLineNumber(2);
for( i=10 ; i>0 ; i--) //Display string on LCD
{
text[i]=( fsrForce%10)+'0';
fsrForce/ = 10;
}
for(i=0;i<=10;i++) //Display string on LCD
{
SendData(text[i]);
}
lcd_clear();
lcd_delay(2000);
}
}
if (fsrReading < 10) {
PORTB=0xC0;
} else if (fsrReading < 200) {
PORTB=0xE0;
} else if (fsrReading < 500) {
PORTB=0xF8;
} else if (fsrReading < 800) {
PORTB=0xFC;
} else {
PORTEbits.RE1=0;
PORTB=0xFE;
}
lcd_delay(1000);
}
SetLineNumber(1);
LCDDisplayStr("--------------------");
lcd_delay(10000);
}