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 support@ccsinfo.com

Counter code

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



Joined: 12 Apr 2010
Posts: 27

View user's profile Send private message

Counter code
PostPosted: Sat Sep 25, 2010 1:24 pm     Reply with quote

Code:

#include <16F877A.H>
#device ADC=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)


#include "C:\Users\deli\Desktop\COde\maximum\brum\Flexlcd2.c"

//==========================

#define      LED1     PIN_D7
#define      LED2     PIN_D6
#define      LED3     PIN_C4
#define      EXIT     PIN_C7
#define      EXIT2    PIN_C6
#define      ZERO     PIN_C5

void main(void)
{


       int16 temp_adc=0,counter1;
       int inout_counter=0,pir1,pir2,in,out;
       int temp;

        setup_adc(ADC_CLOCK_DIV_8);
        setup_adc_ports(PIN_A1);
        set_adc_channel(1); //read analog input from channel 1


        lcd_init();



          while(1)
            {
                pir1=0;
                pir2=0;
                in=0;
                out=0;

                temp_adc = read_adc();

               temp=5.00*temp_adc*100.00/1023.00;

               lcd_gotoxy(1,1);
               printf(lcd_putc,"count:%d",inout_counter);

               lcd_gotoxy(1,2);
               printf(lcd_putc,"Temp:%d",temp);



         {
            pir1 = input(PIN_D5);
            pir2 = input(PIN_D4);

            if(pir1==1)
          {
            output_high(EXIT);
            for(counter1=10000;counter1>0;counter1--)
            {
               pir2 = input(PIN_B7);
               delay_ms(1);
               if(pir2==1)
               in=1;
            }
            if(in==1)
            inout_counter++;
          }

          else if(pir2==1)
          {
            for(counter1=10000;counter1>0;counter1--)
            {
               output_high(EXIT2);
               pir1 = input(PIN_B6);
               delay_ms(1);
               if(pir1==1)
               out=1;
            }
            if(out==1)
            inout_counter--;
          }

          if(inout_counter==0)
           {
           //output_high(LED1);   //desired output
           //output_low(LED2);
           //output_low(EXIT);
           //output_low(EXIT2);
           output_low(LED2);
           output_low(LED3);
           output_low(LED1);
           output_high(ZERO);
         }

         else
         {
             if((temp<20))
            {

            output_high(LED1);
            output_low(LED2);
            output_low(LED3);

            }


            else if ((temp>= 20) && (temp <30))
            {
            output_high(LED2);
            output_low(LED1);
            output_low(LED3);

            }

            else
            {
            output_high(LED3);
            output_low(LED1);
            output_low(LED2);
         }
         }}

}}

Here is the code that I had managed to compile without error.

Here I'm using 2 PIR sensor

The situation that I had achieve regarding counter aspect is
if PIR 1 on , then PIR 2 on the counter will increment.

The situation is , the count only can increase, if the PIR 1 firstly off, before checking the second sensor(PIR2)

Here i want to ask, how to make it more reliable like;

When checking PIR2 condition, it will ignore the condition of PIR1 either on or off....and if PIR2 on-- it will make the count increase,...

The same goes for decrement section.

Or can anyone suggest any suitable counter approach like:
- isn't needed any IC counter?
- new way on counter-- isn't more suitable to use interrupts

*Actually I don't quite familiar with interrupts routine.
sorry for my weakness...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 5:44 pm     Reply with quote

Quote:
setup_adc_ports(PIN_A1);

I didn't try to solve your problem, but I noticed the line above is wrong.
That function does not use pin numbers as the parameter.
It works in your program, but it's because you're lucky. The number
represented by PIN_A1 just happens to enable pin A1 as an analog pin.

To find the correct value to use as the parameter, look in this section
of the 16F877a.h file:
Quote:

// Constants used in SETUP_ADC_PORTS() are:
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Sep 27, 2010 1:59 am     Reply with quote

Your problem lies here:-

Code:

      if(pir1==1)
          {
            output_high(EXIT);
            for(counter1=10000;counter1>0;counter1--)
            {
               pir2 = input(PIN_B7);
               delay_ms(1);
               if(pir2==1)
               in=1;
            }
            if(in==1)
            inout_counter++;
          }

          else if(pir2==1)  // <<<<<<<<<<------------------


The else forces it to only check if the previous (pir1==1) condition is false.
Remove the else and the check on pir2 will be independent of pir1.
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