Ciao a tutti.
Ho un problema curioso. Praticamente l'aggeggio che devo costruire deve misurare diverse cose (tra cui la temperatura) in determinati intervalli di tempo e memorizzarli su una 24C1024. Per tenere traccia dell'ora uso un ds1307. Bene, monto provo il tutto ma a volte l'ora per qualche istante segna 0:4:40 con data casuale, oltre al fatto che a volte si blocca pure il tutto. Vi riporto il codice. Io ho l'impressione che sia un problema hardware dell'rtc, voi che ne pensate?
#include <WProgram.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include <Wire.h>
#include <E24C1024.h>
#define ver 0.7.2
#define rx 2 //To RS232
#define tx 3 //To RS232
#define DS1307_addr 0x68 //RTC address
#define STRLEN 8
#define MIN_ADDRESS 0 //Start of external memory
#define MAX_ADDRESS 131072 // 1 device -> total amount of 128 KB
SoftwareSerial rs232=SoftwareSerial(rx,tx);
int out=13; //Sensor reading debug
int fan=12; //Turning ON or OFF fans
int i=1; //Counter used to read memory
long c=0; //Counter
unsigned long d=0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, control;
int sample=6; //Used by readTemp function
int lm35=0; //Used by readTemp function
int temp=0; //Used by readTemp function
int count=0; //Used by readTemp function
int t;
int minuto;
int ora;
void setDate(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year, // 0-99
byte control) // Second square wave, pin 7
{
Wire.beginTransmission(DS1307_addr);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.send(decToBcd(control));
Wire.endTransmission();
}
//Reading temperature. LM35 on analog pin 0
int readTemp(){
temp=0;
for(count=0;count<sample;count++)
{
temp+=analogRead(lm35);
delay(250);
}
temp/=sample;
temp=(5.0temp100.0)/1024.0;
return temp;
}
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// Gets the date and time from the DS1307
void getDate(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
Wire.beginTransmission(DS1307_addr);
Wire.send(0); // Reset the register pointer
Wire.endTransmission();
Wire.requestFrom(DS1307_addr, 7);// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f);// Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
//'Setup' function
void setup()
{
Serial.begin(9600);
second = 00;
minute = 15;
hour = 15;
dayOfWeek = 4;
dayOfMonth = 28;
month = 1;
year = 9;
control=18; //Blinking led
Wire.begin(); //Initialize I2C bus
//setDate(second, minute, hour, dayOfWeek, dayOfMonth, month, year,control);
pinMode(out,OUTPUT); //Sensor debug
pinMode(fan,OUTPUT);// Set pin 12 like a switch
pinMode(sensor,OUTPUT); //Set pin 11 like a switch
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(fan,HIGH);
digitalWrite(fan,LOW);
}
//Main loop
void loop()
{
getDate(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if(( minute>=0&&minute<=20)||( minute>=30&&minute<=51))
{
EEPROM1024.write(c,hour);
c++;
EEPROM1024.write(c,minute);
c++;
EEPROM1024.write(c,dayOfMonth);
c++;
EEPROM1024.write(c,month);
c++;
EEPROM1024.write(c,year);
c++;
t=readTemp();
EEPROM1024.write(c,t);
c++;
digitalWrite(out,HIGH);
}
for(d=0; d<=3000000; d++){
}
Serial.print(hour,DEC);
Serial.print(":");
Serial.print(minute,DEC);
Serial.print(":");
Serial.println(second,DEC);
digitalWrite(out,LOW);
if((hour==10&&minute==0)||(hour==15&&minute==0))
{
digitalWrite(out,HIGH);
digitalWrite(fan,HIGH);
for(c=0;c<=MAX_ADDRESS;c++)
{
rs232.print(EEPROM1024.read(c),DEC);
rs232.print(";");
i++;
if(i>=7)
{
rs232.println();
i=1;
}
}
c=0;
}
}