mka879
Joined: 30 Oct 2015 Posts: 34
|
Comparison of CCS C FAQ Port B Button Press Code |
Posted: Sat May 07, 2016 12:11 am |
|
|
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: 19535
|
|
Posted: Sat May 07, 2016 3:43 am |
|
|
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.. |
|