View previous topic :: View next topic |
Author |
Message |
misaeldv
Joined: 10 Nov 2005 Posts: 11
|
Reading port A |
Posted: Wed Aug 29, 2007 11:42 am |
|
|
Hi, I have a question about reading port A, it is only 5 pins, which will be 5 bits (right). My question is if I read this into an int8, what would I get.
Will it be as follows?
Pin A1 is high,
Pin A2 is low
Pin A3 is high
Pin A4 is high
Pin A5 is low
int8 x;
x = input_a();
will
x = 00001101
First three zeros since there is only 5 bits, then it would go like A5, A4, A3, A2, A1?
or what will it be equal to?
Thank in advance |
|
|
misaeldv
Joined: 10 Nov 2005 Posts: 11
|
|
Posted: Wed Aug 29, 2007 11:50 am |
|
|
Sorry about that, it should be 6 bits for port A, A0 - A5;
int8 x;
x = input_a();
would x be as follows?
x = 00A5A4A3A2A1A0
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 11:56 am |
|
|
The safe way is to mask off the upper two bits. Then you know they
are set to 0 in 'x'.
Code: | x = input_a() & 0x3F; |
|
|
|
Ttelmah Guest
|
|
Posted: Wed Aug 29, 2007 2:54 pm |
|
|
PCM_programmer has given you the safe answer. The other part of the answer (if you don't want to mask), is 'read the data sheet'. For example, on some of the chips, the high order bits of ports that don't have corresponding pins, are used internally. So (for instance), on the 12CE671/672/673/674, the hgh two bits, connect to the internal EEPROM (when available), and wake up reading '1', but may change if the eeprom is used. While on the 12F675, the memory data says that these bits are unimplemented and _will_ read as '0'.
Best Wishes |
|
|
misaeldv
Joined: 10 Nov 2005 Posts: 11
|
|
Posted: Wed Aug 29, 2007 10:33 pm |
|
|
So, will this theoretically work in a switch statement? I tried it, but it seems to be doing random things.
Basically I'm not using A4 or A5, and A0 is being used as a switch, only A1 - A3 are being used to decide which action to perform, so what I am doing is the following, but it is not working; should this be working? Maybe there is something else going on somewhere in the program.
Code: | controller = input_a() & 0x3F; //bit masking to make sure first two bits are set to 0
switch (controller)
{
case 0x01: steps = normal;
break;
case 0x03:
{
if(counter%3 == 0)
{
steps = double;
} //end if
else if (counter%5 ==0){
steps = normal;
}
}
break;
case 0x05:
{
steps = double;
}
break;
case 0x07:
{
steps = steps/2;
}
break;
default:
steps = steps;
break;
} //end switch |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 10:40 pm |
|
|
Quote: |
Basically I'm not using A4 or A5, and A0 is being used as a switch,
only A1 - A3 are being used to decide which action to perform.
|
Mask down the input byte so it only contains the bits that you're
interested in. Then, since it's apparent from your switch-case values
that you want a right-justified number, shift right it right by 1.
Example:
Code: | controller = (input_a() & 0x0E) >> 1; |
|
|
|
misaeldv
Joined: 10 Nov 2005 Posts: 11
|
|
Posted: Fri Aug 31, 2007 5:49 am |
|
|
Thanks, that makes a lot more sense, just one more question wouldn't shift right be the opposite?
Code: | controller = (input_a() & 0x0E) << 1; |
Thanks |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Fri Aug 31, 2007 6:12 am |
|
|
I wouldn't shift it - just switch on the values you can get from the input.
Also, if you're looking at 3 bits, there are 8 possible combinations to check for. You may need to do something more complex, like remember the input from last time, and see which bits change.
Ken |
|
|
Ttelmah Guest
|
|
Posted: Fri Aug 31, 2007 7:19 am |
|
|
[quote="misaeldv"]Thanks, that makes a lot more sense, just one more question wouldn't shift right be the opposite?
[code]controller = (input_a() & 0x0E) <<1>> shifts a number right (towards the LSb). Effectively dividing it by two (if it is unsigned). << shifts a number left (towards the MSb), effectively doubling it (with the same coda about unsigned). For 'signed' values, the results are 'implementation specific' (varies according to the processor and compiler).
The arrows 'point' in the direction that the operation shifts.
Best Wishes |
|
|
|