View previous topic :: View next topic |
Author |
Message |
Alphada
Joined: 19 Jun 2017 Posts: 27
|
convert input_b result to pin_bx handle |
Posted: Sat Jul 01, 2017 11:40 am |
|
|
Is there mathematical way to convert the result of the result of input_b (int8) to the value of the pin numerical handle?
I know it be done with a conditional statement but that would take lot more instructions to process than a simple mathematical operation if there is any.
Thx in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Jul 01, 2017 11:45 am |
|
|
The numerical handle is simply the memory address of the port, *8, plus the actual pin number.
So yes, this can be coded, but since there would be 256 possible values, you'd need 256 pins for every possible value..... |
|
|
Alphada
Joined: 19 Jun 2017 Posts: 27
|
|
Posted: Sat Jul 01, 2017 12:25 pm |
|
|
Ttelmah wrote: | The numerical handle is simply the memory address of the port, *8, plus the actual pin number.
So yes, this can be coded, but since there would be 256 possible values, you'd need 256 pins for every possible value..... |
thx thats what i wanted to know:
Quote: | The numerical handle is simply the memory address of the port, *8, plus the actual pin number. |
Btw what i want is to loop the whole port with "input_b" for changes from high to low and then send that pin handle to a function to process debounce.
Now i understand your answer, took me some time, but is right. How would that variable know exactly what port to process ? I need to think of something else, maybe using an array and processing the first to arrive, since is a theoretical keyboard and no more than one key should be pressed at once.
That clarified my doubt, thx. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Jul 01, 2017 1:42 pm |
|
|
Beware though that using variables is much bulkier/slower than using direct names.
So (for instance):
input(PIN_xx);
If using fast I/O codes as a single instruction. If using standard I/O 3 instructions normally.
However if you have a variable 'n' with a pin address:
input(n);
Will code as perhaps 20 instructions.....
Remember you can always talk directly to a port by just coding it's register address as a pointer. If you are scanning for a particular bit, then better to use a mask, and rotate this yourself. If you have such a mask already used to access a particular bit, make this global, or pass this to your function, and the function can use this to access the bit far faster. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jul 02, 2017 9:27 pm |
|
|
I ran the program below in MPLAB vs. 8.92 simulator. I put breakpoints
on the input() and while() statements. Then I ran the MPLAB Stopwatch
utility. It showed the following number of instruction cycles to execute
the input() function:
PIN_B0: 79 instruction cycles
PIN_B1: 87 instruction cycles
PIN_B2: 95 instruction cycles
.
.
.
PIN_B7: 135 instruction cycles
The duration depends upon the bit position in PortB. It goes up by 8
cycles for each further bit position. That's because the CCS writebit
and readbit routines each have a DECFSZ loop that takes 4 cycles each.
This was tested with vs. 5.071.
Test program:
Code: |
#include <16F887.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
//==========================
void main()
{
int8 pin;
int8 result;
pin = PIN_B0;
result = input(pin);
while(TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jul 03, 2017 2:11 am |
|
|
I meant to say about 20*, not 20 instructions.
Does make the point of just how much this does cost!...
Worse than I thought. :( |
|
|
|