The first thing that we have to do is to make this electronic circuit in Proteus:
To don't focus all my efforts in unnecesary things, most of the parts of this circuit are explained in the previous I2C practice, so you can find out what are them in this link: http://marcoselectronicblog.blogspot.com.es/2016/05/temperature.html
The main difference with the previous practice is that here we use the DS1307 RTC:
This is a Real Time Clock, compossed by a battery of 3V, a crystal, to send regular clock pulses (in this case, 32.768 kHz) and some connection outputs and inputs (SCL and SDA), that are connected to the Arduino.
If we debug the exit with the I2C Debugger, we can see the sequences that it sends, where:
-The first numbers are the time that it lasts doing the execution
-S: start bit
-A: acknowlege bit
-P: bit of stop
-D0/D1: it's the order to read or write
-The next characters are the direction and the other is the data
In the virtual terminal, we can see clearly what we can see in the debugger:
If we pause the program and we go to the variables, we can see 12 variables (from 0 to 11) which correspond to the months, with it's address and it's value.
We also can see the time and the date in the variables:
Moreover, we can see the program that we have in our arduino and the library, with it's ensambler orders:
//Here, we include the libraries:
#include <Wire.h>
#include "RTClib.h"
//Then, we call the program:
RTC_DS1307 rtc;
//Here, we create a funtion called printnvram:
void printnvram(uint8_t address) {
Serial.print("Address 0x");
Serial.print(address, HEX);
Serial.print(" = 0x");
Serial.println(rtc.readnvram(address), HEX);
}
//Here, we start the program:
void setup () {
//We initialize the serial port at 9600 bauds
Serial.begin(9600);
#ifdef AVR
//We call the I2C library for the arduino
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
//We start the rtc
rtc.begin();
// We print on the screen that the NVRAM has started
Serial.println("Current NVRAM values:");
//We do it 7 times, calling the function previously created
for (int i = 0; i < 6; ++i) {
printnvram(i);
}
// Write some bytes to non-volatile RAM storage.
// NOTE: You can only read and write from addresses 0 to 55 (i.e. 56 byte values).
Serial.println("Writing NVRAM values.");
// We write 0 in the address FE and 1 in the address ED:
rtc.writenvram(0, 0xFE);
rtc.writenvram(1, 0xED);
// We write 2 in BE, EF, 01 and 02:
uint8_t writeData[4] = { 0xBE, 0xEF, 0x01, 0x02 };
rtc.writenvram(2, writeData, 4);
// Read bytes from non-volatile RAM storage.
Serial.println("Reading NVRAM values:");
// Here, we read the rtc values.
Serial.println(rtc.readnvram(0), HEX);
Serial.println(rtc.readnvram(1), HEX);
// We read multiple bytes at the same time:
uint8_t readData[4] = {0};
rtc.readnvram(readData, 4, 2);
Serial.println(readData[0], HEX);
Serial.println(readData[1], HEX);
Serial.println(readData[2], HEX);
Serial.println(readData[3], HEX);
}
//The program stays all the time stucked in the loop, until we stop it manually
void loop () {
// Do nothing in the loop.
}
You can download this program from here:
https://drive.google.com/folderview?id=0B20kHfrc3NnpVDUtNFF2aWpjWHM&usp=sharing
No hay comentarios:
Publicar un comentario