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

reading port

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







reading port
PostPosted: Sun Nov 15, 2009 7:25 pm     Reply with quote

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
PostPosted: Mon Nov 16, 2009 1:44 am     Reply with quote

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







PostPosted: Mon Nov 16, 2009 9:57 am     Reply with quote

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







PostPosted: Mon Nov 16, 2009 11:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 16, 2009 11:05 am     Reply with quote

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







PostPosted: Tue Nov 17, 2009 8:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 17, 2009 8:32 am     Reply with quote

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







PostPosted: Tue Nov 17, 2009 9:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 17, 2009 9:29 am     Reply with quote

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







PostPosted: Fri Nov 20, 2009 8:47 am     Reply with quote

hey thanks alot.............i am going to try this n c how it workz
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