Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Spanning multiple ports in C

Status
Not open for further replies.

Brian Hoskins

New Member
Hi there all,

I've presented myself with what I believe to be a small problem but at the moment I'm banging my head against the desk trying to come up with a good method of solving it.

Basically, I have 10 LED displays that I want to select from within my C program. Only 1 display will be selected at a time (I'm multiplexing them). I've done this before with 4 LED displays and to do it I created an array as follows;

const char select [4] = {1, 2, 4, 8} ;

And then when I wanted to select one of the four displays I just wrote something like;

PORTA = select[*insert display number here*]

And then PORTA would output the required bit pattern to enable the intended display. That all works fine. Here's my problem;

I now want to select from 10 displays instead, using the same idea. But that required more than 8 bits so it's larger than PORT A!

To remedy this I want to use all 8 bits of PORT A, and also two bits from PORT E to make a 10 bit port. I want to define this somehow, so that I can say something like

SELECTPORT = select[*insert display number here*]

where SELECTPORT refers to all of PORTA and also the required two bits of PORTE.

And this is the basis of my question - how do I go about defining this? I'm racking my brains and I can't think how but I'm certain there's going to be an easy way of doing it.

Suggestions?

Cheers!

Brian
 
Make two arrays which contain the LATA values need to turn on each led. The 0th element of each array is associated with led 0.

in c

Code:
rom const char lataVal[10]={0x??,0x??,..};
rom const char latcVal[10]={0x??,0x??,..};

void turnOnLed(char led)
{
    LATA=LATC=0x00; // all off to prevent ghosting
    LATA=lataVal[led];
    LATC=latcVal[led];
}

If you need to use the other 6 bits of porte for output you need to use masking when writting that port.
 
Last edited:
Hmmmm so there isn't an easy way of "defining" your own I/O port which is made up of bits from more than one I/O port then?

I read through your solution but I'm not certain I understand it. Well, if I can't solve it by combining two ports perhaps I can come up with another method. Back to the drawing board!

Thanks for the response,

Bri
 
This is the easy way but I made it look hard.

I redid the example give it another look.

Code:
char selectPortA[10]={0x??,0x??,..};  // 10 PORTA values
char selectPortE[10]={0x??,0x??,..};   // 10 PORTE values

void turnOnLed(char n)
{
    PORTA=selectPortA[n];
    PORTE=selectPortE[n];
}
// instead of
//   PORTA = select[*insert display number here*]
// use
turnOnLed(*insert display number here*);

You could use a macro but it is clearer with the function.

If you still do not understand ask about it.

3v0
 
Last edited:
Yeah I see what you're getting at actually.

So the 'walking 1' values would be inserted for the first 8 values of the selectPortA array and the remaining two values of the selectPortA array would = 0.
For the remaining values of the array (9 and 10) the walking 1 would hop over to PortE where the first 8 values of the selectPortE array would = 0 and the walking 1 values would be inserted for the remaining 2 selectPortE array values (9 & 10)

So I guess it would look like this;

const char selectPortA [10] = {1, 2, 4, 8, 16, 32, 64, 128, 0, 0} ;
const char selectPortE [10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2};

Then to select a display I can use a value n as you suggested. A bit wasteful, but certainly functional!

Have I understood correctly? I'm going to try that - but tomorrow now because it's getting late here :)

Bri
 
The array method is robust in that it works for more complicated multiplexing where more then one bit is on at at time.

For a simple walking 1's try this.

Code:
PORTA=PORTE=0x00;  // turn off last selection

if (n<8)
  PORTA = 1<<n; // 1 shifted n times is correct bit
else
  PORTE = 1<<(n-8);


3v0
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top