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.

Programming concept for a port

Status
Not open for further replies.

RenesasT

Full Member level 2
Joined
Mar 11, 2016
Messages
149
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
1,330
Hello All,

After long time, have a great day all.

I need your help. All suggestion would be greatful for me.

I am using renesas controller, I am taking data in Port lets say port 1 which is of 8 bits,

I am taking status of 3 sensors , which could be either high or low. i.e 0 or 1.

The sensors A, B, C are connected to port 1.0, 1.1,1.2 respectively.

I am reading port in 8 bit variable as sensor.

I am writing below program

Code:
sensor=P1;

Its like P1.7P1.6P1.5P1.4P1.3P1.2P1.1P1.0

It is working. Now thing is that. Now I need like P1.7P1.6P1.5P1.4P1.3P1.1P1.0P1.2 in the sensor.

What to write for that?

suppose Port 1=0x01 (00000001) , then I need sensor value as 0x02(00000010).
If Port 1=0x06(00000110), then I need sensor value as 0x05 (00000101).

Please help me.
 

...I am reading port in 8 bit variable as sensor.



Code:
sensor=P1;

....
What to write for that?

suppose Port 1=0x01 (00000001) , then I need sensor value as 0x02(00000010).
If Port 1=0x06(00000110), then I need sensor value as 0x05 (00000101).

Please help me.

already sensor=P1;
you want it as 0x02.
It is an input port.

You have not described what you want ?
 

Hi,

I really don´t know what you need.

The sensors are on bit 0, 1 and 2.
So at first I´d mask the three bits by ANDing port value with 0b0000 0111.
Then the result can only be 0...7
binary: 000, 001, 010, 011, 100, 101, 110, 111
Now you could do a switch/case processing.

Or use a table with 8 entries.

Klaus
 

Code:
sensor=P1;

P1.7P1.6P1.5P1.4P1.3P1.2P1.1P1.0

P1.7P1.6P1.5P1.4P1.3P1.1P1.0P1.2

What to write for that?

From what I understand, you just want bit 0, 1 and 2 shuffled around?
Of course you could shift around the resulting bits, but if there is no simple arithmetic to convert your port value to your expected signal value, I would just use a table to translate the lower 3 bits:

Code:
uint8_t table[] = { 0, 2, 4, 6, 1, 3, 5, 7 };

port = P1;
sensor = (port & ~7) | table[port & 7];

You'll need bit ops in microcontroller coding regularly. Get accustomed with them.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top