Grouping port bits in C?

Thread Starter

BrianH

Joined Mar 21, 2007
43
Hi all,

On 8-bit Microchip PICs the I/O ports are typically 8-bits wide which is great for controlling LCDs via a parallel interface for example because you can do things like this:

porta = data;

But, what if you're working with a wider port instead? For example, I am now working with a device that has I/O ports that are 16-bits wide. If I want to section off some bits of a port and allocate them for a specific task, can I group them as a name somehow so that I can only write to the bits of the port I am interested in?

I hope this question makes sense!

Cheers, and thanks in advance for any advice,

Brian
 

be80be

Joined Jul 5, 2008
2,072
Some compliers let you do high byte low byte and nibbles. Would be nice if you had posted what chip your using.
You could mask off the bits you don't want changed .
 

cackharot

Joined Feb 2, 2011
13
Hi BrianH,

You can define something like this

#define PORTXH(data) PORTX &= data << 8
#define PORTXL(data) PORTX &= data >> 8

// set upper 8 bits to 1
PORTXH(0xff);

//set lower 8 bits to 0
PORTXL(0x00);
 

Thread Starter

BrianH

Joined Mar 21, 2007
43
Some compliers let you do high byte low byte and nibbles. Would be nice if you had posted what chip your using.
You could mask off the bits you don't want changed .
Hi there be80be, thanks for the response.

I didn't post which chip I was using because I thought my question was more general. It could easily apply to any device where you only want to use certain bits of a port for a particular task.

However, since you've raised the question, I am experimenting with a PIC24FJ128 series device, which is the first time I've played with a 16-bit micro. The I/O ports are a bit strange - you've got a few 16-bit wide ports, which is fine, but then there are some 9,10 and 11 bit ports as well!

Regarding your idea of masking - yeah that's what I was thinking as well, but I wasn't completely sure that it was the best way of doing it and that I wasn't missing a neat trick for this kind of thing.

Thanks for the advice.
 

Thread Starter

BrianH

Joined Mar 21, 2007
43
Hi BrianH,

You can define something like this

#define PORTXH(data) PORTX &= data << 8
#define PORTXL(data) PORTX &= data >> 8

// set upper 8 bits to 1
PORTXH(0xff);

//set lower 8 bits to 0
PORTXL(0x00);
Aha - very useful indeed. I fully understand how I can use the technique you've just described, but I'm not so sure I fully understand how it works.

Firstly, I understand that you've used a bit mask to only set or clear the bits you want, and leave all other bits unchanged.

However, I'm not entirely sure how this is accomplished using the shift-left, shift-right operators.

Also, the (data) part: I assume this is just something the compiler takes to mean that you're going to substitute some data here. So it is compiler specific?
I'll check that works with my compiler (I'm using Microchip's C compiler).

Thanks very much indeed - your solution appears to fit my needs perfectly. All I need to do now is fully understand how it works :)

Brian
 

THE_RB

Joined Feb 11, 2008
5,438
You could also make a function that sends data to the pins you need.

So you just load the data (say 1 byte) into a variable and call the function;
myport1 = blah;
output_myport1();

That way you are not limited to nibbles or halves of a 16bit port.

"myport1" can be any pins you want, on any ports.
 

Thread Starter

BrianH

Joined Mar 21, 2007
43
You could also make a function that sends data to the pins you need.

So you just load the data (say 1 byte) into a variable and call the function;
myport1 = blah;
output_myport1();

That way you are not limited to nibbles or halves of a 16bit port.

"myport1" can be any pins you want, on any ports.
Okay, so if I've understood your suggestion correctly, you're saying that you create a variable myport1, which holds the data you want to output. Then, you're calling a function that outputs this data on any I/O pins you want, which allows you to output the data from the variable onto any group of pins that you choose.

I presume that the function would do this on a bit by bit basis? So it'd output bit 0 of the variable to the pin of choice, then bit 1, and so on.

Have I understood your suggestion correctly?
 

THE_RB

Joined Feb 11, 2008
5,438
BrianH- correct. You can code the function for the best way to handle those pins, whether some pins are in parallel groups or maybe every pin must be handled individually. The point is that it can be optimised for your exact hardware.

Then you have the convenience of one function to output to the "port".

Also I suggested using a global variable and calling a void function() as it is quicker and uses less ROM per function call than using function(variable).
 
Top