readDS3231time - * and & meaning

The two readDS3231time() functions below use * and & before values. What are the different symbols * and & telling me about the operation? (I've only included the first few instances).
readDS3231time(byte *second, byte *minute, .......)
readDS3232time(&second, &minute, ......)

These lines I have taken from Project 57 in John Boxall's Arduino Workshop book where everything is explained except the use of these symbols - possibly on the assumption that everyone knows what they do - however I don't and have failed to find out.

1 Like

Dereferencing is one of the features specifically for use with pointers. The asterisk operator * is used for this purpose. If p is a pointer, then *p represents the value contained in the address pointed by p. Check this link:

Hard to tell exactly with no context.

This

readDS3231time(byte *second, byte *minute, .......)

Might be the start of a function that expects to receive pointers to bytes as arguments.


This

readDS3232time(&second, &minute, ......)

Might be a call to a function that expects some kind of pointers as arguments, the '&' operator takes the address of the variable so it will be a pointer received by the function.

In this way, a function can reach back, so to speak, and change the values of any variables it has been given the address of, here presumably to set second, minute and so forth to the current time it thinks it is.


So google

   arduino pointer address of

and poke around. You in the land of a serious next step: learning about pointers, addresses and how they relate to the use of arrays in C/C++.

HTH

a7

Thanks gilshultz and alto777 - explains why I couldn't find an explanation in the "easy" areas I looked in. I did think that the "&" symbol was somehow referencing the latest version of the variable (e.g. second, minute) from the fact that this was looping around every few seconds - otherwise it would just have picked up the original declared value of second, minute. But the * symbol didn't seem to be doing much other than distinguish the variables from similar variables in another function displayTime() where second, minute, ... are defined. As you can tell - I'm new to C even if not new to programming.

You're still confused. You should find a good beginning C++ (the language of Arduino, not 'C') tutorial. This One is pretty thorough.

Thanks for the pointer to the C++ website - looks very thorough. I've read the relevant sections. I think I'm beginning to understand but am willing to be corrected. The variables are allocated memory addresses when the first call to the DS3231 RTC which set the start values for second, minute, hour, year, etc. I understand that the program needs these memory locations to be referenced explicitly when getting the latest values for the variables second, minute, hour, year etc. This is how I interpreted the use of the dereference operator "*" - this makes sense in the readDS3231time(byte *second, byte *minute ....) call but why the use of the address of operator "&" pointer in a subsequent readDS3231(&second, &minute, ....) within the displayTime() routine when the program doesn't use the addresses of these memory locations - just the values at these addresses? Why not just appeal again to the " * " pointer again instead? Except in assembler - I've never had to explicitly reference memory locations before - have got a lot to learn in Arduino.

The problem is, most people probably don't have that book. So, we can't see the context of the two isolated lines that you plucked out of it.

Please post the complete code you're referring to. Be sure to provide GitHub links to any libraries that you #include.

1 Like

Yes - Sorry - I thought his book was better known. Tho following code is my amended version of his project 57 - main changes to how the data is sent to the serial printer.

[code] // Written by John Boxall from http://tronixstuff.com

#include "Wire.h" // Allows I2C to talk to board
//------------------------------------------------
#define DS3231_I2C_ADDRESS 0x68
// -------Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){ return( (val/1016) + (val%10) );}
// --------Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){ return( (val/16
10) + (val%16) );}
//-------------------------------------------------
void setup(){ // =====================
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,0,0,2,5,7,24);
} //==================================
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year){ //++++++++++++++++++++++++
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); //set next input to start at the seconds register
// Wire.write(0x0E); // select register
// Wire.write(0b00011100); // write register bitmap, bit 7 is /EOS
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void readDS3231time(byte second,
byte minute,
byte hour,
byte dayOfWeek,
byte dayOfMonth,
byte month,
byte year){ //
***********************************
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f);
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
} //
********************************************

void displayTime(){ //~~~~~~~~~~~~~~~~~~~~~~~~~~~
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
if (dayOfMonth<10) { Serial.print("0");}
Serial.print(dayOfMonth, DEC);
Serial.print(":");
if (month<10) { Serial.print("0");}
Serial.print(month, DEC);
Serial.print(":");
Serial.print("20");
Serial.print(year, DEC);
Serial.print(" ");
if (hour<10) { Serial.print("0"); }
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10) { Serial.print("0"); }
Serial.print(minute, DEC);
Serial.print(":");
if (second<10) { Serial.print("0"); }
Serial.print(second, DEC);
Serial.println(); // to create new line
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void loop(){ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(5000); // every 5 seconds
} //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
[/code]
I hope I've inserted the code correctly.

You haven't.
Please use code tags when posting code.

like this

You didn't, but I copy / pasted into the Arduino IDE to look at.

Is that the code from the book verbatim? If so throw away that book, it's garbage. It defines the readDS3231time() function as taking byte parameters:

void readDS3231time(byte second,
                    byte minute,
                    byte hour,
                    byte dayOfWeek,
                    byte dayOfMonth,
                    byte month,
                    byte year) {

Yet it calls the function with pointer-to-byte arguments:

  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

Also, the other notation you asked about doesn't appear in the code at all:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.