Go Down

Topic: ACCESSING PORTS (Read 664 times) previous topic - next topic

abinjohnthomas

hi guys,
am a beginner in arduino, my doubts will definitely be stupid but kindly help me

can i send a hexa decimal value or decimal to a port and view it through led?
the sample codes shows i have to configure each pin n send either high or low bit, i want to access the whole port throgh some commands like portB=H5f of watever, is it possible in an arduino uno?

dxw00d

http://www.arduino.cc/en/Reference/PortManipulation

Tom Carpenter

#2
Oct 06, 2012, 11:23 am Last Edit: Oct 06, 2012, 11:27 am by Tom Carpenter Reason: 1
This wouldn't work:
Code: [Select]
portB=H5f
This would:
Code: [Select]
PORTB=0x5F

You can dec/increment the value in the port register too:
Code: [Select]
PORTB++;
PORTB += 10;
PORTB--;

And toggle all bits at once:
Code: [Select]
PORTB ^= 0xFF;
And pretty much anything you can do with a byte type variable (not quite but most things).
~Tom~

Nick Gammon



You can dec/increment the value in the port register too:
Code: [Select]
PORTB++;
PORTB += 10;
PORTB--;



Another entrant: http://www.ioccc.org/
http://www.gammon.com.au/electronics

Tom Carpenter

~Tom~

dhenry

Or to define a set of macros:

#define setbits(var, bits) var |= (bits)
#define clrbits(var, bits) var &=~(bits)
#define flpbits(var, bits) var ^= (bits)

with that, you can operate on the port (or ddr or any other register for that matter)

  setbits(PORTB, (1<<2)); //sets bit2 on portb
  clrbits(DDRB, (1<<0)); //bit0 on portb set as input
  flpbits(PORTB, (1<<2) | (1<<3)); //flip bit2 and bit3 on portb

Go Up