Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: ACCESSING PORTS  (Read 653 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1733
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This wouldn't work:
Code:
portB=H5f
This would:
Code:
PORTB=0x5F


You can dec/increment the value in the port register too:
Code:
PORTB++;
PORTB += 10;
PORTB--;
And toggle all bits at once:
Code:
PORTB ^= 0xFF;
And pretty much anything you can do with a byte type variable (not quite but most things).
« Last Edit: October 06, 2012, 04:27:40 am by Tom Carpenter » Logged

~Tom~

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Another entrant: http://www.ioccc.org/
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1733
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Another entrant: http://www.ioccc.org/
smiley-cool
Logged

~Tom~

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: