Pages: [1]   Go Down
Author Topic: need 4 high bits in 4 ports pins  (Read 268 times)
0 Members and 1 Guest are viewing this topic.
West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i need the 4 high bits of a binary number to set 4 pins on portc , not the four high bits of the port , meaning the four pins i need set are split between the 4 high and four low pins
example
binary 8 bit 
extract 4 high bits :    testNUmber<<4
PORTC                       7 6 5 4 3 2 1 0
testNUmber                x x x 1 1 1 1 x
                                         
these 4 bits need to set or clear these 4 pins
bitwise operations always make me run in circles

would i do something like
PORTC =0b00011110 | (testNUmber<<4);

??

I cant really test it out right now if not i would run some tests  but im not home.
Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Montreal
Offline Offline
Full Member
***
Karma: 4
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
PORTC&=0b11100001; // clear bits 1-4 in the port
PORTC|=(v>>3)&0b00011110; // set bits 1-4 in the port to 4 high bits in v (v=uint8_t)
Also you could do:
Code:
PORTC=(PORTC&0b11100001)|((v>>3)&0b00011110);
« Last Edit: August 13, 2013, 10:23:50 pm by JarkkoL » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33271
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have to use direct port manipulation?
If not look at doing this
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
Logged

Pages: [1]   Go Up
Jump to: