A question on port methods

I'm trying to use the portMode(), portRead(), and portWrite() methods but the Arduino IDE doesn't seem to support it. The Wiring IDE does support them. Is there any way I can use them with the Arduino IDE?

I know nothing about wiring but those function don’t exists on Arduino.

You do have pinMode(), digitalRead(), and digitalWrite() but they only work on a single pin.

What exactly do you need to do?


Rob

You use the following for direct port manipulation

DDRx to set the pins in port x to input or output mode, all pins default to input on reset or power-up.
PORTx set/reset pins on port x.
PINx read the pins on port x.

Where x is the port letters supported by the chip you are writing for. Note that the port letters and how many ports there are, are different between the avr mega8/mega168/mega328 family of chips and the mega1280/2560 family of chips, so be sure to reference the AVR datasheet for the specific AVR chip you are writing for.
http://www.arduino.cc/playground/Learning/PortManipulation

Lefty

retrolefty I tried using those methods but all I got was many errors during compiling. I’ve been programming in assembly for over 30 years. So I’m relatively new to C language. That’s probably why I’m having so much trouble. What I’m trying to do is initialize a graphic LCD with a SED1330 controller in 8-bit mode. So I need to change all 8 bits simultaneously which is what the port methods do.

KMOver50:
retrolefty I tried using those methods but all I got was many errors during compiling. I've been programming in assembly for over 30 years. So I'm relatively new to C language. That's probably why I'm having so much trouble. What I'm trying to do is initialize a graphic LCD with a SED1330 controller in 8-bit mode. So I need to change all 8 bits simultaneously which is what the port methods do.

Well post an example of some code you wrote that doesn't work in the Arduino IDE, tells us what results you get or don't get, and let us see how you are applying the commands. Also tell us what arduino board you are using. I've used such commands in the past with no problem.

Lefty

I did a simple test using this code:

int outport = 1;
int val = 0;

void setup() {
portMode(outport, OUTPUT);
}

void loop() {
portWrite(outport, val);
val = val + 1;
if (val > 255)
{
val = 0;
}
}

When trying to complile it, these are the error messages I got:

sketch_oct20a.ino: In function 'void setup()':
sketch_oct20a:5: error: 'portMode' was not declared in this scope
sketch_oct20a.ino: In function 'void loop()':
sketch_oct20a:9: error: 'portWrite' was not declared in this scope

KMOver50:
I did a simple test using this code:

int outport = 1;
int val = 0;

void setup() {
portMode(outport, OUTPUT);
}

void loop() {
portWrite(outport, val);
val = val + 1;
if (val > 255)
{
val = 0;
}
}

When trying to complile it, these are the error messages I got:

sketch_oct20a.ino: In function 'void setup()':
sketch_oct20a:5: error: 'portMode' was not declared in this scope
sketch_oct20a.ino: In function 'void loop()':
sketch_oct20a:9: error: 'portWrite' was not declared in this scope

We already stated that portWrite and portMode are not arduino defined functions, They don't exist in the Arduino supplied libraries, those errors are real, you have to convert to using the DDRx, PINx, and PORTx methods. See the examples in the link I posted.

Lefty

So I need to change all 8 bits simultaneously

That's physically not possible on the smaller Arduinos.

But also it's shouldn't be necessary I would think, the data doesn't usually have to be presented simultaneously because it's clocked in after it's stable.

So I think that needs to be addressed before changing your code.

Do you have a schematic?
What Arduino are you using?


Rob

I'll check the link you posted now. By the way I'm using the Arduino Uno board. I will also be losing the serial functionality (digital pin o and 1) by using the 8-bit port.

I will also be losing the serial functionality

Yes you got me there, if you are willing to lose them you can get 8 bits. I still don't think it's required though.


Rob

KMOver50:
What I'm trying to do is initialize a graphic LCD with a SED1330 controller in 8-bit mode. So I need to change all 8 bits simultaneously which is what the port methods do.

You don't need to do that in any LCD I have seen. There is a clock line you use. So you set up the 8 bits at your leisure and then clock the data in or out.

Judging by the datasheet the data is clocked in when the E (enable) line goes low.

It doesn't make sense any other way anyway. Say you have a series of 0x00 bytes. How is the controller to know if you have sent one or ten of them? There has to be a clock pulse.

That's true but if you change the port bits one at a time then it takes much more code and flash memory.

I used the instructions in the link and it worked. I tested it with this sketch and got no errors on compiling:

int val = 128;

void setup() {
DDRD = 255; //make portd an output
}

void loop() {
PORTD = val;
}

Well hardly much more code. All (most of?) the LCD examples use the individual bits method.

Be warned that the D port on the Uno shares D0 and D1 with the USB chip and if you are not careful the pull-ups may push them high. This probably won't be an issue if they are configured as outputs, but with 32 Kb of program memory, breaking the bits into individual pins won't be a huge overhead.

However if you can make it work, fine. That will simplify things.

Thanks for the help Nick. I already started writing the sketch for initializing the graphic LCD. Although you are right about the flash having 32k. But I noticed that C programming can use up a lot of flash memory very quickly. Not like assembly which takes very little memory. To prevent interfeering with the serial communications on digital pins 0 and 1, I can always make sure that digital pins 0 and 1 remain low using PORTD = 252 after every write to the LCD. I hope this works.

Actually the libraries, when used, tend to have a fixed overhead. For example, digitalWrite allows for the pin number to be supplied at runtime, rather than compile time, and there is an overhead in doing that. Ditto for digitalRead, and various other things.

However the compiler generates pretty compact code after that. For example I made an adventure game, using an LCD, and button-detection, here:

The whole thing fitted on a Uno. I was more worried about the lengthy room descriptions filling up the program memory, than the overhead of the libraries.

Not like assembly which takes very little memory.

To amplify, if the assembly code did the same things as the libraries, you wouldn't save much (if any) by writing in assembly. I've done a lot of assembly programming in my day, I know the pros and cons of it. :slight_smile:

That's an impressive project Nick. You are also right about the memory overhead from use of the libraries. I compiled the sketch several times to make sure I don't generate too many errors. Even after adding extra lines of code, the memory usage stayed the same at 550 bytes of flash and 9 bytes of SRAM. I'm converting 8051 assembly language into Arduino C language. I'm sure I will be adding some delays because my 8051 is much slower than the ATmega328 in my Arduino Uno. I'm not expecting too much extra code for the delays. When I complete it I hope it works without too much debugging.