Read and Write to registers

Hi,

I'm fairly new in programming my ArduinoUNO and I would like to have some help with example code how to read and write directly to the various in and output data registers without using digitalWrite(), digitalRead() and analogRead().

All The Best /GG

Read a port:
byte value = PINx; // where x = A,B,C,D etc.
Write a port:
PORTy = 0x00 to OxFF; // where y = A,B,C,D etc.

Act on a single bit:
if ((PIND & 0b00000100) == 0){ // bit 2 of PORT D for example
// bit is 0, do something
}
else {
// bit is 1, do something else
}
Writing a single bit:
PORTD = PORTD & 0b11111011; // make bit 2 of PORT D a 0 (clear the bit), leaving rest alone
PORTD = PORTD | 0b00000100; // make bit 2 of PORT D a 1 (set the bit), leaving rest alone

Have not tried same for analogRead, suggest you use as is so time is allowed for the 110uS conversion.

My advice is "Don't do that."

It makes it impossible to use your program again on a different processor. If you get halfway through your project and find you need to upgrade to a bigger or faster Arduino, then all that code stops working.

Some things are common between related processors, like the Uno and the Mega but even those will have different registers for some functions.

There is a digitalWriteFast() function which skips some of the smart stuff that digitalWrite() does, but even then I would not normally use that.

Thank's CrossRoads.

I have tried to read/write to the direction register DDRD and data register PORTD and and PIND and that seems tho work fine. One byte covers all digital pins 0 to 7 wehere each bit representing pin.

My problem is with the analog inputs. Do I need somehow to adress one pin at a time, because here the status of a pin is not just LOW or HIGH (0/1) but a whole byte(s).

I have tried to read PINC and it seem to read pin A0-A5 as digital inputs (like the output from a comparator). But in what register do I find the analog value of each pin??

But in what register do I find the analog value of each pin??

The setup and register reading for the ADC values is more complex than accessing a PINX register.
Take a look at Nick Gammon's tutorial on ADC conversion on the Arduino (analogRead)

You would also do well to download the data sheet for the ATmega 328 and dig in to the analog sections.

analogRead signals have to go thru the analogMux before the ADC can convert them. That's why you can't do a port read of the analog inputs. That only works if reading the digital inputs.

The other thing (well, one of the other things) about analogRead is that it isn't instantaneous, so you have to set the mux, start the conversion and then wait a lifetime until the conversion is finished.

Luckily, you have all the source code of how this happens,

Thank's all of you for taking time to answer.

I think it cleared up some of my questions
I think I stick to the analogRead() function. :slight_smile:

But when it comes to the digital inputs it sometimes seems to make seems to be able to read all the pins in parallel with for example PIND.

Once again thanks /GG