View previous topic :: View next topic |
Author |
Message |
edi
Joined: 22 Dec 2003 Posts: 82
|
2 sensors handling |
Posted: Thu Mar 04, 2004 4:44 am |
|
|
Hi,
I have 2 digital sensors connected to 2 I/O's pins, one to PORTA.2 and one to PORTA.3.
I have a good function that handle the reading of the sensors.
My question is: how can I use the same reading function, for both sensors.
I want to call the function once to handle PORTA.2, and once to handle PORTA.3.
Thanks,
Edi |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Mar 04, 2004 8:26 am |
|
|
I would read both sensors at the same time if they were on diferent pins and of the same type. It will cost half as much time. |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Thu Mar 04, 2004 8:53 am |
|
|
I can't do that because they are not working at the same time. |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Mar 04, 2004 9:10 am |
|
|
Post your good function. |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Thu Mar 04, 2004 3:52 pm |
|
|
Hi,
Neutone, you can see the function that works now for Sensor1.
I need to run it also for Sensor2, currently I copy the function and change Sensor1 -> Sensor2, but isn't an elegant way to run the same function one time for Sensor1 (just point the function to the specific I/O bit - portA.2)
and second time for Sensor2 (point the function to I/O bit - portA.3)
Thanks in advance,
Edi
Code: | #include <16f877a.h>
//#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use fast_io(A)
#use fast_io(B)
#byte porta=5
#byte trisa=0x85
#byte portb=6
#byte trisb=0x86
#bit Sensor1 = porta.2
#bit Sensor2 = porta.3
.
.
.
.
.
.
int8 read_code_sensor1()
{
int8 err=0;
int8 index;
int16 timeout;
for (index=0; index<5; index++) // clear the last read code
{
press[index]=0;
}
for (index=0; index<5; index++)
{
delay_ms(10);
if (Sensor1==0)
{
delay_ms(10); // debounce
if (Sensor1==0)
{
LED = 0; // turn on led
do
{
press[index]++;
delay_us(30);
if (press[index]>ProgTime)
{
LED = 1;
return 2;
}
}
while (Sensor1==0);
LED = 1;
delay_ms(10);
}
}
timeout=0;
do
{
timeout++;
delay_us(50);
if (timeout>ProgTime)
{
return 2;
}
}
while ((Sensor1==1) && (index<4));
}
for(index=0; index<5; index++)
{
if (press[index]==0)
return 2;
}
return 1;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Mar 04, 2004 4:20 pm |
|
|
Do you need a seperate press array for both sensors or can you use the same for both? I am assuming you have continous pulses on each sensor input and thats why you cant read both at the same time? I would pass a Flag to the function to select the sensor to be read.
In each place you find
Sensor1==0
replace with
(!Sensor1&&Flag)||(!Sensor2&&!Flag)
In each place you find
Sensor1==1
replace with
(Sensor1&&Flag)||(Sensor2&&!Flag) |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Sat Mar 06, 2004 3:36 pm |
|
|
Good idea,
Thanks a lot. |
|
![](templates/subSilver/images/spacer.gif) |
|