How to change PORTX register in other place of program?(Code

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everybody
i have a question about GCC and CodeVision: How can i changing PORTX register in other place of program. as you know, normally we setting it at Main function inside DDRX rigester for example like this:

DDRB = 0x00;
PORTB = 0x00;

but how do you change it at other place? and an another question:

is it wisely that we setting DDRX and PORTX rigester like this(also for example)?:

DDRB.0=0;
PORTB.0=1;

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

but how do you change it at other place?

What do you mean "other place"? If I do:

void foo(void) {
  PORTB = 0x55;
}

int main(void) {
  DDRB = 0xFF;
  while (1) {
    PORTB = 0xAA;
    foo();
  }
}

then the PORTB output toggles between 0x55 and 0xAA. Does foo() count as "another place"?

Quote:

is it wisely that we setting DDRX and PORTX rigester like this(also for example)?:

That code is using a CodeVision only special feature for AVRs. In theory '.' in that context in C must be followed by a letter so it is a non-standard extension as it's actually followed by 0..7. This doesn't matter if you only ever plan to use CV and to share code with other CV users. If you want to write code that works in both CV and GCC then either use:

DDRB &= ~(1<<0);
PORTB |= (1<<1);

which will work in any C compiler on earth (as long as "DDRB"/"PORTB" is known) or you can create a construct something like the CV feature with:

struct bits {
	uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
} __attribute__((__packed__));

#define MYPORTB (*((struct bits *)&PORTB))

int main(void) {
	MYPORTB.b3 = 1;

true there has to be a 'b' there to make this into standard C but something like this should work in any C compiler.

Personally I like Peter Danegger's sbit.h solution for use with GCC - the above is based on that (I stole his bits{} structure).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Cliff

Quote:
What do you mean "other place"?

when you set PORTX at main function like this:

DDRB = 0xff;
PORTB = 0xff;

then the PORTB register put high logic at B pin/port.
when you set PORTX at main function like this:

DDRB = 0x00;
PORTB = 0xff;

then the PORTB register put a pull-up resistor. thus, at main function, the PORTB register doesn't output or input toggles. correct?
but when we use the PORTB to other place like this(as you provided):

void foo(void) {
  PORTB = 0x55;
}

int main(void) {
  DDRB = 0xFF;
  while (1) {
    PORTB = 0xAA;
    foo();
  }
} 

this register just does output or input and doesn't a high logic or pull-up resistor. my mean is this.

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The AVR does not know where in your source code you do register manipulation. Thus, when you code e.g.

DDRB = 0xff;
PORTB = 0xff;

or

DDRB = 0x00;
PORTB = 0xff;

it does not matter if this is done in the main() function, or in some other function. The effects should be the same regardless of where in your code this is placed. Indeed, when the code executes from flash there are no significant traces left that indicates in which function the source code the instructions was generated from.

Quote:
my mean is this.

Since you have asked earlier to correct you when you are less fortunate with the English language I'd say that
"This is what I mean" would be a better formulation. Alternatives are "That is what I mean" (present tense), "That was what I meant" (past tense) etc..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Great Johan! thanks(especially for english language points)
but still i'm staggerer.

Quote:
The effects should be the same regardless of where in your code this is placed.

but i think when we use of these two register(DDRX or PORTX) in other place(except main function) they just set the port to output or input. i think when use of port like this(for example):

PORTB = 0xff;

it set all B pin to output and when use of port like this(for example):

DDRB = 0xff;

it set all B pin to input.
really i confused. :?

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

ut i think when we use of these two register(DDRX or PORTX) in other place(except main function) they just set the port to output or input. i think when use of port like this(for example):

PORTB = 0xff;

it set all B pin to output


No. It sets all bits of PORTB to 1. What effect that has depends on the value of DDRB. If DDRB has bits cleared (i.e. the port is set to input) then this means that pull-ups are enabled. If DDRB has its bits set (i.e. the port is set to output) then this means that the port pulls the pin high.

There is nothing special with where this is done, or in what order. Consider this example, and assume that DDRB and PORTB has not been touched since reset:

// Since both DDRB and PORTB is at their default value (i.e. 0) the port is set to input, and pull-ups are not enabled.

PORTB = 0xFF;  // The port is still set to input, but now the pull-ups are enabled
DDRB = 0xFF;   // The port is now set to output, and the pins are pulled high
PORTB = 0x00;  // The port is still set to output, but now the pins are pulled low
DDRB = 0x00;   // The port is set to input. Since PORTB is still 0x00 pull-ups are not enabled

Although I've manipulated all bits of the port, this is not necessary. You can of-course do similar operations on individual bits of the port.

All this is excellently documented in the data sheet, which you should read thoroughly until you understand it. For parts that you do not understand: Quite them here, or point to them, and we shall try to explain. For the meaning of DDRB and PORTB bits you should dlook in the section called "I/O Ports", and a table probably named "Port Pin Configurations". In the ATmega328P data sheet it is on page 78, and the table is numbered 14-1.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well done, Johan! Thanks.

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you combine the bits of DDR and PORT then for each bit position (pin) there can only be four states:

DDR PORT
 0    0  -- input floating
 0    1  -- input with pull-up enabled
 1    0  -- output driven low
 1    1  -- output driven high

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Well done, Johan! Thanks.

Thank you. Still, all I did was to flesh out the terse but precise table of the data sheet. You know what they say.. "When everything else fails, try reading the documentation." :wink:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Cliff,
i already was confused, but now i understood it.

JohanEkdahl wrote:
Thank you. Still, all I did was to flesh out the terse but precise table of the data sheet. You know what they say.. "When everything else fails, try reading the documentation." :wink:

:)

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Thanks Cliff,
i already was confused, but now i understood it.

Not to berate Cliffs effort, but you do realize he was more or less quoting directly from the data sheet?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now, yes! :mrgreen:

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!