Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to assign bit values at once to different port pins of different I/O ports?

Status
Not open for further replies.

kemalkemal

Member level 1
Joined
Jan 27, 2013
Messages
38
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,672
how to assign bit values at once to different port pins of different I/O port through a register which I define as comprised of those port pins of I/O ports?

I want to define a variable(name=my_reg) of uint8_t type which i plan to use it as a register of 8 bits. Each bit of this register should send its value to different bits of different I/O ports.

For example When my_reg = 3;

LATAbits.LATA0 which is my_reg.0 should be 1
LATAbits.LATA1 which is my_reg.1 should be 1
LATCbits.LATC0 which is my_reg.2 should be 0
LATCbits.LATC4 which is my_reg.3 should be 0
LATBbits.LATB5 which is my_reg.4 should be 0
LATBbits.LATB0 which is my_reg.5 should be 0
LATAbits.LATA7 which is my_reg.6 should be 0
LATAbits.LATA6 which is my_reg.7 should be 0

I am new to C. I defined a struct which is comprised of 8 subplaces all is 1 bit. But i couldn't give a value to MY_REG.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
typedef struct {
uint8_t L1;
uint8_t L2;
uint8_t L3;
uint8_t L4;
uint8_t L5;
uint8_t L6;
uint8_t L7;
uint8_t L8;
} MY_REG;

 

Try this but not tested.

Code:
#define myPortA LATA

typedef struct {
uint8_t B0:1;
uint8_t B1:1;
uint8_t B2:1;
uint8_t B3:1;
uint8_t B4:1;
uint8_t B5:1;
uint8_t B6:1;
uint8_t B7:1;
} BitField;

BitField myPortB;

Code:
myPortA.B0 = 1;
myPortA.B0 = 0;
 

That example only works if the bits are in the same port. The 'MY_REG' in post #1 used bits across three different ports.

Brian.
 

Try this but not tested.

Code:
#define myPortA LATA

typedef struct {
uint8_t B0:1;
uint8_t B1:1;
uint8_t B2:1;
uint8_t B3:1;
uint8_t B4:1;
uint8_t B5:1;
uint8_t B6:1;
uint8_t B7:1;
} BitField;

BitField myPortB;

Code:
myPortA.B0 = 1;
myPortA.B0 = 0;

thanks for reply. i tried something similar but it didn't work. I dream something like that; when i give a value to myPortB, myBportB would distribute/place all its bit values to appropriate port pins.

This results an error of main.c:49:13: error: incompatible types when assigning to type 'myPortB' from type 'int'
Code:
myPortB  = 3;
 

The processor can only write to one port at a time. Why do you want to hide this fact from the programmer?
There is no generic way to do this, but for a few ports with the registers at contiguouos addresses, it could be possible to place a larger data type at the lowest address.
I will not give an example, because I think it is a terrible idea.
 

The processor can only write to one port at a time.
I don't aim a parallel work. I have microchips explorer16/32 development board. And 8 LED's on that board are driven from different ports. (4 of them are from PORTA, 3 from PORTB and one is from PORTC) As i sad in my first post i try to send an ON-OFF command to all those LEDs at once. But after replies to my question i understand that it is a rather difficult task may be impossible. Thanks.
 

I suggest that you do it with a function. Call it set_leds(uint8_t led_bits) or similar. The function can easily extract the bits for each output port.
 

I haven't tried different ways to see which is quickest/smallest but my first approach would be a function like this:
Code:
if(led_bits & 0x01) LATAbits.LATA0 = 1; else LATAbits.LATA0 = 0;
if(led_bits & 0x02) LATAbits.LATA1 = 1; else LATAbits.LATA1 = 0;
if(led_bits & 0x04) LATCbits.LATC0 = 1; else LATCbits.LATC0 = 0;
.
.
if(led_bits & 0x80) LATAbits.LATA6 = 1; else LATAbits.LATA6 = 0;

It might be worth trying 'LATAbits.LATA0 = (led_bits & 0x01)?1:0;' on the basis that any '1' should be 'true' regardless of actual bit value.

It's worth noting the doing the checks bit-by-bit in a function can only change the state of one bit at a time. If done using bit shifts and then ANDing & ORing the bits it is possible to set/reset more than one bit simultaneously but only within one port at a time.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top