View previous topic :: View next topic |
Author |
Message |
switchblademaster42 Guest
|
reading port |
Posted: Sun Nov 15, 2009 7:25 pm |
|
|
ok so I have this problem:
I am using a PIC18f4520 and I have set portA as inputs. I would like to assign a different value for the condition of portA ie:
Code: |
0b00000=0,
0b00001=10,
0b00011=20,
0b00010=30,
0b00111=40,
0b00110=50,
0b00100=60,
0b01100=70,
0b11100=80,
0b01000=90,
0b11000=100,
0b10000=110
|
How can I do this?? is it possible to read all the inputs of the port at a time? Can I put this information in an array?
Please help. |
|
|
Guest
|
Re: reading port |
Posted: Mon Nov 16, 2009 1:44 am |
|
|
Quote: | is it possible to read all the inputs of the port at a time? | Have you even tried browsing the manual? Just studying the list of contents will give you the answer.
Quote: | Can I put this information in an array? | Yes you can, but if the number of different values you have to choose from is low (less than 20-30) it is more efficient to use the 'switch' statement. |
|
|
switchblademaster42 Guest
|
|
Posted: Mon Nov 16, 2009 9:57 am |
|
|
yea I have read the manual............I am able to read the data from the input port and recieve an 8-bit number that is equivalent to the binary. But can I get the data in binary form??
i was thinking to use the same switch statement with the 8-bit return n map it to the interger value i want.
Just wanted to know if i can get binary data from port without using I2C |
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 16, 2009 11:05 am |
|
|
The data _is_ in binary form. You are converting it to decimal to print it out!....
Everything inside the PIC, is binary.
The value stored in the register, _is_ the binary value....
If you want to access a single 'bit' of this, then use a bit_mask, or the bit_test function (more efficient), which will return an individual bit from the stored value.
You don't have to do any conversion to get it into binary...
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 16, 2009 11:05 am |
|
|
What do you exactly mean with "get the data in a binary form"?
Binary isn't but one of several representations of digital data. Below representations are equivalent.
0b01100 binary
0x0c hexadecimal
12 decimal
But the C standard hasn't binary constants or binary format specifiers to print numbers. If you intend something like binary printing, you have to do it on your own. If you're trying to achieve something different, please clarify.
I can't imagine, how the problem is related to I2C? |
|
|
switchblademaster42 Guest
|
|
Posted: Tue Nov 17, 2009 8:16 am |
|
|
yea I would like to get binary printing but i realsie I would have to code that separately.
The main idea is to have a certain action happen based on the inputs of the port.
eg. if the port input pins read:
Pina1 = 0
Pina2 = 1
Pina3 = 1
Pina4 = 1
Pina5 = 0
Pina6 = 1
Then a certain thing happens. But since I can get the 8-bit decial back I will use that instead to execute my function
for I2C i was thinking the same way a device like MCP23008
sends data in binary...where I can get the data from the sensors in the binary form |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Nov 17, 2009 8:32 am |
|
|
Code: |
int a = (input_a() & 0b11111);
int val = 0;
switch(a)
{
case 0b00000:
val = 0;
break;
case 0b00001:
val = 10;
break;
case 0b00011:
val = 20;
break;
case 0b00010:
val = 30;
break;
//. you fill in the rest.
//.
//.
//. A default case would proberbly be usefull!
}
Is exactly the same as:-
int a = (input_a() & 0x1F);
int val = 0;
switch(a)
{
case 0x00:
val = 0;
break;
case 0x01:
val = 10;
break;
case 0x03:
val = 20;
break;
case 0x02:
val = 30;
break;
//. you fill in the rest.
//.
//.
//. A default case would proberbly be usefull!
}
|
Simple solution.
This doesn't cover all posible values of port a though!
There are other more elegant but proberbly more memory or processor hungry methods. |
|
|
switchblademaster42 Guest
|
|
Posted: Tue Nov 17, 2009 9:08 am |
|
|
Hey thanks man.............
Just one clarification
Code: | int a = (input_a() & 0b11111); |
Let me know if this is correct:
int a - declare "a" as an int.
input_a() & 0b11111 - makes port A an input.
The input state of porta stored in "a". |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Nov 17, 2009 9:29 am |
|
|
Yes and no.
int a; declares variable name a as an int (8 bit unsigned default in CCS)
input_a() & 0b11111 - Takes the 8 bit input value from port a and & (AND's) it with 0b11111 This basiacally ignores the upper 3 bits of port a (a7, a6 and pin a5) as you do not appear to be using them in your comparrisons. If we did not map them out then they may affect the execution of your code.
one more thing I just thought of.
you may need to do the following
int a;
a = (input_a() & 0b11111);
because CCS will not allow you to have a declaration which is not a constant. |
|
|
switchblademaster42 Guest
|
|
Posted: Fri Nov 20, 2009 8:47 am |
|
|
hey thanks alot.............i am going to try this n c how it workz |
|
|
|