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

Prevent sensor from continuously sense

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



Joined: 18 Sep 2008
Posts: 3

View user's profile Send private message

Prevent sensor from continuously sense
PostPosted: Thu Feb 26, 2009 8:37 pm     Reply with quote

I'm developing a sensor system that will detect the position of a human. Below is my program:
Code:

#include <16f877a.h>
#use delay (clock = 40000000)
#fuses hs,noprotect,nowdt,nolvp

#BYTE PORTA=5
#BYTE PORTB=6
#BYTE PORTC=7

#include <flex_lcd.c>
#include <kbd.c>

void main()
{
char k;
set_tris_a (0x0f);
set_tris_b (0);
set_tris_c (1);

lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
delay_ms(1000);

   do
   {

      if(portc == 0b00000001)
      {
         lcd_putc("Room 1");
         delay_ms(100);
      }   
         
  else if(portc == 0b00000011)
      {
        lcd_putc("Kitchen");
        delay_ms(100);
      }
         
             
   }while(true);
}

The problem is when sensor continuously operates, the LCD will repeat the display, e.g. kitchen. How to make the sensor detect once even if the sensor still continuously on?

TQ
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

re
PostPosted: Thu Feb 26, 2009 10:43 pm     Reply with quote

you could compare the current position with the previous position, if there is change in the position you could display it, otherwise just ignore.

when the program starts first, the previous position value is zero, read the position once and display it. now read and compare with this position, display the position, only when a change is detected.

hope this was helpful

thanks
aa
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Feb 27, 2009 3:05 am     Reply with quote

use a copy of portc and only check it if it has changed
I would also use a switch statement especially if this will be extended, just looks nicer.

Code:

  int oldportc = portc;
  int newportc = oldportc;

   do
   {
      newportc = portc;
      if (newportc != oldportc )  // Only do something if it has changed
      {
          oldportc = newportc;
          switch(newportc)
          {
             case 0b00000001:
                lcd_putc("Room 1");
                break;
             case 0b00000001:
                lcd_putc("Kitchen");
                break;
          }
          // delay_ms(100);  // With the current code you may as well put this here, Don't think it is needed anyway!
       }
   }while(true);


The reason I use newport=portc is because everytime you read the port it potentially could have changed so it is best to only read it once. I don't see it being a problem with your code though so you could just leave it the way it is.

If the code changes you may want to move the delay back.

Not sure why the delay is in there anyway, should not be required unless the port changes faster tham 100ms and you need the delay!
f_sven
Guest







PostPosted: Fri Feb 27, 2009 4:36 am     Reply with quote

Code:

  int oldportc = portc;
  int newportc = oldportc;

   do
   {
      newportc = portc;
      if (newportc != oldportc )  // Only do something if it has changed
      {
          oldportc = newportc;
          switch(newportc)
          {
             case 0b00000001:
                lcd_putc("Room 1");
                break;
             case 0b00000010:
                lcd_putc("Kitchen");
                break;
          }
          // delay_ms(100);  // With the current code you may as well put this here, Don't think it is needed anyway!
       }
   }while(true);


Thanks for reply. I try running this code but lots of error appear. Actually the basic idea is to read the sensor pulse in short time. Means the pic read the pulse once even the sensor have long pulse.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Feb 27, 2009 5:55 am     Reply with quote

A few mods to make it work

Code:

#include <16f877a.h>
#use delay (clock = 40000000)
#fuses hs,noprotect,nowdt,nolvp

#BYTE PORTA=5
#BYTE PORTB=6
#BYTE PORTC=7

//#include <flex_lcd.c>
#include <kbd.c>

void main()
{
char k;
int oldportc;
int newportc;

set_tris_a (0x0f);
set_tris_b (0);
set_tris_c (1);

//lcd_init();
kbd_init();
//lcd_putc("\fReady...\n");
delay_ms(1000);


   do
   {
      newportc = portc;
      if (newportc != oldportc )  // Only do something if it has changed
      {
          oldportc = newportc;
          switch(newportc)
          {
             case 0b00000001:
//                lcd_putc("Room 1");
                break;
             case 0b00000011:
//               lcd_putc("Kitchen");
                break;
          }
          // delay_ms(100);  // With the current code you may as well put this here, Don't think it is needed anyway!
       }
   }while(true);
}


I commented all the lcd routines out because I could not find flex_lcd.c, is this one of your files or a CCS driver ?

I did not look to extensively
Guest








PostPosted: Fri Feb 27, 2009 6:22 am     Reply with quote

Thanks..its works!! Very Happy
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