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

Comparison of CCS C FAQ Port B Button Press Code

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



Joined: 30 Oct 2015
Posts: 34

View user's profile Send private message

Comparison of CCS C FAQ Port B Button Press Code
PostPosted: Sat May 07, 2016 12:11 am     Reply with quote

I came across CCS C FAQ section and read Port B Button Press Code. I have recently implemented the same thing but differently. I would like to compare the two code and determine which is better and why.

Here is CCS C FAQ code
Code:

#int_rb
rb_isr ( ) {
   byte changes;
   changes = last_b ^ port_b;
   last_b = port_b;
   if (bit_test(changes,4 )&& !bit_test(last_b,4)) {
      //b4 went low
   }
   if (bit_test(changes,5)&& !bit_test (last_b,5)) {
      //b5 went low
   }
   .
   .
   .
   delay-ms (100); //debounce
}




Here is mine

Code:


#int_rb
void rb_isr(void)
{       
   int1 state_b7,state_b4;
 
   setup_timer_1(T1_INTERNAL); 
   state_b7 = input(pin_b7);
   state_b4 = input(pin_b4);
}

if (overflow_timer1 > 0)
      {
         output_bit(pin_b5,!input_state(pin_b5));
         setup_timer_1(T1_DISABLED|T1_DIV_BY_1);
         overflow_timer1 = 0;
         check_portb();
         if (flag_b7 == 1)
         {
            //button has gone from high to low
            //do something                             
         }
         if (flag_b4 == 1)
         {
             //button has gone from high to low
             //do something   
         }
      }
void check_portb()
{
    if (input_state(pin_b7) != w)
   {
      if (input_state(pin_b7))
      {
         flag_b7 = 1;
         w = 1;
      }
      else
         w = 0;
   }
   if (input_state(pin_b4) != y)
   {
      if (!input_state(pin_b4))
      {
         flag_b4 = 1;
         y = 0;
      }
      else
         y = 1;
   }     
}

flags, w and y are global 1 bit variables.

Apart from the debouncing part, which the FAQ section itself does not recommend and proposes the timer approach, please comment.
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Sat May 07, 2016 3:43 am     Reply with quote

Honestly, both are awful.

Forget using INT_RB, unless you have added external hardware to do the debounce. With this you then don't need delays.

Use a tick timer instead. Just once every hundredth of a second check the port. If the inputs are the same for two successive checks record this as a 'value'. This is fundamentally how keys are handled on things like PC keyboards. Typically a 6502 chip or a custom modern derivative of this. Nice thing is that then the same tick can handle things like auto-repeat etc..
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