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

Please help me to solve switch pressing function.

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



Joined: 25 Jul 2012
Posts: 20

View user's profile Send private message

Please help me to solve switch pressing function.
PostPosted: Wed Aug 08, 2012 1:17 am     Reply with quote

Dear Experts
I have a little bit logic of switch pressing. The below mention program is set and clear switchstatus,5 at every pressing of switch connected at PORTA,3. Now my requirement is that:
1- First it sense switch pressing.
2- On pressing of switch it check two points: That if switch is pressed for 1 second then it goto another mode and if switch is pressed for 03 second then it clear or set the switchstatus,5, and go out of that routine.
3- If switch is pressed for 01 second then it go in another mode and do following work:
Set the PORTC,1 and clear the PORTC,0 and now remain in this loop and continuous checking the switch connected at PORTA,3, on every pressing of switch it change the status of PORTC,0 and PORTC,1 from set to clear and clear to set. Also check if here also switch is pressed for 03 second then it change the switchstatus,5 and go out from that routine and comes to first routine. Please help me out to solve that problem.
Requirement are these.

Code:

#include <16F676.h>
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,MCLR
#use delay (clock=4000000) // 4MHz clock

#byte PORTA = 0x05
#byte PORTC = 0x07
#byte TRISA = 0x85
#byte TRISC = 0x87

void CPU_SETUP(void);

unsigned char temp;
unsigned char switchstatus;

void main()
{     
   CPU_SETUP();
          while(1)
         {

             if( !bit_test(PORTA,3) )      //   FRONT SWITCH
               {
                         while ( !bit_test(PORTA,3) )   // wait until switch is again high for debounce affect. change status when switch is low and again high.
                             {
                                   }
                        if(bit_test(switchstatus,5) )
         {
          bit_clear(switchstatus,5);
         }
         else
            {
             bit_set(switchstatus,5);
            }
                  }
         }
}

void CPU_SETUP()
{   
   setup_comparator(NC_NC_NC_NC);   // not use comparator module
   setup_adc_ports( sAN0 | VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_64);
   TRISA=0b00001101;
   PORTA=0x0D;
   TRISC=0b00000000;
   PORTC=0x00;
   temp=0x00;
   temp1=0b11000000;
   
   
}   
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Aug 08, 2012 9:47 am     Reply with quote

Is your posted code complete and compilable?

How are you proposing measuring time?

Mike
Seaborgium



Joined: 23 Jul 2012
Posts: 14

View user's profile Send private message

PostPosted: Wed Aug 08, 2012 11:58 am     Reply with quote

Here is main() with fixed spacing.

Right now, uh... it just toggles bit 5 of switchstatus whenever you press and release the button. Doesn't seem to do anything else. Not sure what the point of this is.

Code:
void main() {
   CPU_SETUP();
   while(TRUE) {
      if(!bit_test(PORTA,3)) { //If third bit of PORTA is 0 (i.e. button is pressed?)
         while(!bit_test(PORTA,3)); //Wait until third bit of PORTA is on (i.e. button is released?)
         
         //Toggle 5th bit of switchstatus
         if(bit_test(switchstatus,5))
            bit_clear(switchstatus,5);
         else
            bit_set(switchstatus,5);
      }
   }
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Aug 08, 2012 3:16 pm     Reply with quote

Your code cries out for use of a TIMER
as the basis of a proper state machine - for managing the buttons.

As proposed - the time domain aspect of the challenge seems completely ignored.

Translation: the code you posted will never work with humans pressing even PERFECT bounce less buttons - even at INhuman speeds.

Very Happy Very Happy

i cant help but add that the preferred ORDER for inting a port is this:

TRIS is only required with FASTIO - which you have not declared
but for completeness, you really should:

output_B(your_value);
set_trisB(yourtrisvar);

so the port output state is set while it is still established in the HI-z spin up condition. Then you set the tris - the output condition is already set.

then again without fastio - all you need to do is

output_B(your_value);

you have cart before horse in your port initialization sequence.
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