PDA

View Full Version : Two Bit request (sic)



Sean_Goddard
- 26th June 2007, 22:33
I've read through the help files and the manual anad I'm SURE it can be done but I just cant seem to find out how. I'm confusing PICBASIC with VHDL in that what I want to do is this;

I have 2 INPUT pins on a PIC which are pulled high, these then go to a switch which can pull EITHER bit of low, so the truth table is;

BIT A BIT B Port "MODE"
0 1 1
1 0 2
1 1 3

What I want to be able to do is define MODE as two bits of a port so that I can use it as a variable to defire a series of CASE statement to select an option and thence jump to a subroutine.

It's a cheap way of getting 3 funtions out of two pins. Anyone any ideas how I can do this. I guess the VHL instatiation would be sort of MODE=PORTA[0 TO 1] or something like that, but HOW to do it in PB??

SteveB
- 26th June 2007, 22:57
Lots of ways to do this, here’s one:

If the pins are two adjacent pins of a port (for example portA.2 and porta.3) then you could use a bit mask and then shift the bit to the right as needed. So, using A.2 and A.3 it would be:

MODEval= PortA & %00001100
MODEval = MODEval >> 2

This will give you a value of MODE from 0-3.

Also, you would even skip the shift and just use the CASE to check for values of 4, 8, and 12.

And, if the pins are not consecutive, you can still use a bit mask and then check the values:

MODEval = PortA & %10001000
SELECT CASE MODEval
CASE 8
Statement...
CASE 128
Statement...
CASE 136
Statement...
{CASE ELSE
Statement...}
END SELECT

HTH,
SteveB

Sean_Goddard
- 27th June 2007, 02:24
Thanks for the rapid reply. I've used this method before many times,and probably will in this case too, so couldn't believe I missed it =-) nvm.

No all I gotta do if figure out how to attach 4 swiches per port. It's a LONG time since I did any programming and I forget a lot of the techniques involved (happens when you get old).

Thanks Steve that was a great help.