CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

2 sensors handling

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

2 sensors handling
PostPosted: Thu Mar 04, 2004 4:44 am     Reply with quote

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
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 8:26 am     Reply with quote

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.
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 8:53 am     Reply with quote

I can't do that because they are not working at the same time.
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 9:10 am     Reply with quote

Post your good function.
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 3:52 pm     Reply with quote

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;
}

Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Mar 04, 2004 4:20 pm     Reply with quote

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)
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Sat Mar 06, 2004 3:36 pm     Reply with quote

Good idea,

Thanks a lot.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group