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 CCS Technical Support

Reading port A

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



Joined: 10 Nov 2005
Posts: 11

View user's profile Send private message

Reading port A
PostPosted: Wed Aug 29, 2007 11:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:56 am     Reply with quote

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







PostPosted: Wed Aug 29, 2007 2:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 10:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 10:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 31, 2007 5:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 31, 2007 6:12 am     Reply with quote

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







PostPosted: Fri Aug 31, 2007 7:19 am     Reply with quote

[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
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