Pages: [1]   Go Down
Author Topic: Direct Port Manipulation Through a variable - help  (Read 550 times)
0 Members and 1 Guest are viewing this topic.
Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
    I am trying to access a port through a variable as in -

Code:
thePort = PORTB;

thePort ^= 255;
 

    Whatever I do I cannot get this to work inside a library - I am sure its something daft but I need another pair of eyes to see it

Thanks

Duane B
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Try this...

Code:
static uint8_t volatile * thePort;

thePort = & PORTB;

*thePort ^= 255;


Edit: Forgot the ampersand.
« Last Edit: October 28, 2012, 02:59:43 am by Coding Badly » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12425
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


your code makes a copy of the value in the port, you should write it back.

thePort = PORTB;
thePort ^= 255;
PORTB = thePort;

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   Thanks all, mystery solved, it was the forgotten ampersand.

Thanks

Duane B.


Logged


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

Easy:

Code:
#define thePort PORTB

thePort ^= 255;
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy, also pointless.

Duane B
Logged


Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
volatile uint8_t& thePort = PORTB;
thePort ^= 0x08
Logged

Pages: [1]   Go Up
Jump to: