Cintillo Institucional

/*
Carlos Soto
check 21/03/11
RTC DS1307 (modificación en sitio)
SHT15
8 salidas
4 entradas
*/


//Este programa realiza una verificación del rct (real clock time),
//en este caso utilizando el chip DS1307, permitiendo realizar las 
//operaciones permitidas, como configurar la hora, asi como
//también la lectura desde el el DS1307, esta es la base del código 
//del proyecto HAPA, pues tenemos un control programado de riego. 
//La verificación se realiza mediante el puerto serial
//Ademas se realiza una visualizacion de la hora actual por LCD
//Se agrega la medicion de temperatura


#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

//Declaracion de LCD mediante la libreria LiquidCrystal.h
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);


// Convirtiendo número decimal en bcd
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convirtiendo bcd en número decimal
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(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
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

///////////////////////
//Configuracion de las entradas
//para la interface, 
//     Boton01 (visualizacion)
//Boton02,Boton03 (Modificacion)
//     Boton04 (set)
//Manejo de Entradas
const int inPin01 = 4; 
const int inPin02 = 5; 
const int inPin03 = 6; 
const int inPin04 = 7; 
//Variables de control de botones
int boton01=0;
int boton02=0;
int boton03=0;
int boton04=0;
//Retardo
int dd2=500;
//Modificacion de Hora
float Horatemp=12;
int boton02temp;
int boton03temp;

//////////////////////////////////////
//Configuracion del expansor

int dd=50;
#define expander B0100000  // Direccion del expansor alpha-num
                           // 0  1   0   0   A2  A1  A0
                           // 0  1   0   0   0   0    0
// Note that the R/W bit is not part of this address.
/////////////////////////////////////////
// Pegando el expansor
void expanderWrite(byte _data ) {
  Wire.beginTransmission(expander);
  Wire.send(_data);
  Wire.endTransmission();
}



 


// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 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());
}





///////////////////////////////////
//Inicio Configuracion sensor SHT15
//////////////////////////////
//////////////////////////////
#define comandoTemp 0x03 //Comando para obtener la temperatura del SHT15
#define comandoHume 0x04 //Comando para obtener la humedad del SHT15

#define pinDatos 3
#define pinReloj 2

float temperatura,
        humedad;

///////////SHT15////////////////////////
//Funciones para la lectura del SHT15///
int leerDatos (int numBits) //Función para leer datos en forma serial.
{
  int deci = 0;

  for (int i=0; i<numBits; i++)
  {
    digitalWrite(pinReloj,HIGH);
    deci = (deci<<1) + digitalRead(pinDatos);
    digitalWrite(pinReloj,LOW);
  }
  return deci;
}

void iniciarTrans()//Rutina para iniciar la transmisión
{
  digitalWrite(pinReloj,HIGH);
  pinMode(pinDatos,OUTPUT);
  digitalWrite(pinDatos,LOW);
  digitalWrite(pinReloj,LOW);
  digitalWrite(pinReloj,HIGH);
  digitalWrite(pinDatos,HIGH);
  digitalWrite(pinReloj,LOW);
}

void enviarComando(int comando)
{
  //shiftOut(pinDatos, pinReloj, MSBFIRST, comando);
  for(int i=0;i<8;i++){
    digitalWrite(pinDatos,!!(comando & (1<<(7-i)))); //Enviar datos en
//forma serial. Tomado de la función shiftOut
    
    digitalWrite(pinReloj,HIGH);
    digitalWrite(pinReloj,LOW);
  }
  digitalWrite(pinReloj,HIGH);
  pinMode(pinDatos,INPUT);

  /*if (digitalRead(pinDatos))
    Serial.println("Error del ACK bajo");*/

  //PORTB&=~_BV(PB0);/*
  digitalWrite(pinReloj,LOW);

  /*if (!digitalRead(pinDatos))
    Serial.println("Error del ACK alto");*/
}

int recibirDato()
{
  int datos;

  pinMode(pinDatos,INPUT);
  while(digitalRead(pinDatos)){
  
  }

  datos = leerDatos(8) << 8;

  pinMode(pinDatos,OUTPUT);
  digitalWrite(pinDatos,LOW);
  digitalWrite(pinReloj,HIGH);
  digitalWrite(pinReloj,LOW);
  pinMode(pinDatos,INPUT);

  return datos |= leerDatos(8);
}

float tempe()
{
  int dato;
  float medida;
  iniciarTrans();
  enviarComando(comandoTemp);
  dato=recibirDato();
  return medida=-40.1+0.01*dato;
}

float hume()
{
  int dato;
  float medida;

  iniciarTrans();
  enviarComando(comandoHume);
  dato=recibirDato();
  return medida=-2.0468+0.0367*dato+-1.5955e-6*dato*dato;
}

// Fin configuracion SHT15
///////////////////






void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
  pinMode(pinReloj,OUTPUT);
  // Codigo que permite configurar de manera directa el 
  // Reloj, forzando a la hora copiada, es necesario realizarla al 
  // depurar progrmas, cuando el codigo permita realizar configurar
  // la hora desde el teclado, no es necesario realizarla 
  //second = 45;
  //minute = 31;
  //hour = 15;
  //dayOfWeek = 4;
  //dayOfMonth = 10;
  //month = 11;
  //year =10;
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  
  //Inicializacion de la LCD
  // Ubicando el cursor de la LCD mediante numero de columna y fila
  lcd.begin(16, 2);
  // Imprimiento el mensaje de inicio en la LCD.
  lcd.print("HAPA 004");
}

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

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
//  Serial.print("HAPA 001  ");
//  Serial.print(hour, DEC);
//  Serial.print(":");
//  Serial.print(minute, DEC);
//  Serial.print(":");
//  Serial.print(second, DEC);
//  Serial.print("  ");
//  Serial.print(dayOfMonth, DEC);
//  Serial.print("/");
//  Serial.print(month, DEC);
//  Serial.print("/");
//  Serial.print(year, DEC);
//  Serial.print("  Dia:");
//  Serial.println(dayOfWeek, DEC);
  
  //////////////////////////
  //Mostrando la Hora
  //en la posicion de LCD 1,0
  //con el formato 08:04
  /////////////////////
  //lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hora:");
  lcd.setCursor(0,1);
  if ((float)hour < 10) {
    lcd.print("0");
    lcd.print((float)hour,0);
  }
  else {  
  lcd.print((float)hour,0);
  }
  lcd.print(":");
  if ((float)minute < 10) {
    lcd.print("0");
    lcd.print((float)minute,0);
  }  
  else {
    lcd.print((float)minute,0);
  }
  ///////////////////////
  
  ///////////////////////
  //Configuracion de las entradas
  //para la interface, 
  //     Boton01 (visualizacion)
  //Boton02,Boton03 (Modificacion)
  //     Boton04 (set)
  
  //Actualiza el B0000 (boton01-04)
  if (digitalRead(inPin04) == LOW) {     
    boton04=boton04+1;
    delay(dd2);
  } 
  else
      if (digitalRead(inPin03) == LOW) {     
        boton03=boton03+1;
        delay(dd2);
      } 
      else
           if (digitalRead(inPin02) == LOW) {     
           boton02=boton02+1;
           delay(dd2);
      } 
           else 
               if (digitalRead(inPin01) == LOW) {     
               boton01=boton01+1;
               delay(dd2);
               }

  //////////////////
  //Programa de Prueba
  //para los auxiliares de los botones de interface
  /////////////////////
  if (boton01==5) {boton01=0;}
  if (boton02==5) {boton02=0;}
  if (boton03==5) {boton03=0;}
  if (boton04==5) {boton04=0;}

  lcd.setCursor(11, 0); 
  lcd.print("B");
  lcd.print(boton01);
  lcd.print(boton02);
  lcd.print(boton03);
  lcd.print(boton04);
  //lcd.autoscroll();
  // delay(1000);
  
  ////////////////
  //Entrando a Pantalla 1
  //Manipulacion en sitio del RTC
  ////////////
  
  float hourtemp;
  float mintemp;
  hourtemp=(float)hour;
  mintemp=(float)minute;


  while (boton01==1){
    
  //Para la Hora   
  if (boton04==1){
  //delay(5000);
  lcd.setCursor(1, 1); 
  lcd.blink(); 
  //delay(2000);



  if (digitalRead(inPin03) == LOW) {     
      boton03=boton03+1;
      delay(dd2);
      hourtemp=hourtemp+1;
      lcd.setCursor(0, 1);
      if (hourtemp < 10) {
        lcd.print("0");
        lcd.print(hourtemp,0);
      }
     else {  
        lcd.print(hourtemp,0);
      }
  
    } 
  else 
    if (digitalRead(inPin02) == LOW) {     
       boton02=boton02+1;
       delay(dd2);
       hourtemp=hourtemp-1;
       lcd.setCursor(0, 1);
       if (hourtemp < 10) {
        lcd.print("0");
        lcd.print(hourtemp,0);
       }
         else {  
         lcd.print(hourtemp,0);
       }

   }
  
   } 



   //Para los minutos   
   if (boton04==2){
   //delay(5000);
   lcd.setCursor(4, 1); 
   lcd.blink(); 
   //delay(2000);



  if (digitalRead(inPin03) == LOW) {     
      boton03=boton03+1;
      delay(dd2);
      mintemp=mintemp+1;
      lcd.setCursor(3, 1);
      if (mintemp < 10) {
        lcd.print("0");
        lcd.print(mintemp,0);
      }
     else {  
        lcd.print(mintemp,0);
      }

    } 
   else 
    if (digitalRead(inPin02) == LOW) {     
       boton02=boton02+1;
       delay(dd2);
       mintemp=mintemp-1;
       lcd.setCursor(3, 1);
       if (mintemp < 10) {
        lcd.print("0");
        lcd.print(mintemp,0);
       }
         else {  
         lcd.print(mintemp,0);
       }

   }
  
   } 







   if (digitalRead(inPin04) == LOW) {     
       boton04=boton04+1;
       delay(dd2);
   }
   if (digitalRead(inPin01) == LOW) {     
       boton01=boton01+1;
       delay(dd2);
       setDateDs1307(second, mintemp, hourtemp, dayOfWeek, dayOfMonth, month, year);       
   }
   if (boton04==3) {
     boton04=1;
   }


 }//Fin del 
 ///////////////////
 //Fin rutina manipulacion RTC
 //////////////////////////////
 
  // Turn off the cursor:
  //lcd.noCursor();
  //delay(500);
   // Turn on the cursor:
//  Horatemp=(float)hour;


//  lcd.setCursor(0,1);
//  lcd.print(Horatemp,0);
//  lcd.print((float)hour,0);
//  delay(500);
//  lcd.setCursor(0,1);
//  lcd.print("  ");
//  delay(500);
//  
//  if (boton02temp<boton02)
//  {Horatemp=Horatemp-1;
//  }  
//  //while (boton01==1) {
//       
//  boton02temp=boton02;
//  boton03temp=boton03;
//    
//} 

  expanderWrite (B10000000);
  delay(dd);
  expanderWrite (B01000000);
  delay(dd);
  expanderWrite (B00100000);
  delay(dd);
  expanderWrite (B00010000);
  delay(dd);
  expanderWrite (B00001000);
  delay(dd);
  expanderWrite (B00000100);
  delay(dd);
  expanderWrite (B00000010);
  delay(dd);
  expanderWrite (B00000001);
  delay(dd);
  expanderWrite (B11111111);
  delay(dd);
  
  
  ///////////////////////////////
  //Lectura y Visualizacion del SHT15 
  ///////////////////
  temperatura=tempe();
  delay(800);
  humedad=hume();
  delay(18000);
  //temperatura=10;
  //humedad=5;
  //Serial.print("HAPA 001  ");
  //Serial.print(temperatura,DEC);
  //Serial.print(":");
  //Serial.print(humedad,DEC);
  //delay(20);
  

  lcd.setCursor(8,1);
  lcd.print("t=");
  lcd.print(temperatura,0);
  lcd.print(";h=");
  lcd.print(humedad,0);

  //delay(1800);
  


  
}//Fin del loop

hapa05 (última edición 2011-03-23 20:14:46 efectuada por _desactivada_csoto)