Fikret
Published

Table Clock with Temperature, Humidity & Light Level Measure

It is clock. In fact, it's not only a clock. It measures temperature, humidity, light level. Several sensors or devices can be connected.

AdvancedWork in progressOver 2 days263
Table Clock with Temperature, Humidity & Light Level Measure

Things used in this project

Hardware components

Microchip PIC24FJ256GB106
×1
WH2004A 4X20 lcd
×1
PCBWay Custom PCB
PCBWay Custom PCB
×1
Real Time Clock (RTC)
Real Time Clock (RTC)
×1
Microchip 2464
×1
SHT11
×1

Story

Read more

Custom parts and enclosures

PCB

File missing, please reupload.

Schematics

PCB Gerber

Link order this project pcb from PCBWAY

https://www.pcbway.com/project/shareproject/W118076ASJ3_FD_DGCLK_R01___CADCAM.html
File missing, please reupload.

Gerbers

Code

Main routine

C/C++
Main Routine
//Bismillahirrahmanirrahim
#include <main.h>
#include <def.h>
#include <regs.h>
#include <2465x.c>
#include <ds_1307.c>
#include <ds18b20.c>
#include <func.c>
#include <lcd4x16.c>
#include <string.h>

#INT_EXT0
void  ext0_isr(void) 
{

}

#INT_RDA
void  rda_isr(void) 
{

}

#INT_RDA2
void  rda2_isr(void) 
{
   counterData=0;
   counterTemp=0;
   permissionToAttend=0;    //eğer debug porttan veri varsa çalışmayı kes ve veri ile uğraş burası tehlikeli olabilir
   hiX(buzzer);
   do{
            if(kbhit(CommDbg))
            {
                  counterData=fgetc(CommDbg);
                  if(counterData < maxRda2Data)  //gelen veriler fazla uzun ise  250 karakterden sonrasını önemseme 
                  {
               
                     rda2Data[counterData]=counterData;
                     counterData++;
                     rda2Data[counterData]='\0';
                     
                     //gsm_rda_data[counterData]='\0';
                     counterTemp=0;
                  }
            }
            counterTemp++;
            
      }while(counterTemp<2500);
            //gsm_rda_data[counterData]=0;
            
            
           //fprintf(CommDbg,"Data:%s\r\n"rda2Data);
      rda2IntActivity=1;
      loX(buzzer);
     
      
}



void main()
{

  
   

   setup_adc_ports(NO_ANALOGS, VSS_VDD);


   init_ext_eeprom();
   bekle(tKisa);
   ds1307_init(); // saat init
   bekle(tKisa);
   //enable_interrupts(INT_EXT0);
   //enable_interrupts(INT_RDA);
   enable_interrupts(INT_RDA2);

   
   enable_interrupts(INTR_GLOBAL);


   //Pin Konfigurasyonu burada
   //userLed
   output_drive(userLed);
   //buzzer
   output_drive(buzzer);
   //Butonlar
   output_float(buton1);
   output_float(buton2);
   output_float(buton3);
   output_float(buton4);
   output_float(buton5);
   
   //HMTRP RF pin konfigurasyon
   output_drive(rfEnable);
   output_drive(rfConf);
   
   //lcd ayarlama rutini
   lcd_ayarla();
   setBacklight(disable);
   
   //HMTPRP rf modül ayarla 
   hmtrp(disable);
   bekle(tSn);
   hmtrp(enable);
  bekle(tSn);
   bekle(tKisa);
   
   
   
   //Başlıyoruz
   backlightVal=lHi;
   setBacklight(backlightVal);
   /* // LCD Backlight Test rutini
   for ( genCounter=0; genCounter<120;  genCounter++)
   {
      backlightVal=genCounter * 500;
      setBacklight(backlightVal);
      adres(1,1);
      printf(lcd_yaz,"LCD Backlight Dimm: %lu"backlightVal );
      bekle(tSn);
   }
   */
   
   fprintf(CommDbg,"FiDeNet\r\n");
   fprintf(CommRf,"FiDeNet\r\n");
   
   genCounter=0;
   
   lcd_sil();
   adres(1,1);
   printf(lcd_yaz,"PCB Ref:%s"deviceData[cHWID].hwStr);
   adres(1,2);
   printf(lcd_yaz,"Sw Ver: %s"deviceData[cSWVersion].hwStr);
   adres(1,3);
   printf(lcd_yaz,"Hw Rev: %s"deviceData[cHWRev].hwStr);
   adres(1,4);
   printf(lcd_yaz,"By: %s"deviceData[mfc].hwStr);
   
   bekle(tSn);
   lcd_sil();
   adres(1,1);
   printf(lcd_yaz,"       FiDeNet"); 
   adres(1,3);
   printf(lcd_yaz,"    Digital Clock");
   
   beep(2,2);
   
   bekle(tSn);
   lcd_sil();
   //saat ayarlama kısmı
   /*
   ds1307_set_time(10,48,30);                      //Saat ayarla
   bekle(tKisa);
   ds1307_set_date(10,9,18,2);                     //Tarih ayarla dow değeri önemli olmasa da ayrla belki ilerde kullanılır.
   bekle(tKisa);
   
   */
  
   
   /* //eeprom test rutini
    fprintf(CommDbg,"EEPROM Test Rutini %s \r\n"strGen1);
   eepromWrTest();
   bekle(tMini);
   eepromRdTest();
   */
     
   //read_dev_id();
   isReadDevIdValid=0;
   
   if(readDevId()==1)
   {
      fprintf(CommDbg,"Dev ID:%s \r\n"devId);
   
   }
   else
   {
      fprintf(CommDbg,"devId N/A\r\n");
   }
    genCounter=0;
   
   while(TRUE)
   {
      
      enable_interrupts(INT_RDA2);
      genCounter++;
     
        
      bekle(tMicro);
      output_toggle(userLed);
      
      
      
      if ((genCounter % 500)==0)
      {
         //myClock.year=year+2000;
         ds1307_get_time(myClock.hrs,myClock.min,myClock.sec);
         adres(1,1);
         printf(lcd_yaz,"Saat:  %2u:%2u:%2u"myClock.hrs,myClock.min,myClock.sec);
      }
      
       if ((genCounter % 500)==150)
      { 
         ds1307_get_date(myClock.day,myClock.month,myClock.year,myClock.dow);
         adres(1,2);
         printf(lcd_yaz,"Tarih: %2u/%2u/20%u"myClock.day,myClock.month,myClock.year);         
      }
      
      if ((genCounter % 500)==450)
      {
         temperature = ds1820_read();
         adres(1,3);
         printf(lcd_yaz,"Hava %3.1f%cC"temperature,223);
      }   
      
      if(input(buton1)==0)
      {
         fprintf(CommDbg,"buton1\r\n");    
      }
      
      if(input(buton2)==0)
      {
         fprintf(CommDbg,"buton2\r\n");    
      }
      if(input(buton3)==0)
      {
         fprintf(CommDbg,"buton3\r\n");    
      }
      if(input(buton4)==0)
      {
         fprintf(CommDbg,"buton4\r\n");    
      }
      if(input(buton5)==0)
      {
         fprintf(CommDbg,"buton5\r\n");    
      }
      
      
      if(rda2IntActivity==1)
         {
            //fprintf(CommDbg,"Data2:%s\r\n"rda2_data);
            rda2IntActivity=0;
            
                        
            strcpy(tempBuffer,myCmd[AT].str);// AT
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"AT:OK\r\n");
            }
            strcpy(tempBuffer,myCmd[ATI].str);// GET device_id
            
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"ATI:%s\r\n"devId);
            }
           
            strcpy(tempBuffer, myCmd[ATHW].str);// GET cHW_id
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"ATHW:%s\r\n"deviceData[cHWId].hwStr);
            }
            
            strcpy(tempBuffer, myCmd[ATHWr].str);// GET Hardware revizyon
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"ATHWr:%s\r\n"deviceData[cHWRev].hwStr);
            }
            
            strcpy(tempBuffer, myCmd[ATSW].str);// GET Sofware revizyon
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"ATSW:%s\r\n"deviceData[cSWVersion].hwStr);
            }
            
            strcpy(tempBuffer,myCmd[ATC].str); // clear device_id
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               for (counterTemp=0;counterTemp<20;counterTemp++)
               devId[counterTemp]='\0';
               writeDevId();
               fprintf(CommDbg,"ATC:OK\r\n");
            }
            
            strcpy(tempBuffer,myCmd[ATr].str); // clear device_id
            if(strcmp(rda2Data,tempBuffer)==0)
            {
               fprintf(CommDbg,"ReBoot\r\n");
               bekle(tOrta);
               reset_cpu();
            }
            
            
            strcpy(tempBuffer,myCmd[ATW].str);//write device_id
            if(strstr(rda2Data,tempBuffer)>13)
            {
               //fprintf(CommDbg,"ATW\r\n");
               counterGen2=strlen(rda2Data);
               if((counterGen2<26)&&(counterGen2>20))
               {
               
                  for(counterData=0;rda2Data[counterData]!=':';counterData++)
                  {}
                  counterData++;
                  
                  //fprintf(CommDbg,"str_len:%lu\r\n"strlen(rda2_data));
                  
                 counterGen2=counterGen2-counterData-2;
                  for(counterTemp=0;counterTemp!=counterGen2;counterTemp++)
                  {
                     devId[counterTemp]=rda2Data[counterTemp+counterData];
                  }
                  devId[counterTemp]='\0';
                  writeDevId();
                  fprintf(CommDbg,"ATW:OK\r\n");
                 
               }
               else
               {
                  fprintf(CommDbg,"ATW:ERROR\r\n");
               }
            }
       
       
         }
      
      
      
      //TODO: User Code
   }

}

Functions

C/C++
Functions
void hmtrp(u8 state)
{
   if (state==disable)
   {
      hiX(rfEnable);
      hiX(rfConf);
   }
   if(state==enable)
   {
      loX(rfEnable);
      hiX(rfConf);
   }
}
void setBacklight(u16 value)
{
      set_pwm_duty(1,value);  
}


void beep(u8 sure,u8 time=1)
{
   u8 genCntr;
   setBacklight(lLo);
   for (genCntr=0;genCntr<time;genCntr++)
   {
      hiX(buzzer);
      bekle(sure * 100);
      loX(buzzer);
      bekle(sure * 100);
   }
   setBacklight(backlightVal);
}

u1 readDevId(void)
{
   u8 sayac;
   char temp;
   readDevIDOk=0;
   for(sayac=0;sayac<19;sayac++)
   {
      temp=read_ext_eeprom(sayac + devIDAddr);
      if((temp>32)&&(temp<128))
      {
         devID[sayac]=temp;
         readDevIDOk++;
      }
      else
      {
          if(sayac>1)
          {
            devID[sayac]="\0";
          }
          
      }
      
   }
   devID[sayac]='\0';
   
   
   if(readDevIDOk>15)
   {
      //fprintf(comm_dbg,"dev_id=%s\r\n"dev_id);
      return 1;
   }
   else
   {
      return 0;
   }
   
   

}

void writeDevId(void)
{
   u8 sayac2;
   char chr;
   u16 addr;
   
   
   for(sayac2=0;sayac2<20;sayac2++)
   {
      addr=sayac2+devIDAddr;
      chr=devID[sayac2];
      write_ext_eeprom(addr,chr);
   }
}

void eepromWrTest()
{
   u8 sayac;
   fprintf(CommDbg,"eeprom Write Test\r\n");
   for (sayac=1;sayac<20;sayac++)
   {
      write_ext_eeprom(sayac,65+sayac);
      bekle(tMini);
   }
}

void eepromRdTest()
{
   u8 sayac;
   char okunan[23];
   okunan[0]="A";
   for (sayac=1;sayac<20;sayac++)
   {
      okunan[sayac]=read_ext_eeprom(sayac);
      bekle(tMini);
   }
   okunan[sayac]=0;
   fprintf(CommDbg,"EEprom Test: %s\r\n"okunan);
   
}

Definitons

C Header File
typedef  int1 u1;
typedef  unsigned int8 u8;
typedef  volatile unsigned int8 vu8;
typedef  unsigned int16 u16;
typedef  volatile unsigned int16 vu16;
typedef  unsigned int32 u32;


#define bekle(x) delay_ms(x)

#pin_select U1TX=PIN_B6
#pin_select U1RX=PIN_B7
#use rs232(UART1, baud=9600, stream=CommRf)

#define rfEnable Pin_G9
#define rfConf Pin_B3

#pin_select U2TX=PIN_B2
#pin_select U2RX=PIN_B1
#use rs232(UART2, baud=115200, stream=CommDbg)

#define hiX(x) output_high(x)
#define loX(x) output_low(x)

#define ONE_WIRE_PIN PIN_D10 // sensörün DQ bacağının bağlanacağı pin

#define disable 0
#define enable 1


#define userLed Pin_B15
#define buzzer  Pin_G8

#define buton1 Pin_E7
#define buton2 Pin_E6
#define buton3 Pin_E5
#define buton4 Pin_G7
#define buton5 Pin_G6

//#define lcdBacklight Pin_B14

#define tSn 1000
#define tUzun 500
#define tOrta 100
#define tKisa 50
#define tMini 10
#define tMicro 1


#define lHi 16000
#define lMe 6000
#define lLo 1000
#define lMi 500
#define l0 0

LCD Lib

C/C++
/****************************BEST ENGINEER*************************************/

////////////////////////////////////////////////////////////////////////////////
//                             TR_LCD.c                                    /////
//                                                                         /////
// Bu LCD sürücü dosyası 2x16 HD44780 uyumlu LCD’ler                       /////
// içindir. LCD’ye sadece yazma yapılacağı için R/W Logic0                 /////
// konumunda durması için şaseye bağlanmalıdır.                            /////
//    lcd_ayarla(); bu komut LCD'nin başlangıç ayarlarını yapmak içindir.  /////
////////////////////////////////////////////////////////////////////////////////
//    lcd_sil(); bu komut Lcd'yi silmek için kullanılır.                   /////
////////////////////////////////////////////////////////////////////////////////
//    adres(x,y); komutuyla imleç istenilen adrese getirilebilir.          /////
// Bu komutta y satır bilgisini, x sütun bilgisini ifade eder.             /////
////////////////////////////////////////////////////////////////////////////////
//    lcd_yaz(" "); komutuyla iki tırnak arasına sadece yazısal ifadeler   /////
// kullanılabilir.Örneğin bu komutla %sabitleri kullanılamaz.              /////
////////////////////////////////////////////////////////////////////////////////
//    printf(lcd_yaz,"  "); komutuyla yazılar hemde değişkenler            /////
// gösterilebilir. %sabitleri kullanılabilir.Ancak \sabitleri kullanılamaz /////
////////////////////////////////////////////////////////////////////////////////
// İstenilen pinler aşağıdaki define tanımlamalarından ayarlannabilir.     /////
// LCD..pin..bağlanılacak pin                                              /////
////////////////////////////////////////////////////////////////////////////////


#define  e   pin_B12
#define  rs  pin_B13
#define  d4  pin_B11
#define  d5  pin_B10
#define  d6  pin_B9
#define  d7  pin_B8

void data_tara(byte dsg)
{
   output_bit(d4,bit_test(dsg,0));
   output_bit(d5,bit_test(dsg,1));
   output_bit(d6,bit_test(dsg,2));
   output_bit(d7,bit_test(dsg,3));
}

////////////////////////////////////////////////////////////////////////////////

void lcd_command(byte m)
{   
   data_tara(m>>4);   
   output_bit(rs,0);       
   delay_cycles(1);         
   output_bit(e,1);       
   delay_cycles(1);      
   output_bit(e,0);
   delay_ms(2);          

   data_tara(m&0x0F); 
   output_bit(rs,0);        
   delay_cycles(1);           
   output_bit(e,1);       
   delay_cycles(1);      
   output_bit(e,0);
   delay_ms(2);          
}

////////////////////////////////////////////////////////////////////////////////

void lcd_yaz(byte k)
{ 
   data_tara(k>>4);   
   output_bit(rs,1);      
   delay_cycles(1);         
   output_bit(e,1);       
   delay_cycles(1);      
   output_bit(e,0);
   delay_ms(2);         

   data_tara(k&0x0F); 
   output_bit(rs,1);      
   delay_cycles(1);         
   output_bit(e,1);      
   delay_cycles(1);     
   output_bit(e,0);
   delay_ms(2);         
}

////////////////////////////////////////////////////////////////////////////////

void adres(byte x, byte y)
{
   if (y==1)     
      lcd_command(0x80|(x-1));

   if (y==2)     
      lcd_command(0x80|(0x40+(x-1)));
   
   if (y==3)
      lcd_command(0x80|(0x14+(x-1)));
      
   if (y==4)
      lcd_command(0x80|(0x54+(x-1)));
      
      
}

////////////////////////////////////////////////////////////////////////////////

void lcd_ayarla()
{
   int j=0;
   output_bit(rs,0); 
   output_bit(e,0);  
   delay_ms(30);   
   
   for(j=0;j<=3;j++)    
   {
      lcd_command(0x03);
      delay_ms(5);      
   }
   
   lcd_command(0x02); 
   lcd_command(0x28); 
   lcd_command(0x08); 
   lcd_command(0x0C); 
   lcd_command(0x06); 
   lcd_command(0x01); 
}

////////////////////////////////////////////////////////////////////////////////

void lcd_sil()
{
   lcd_command(0x01);
}

////////////////////////////////////////////////////////////////////////////////

Registers

C/C++
/*Cihaz ile ilgili kullanılacak sabitler*/
enum sabitler{
      cHWID,
      cSWVersion,
      cHWRev,
      mfc,  //manifacturer
   };
   
   typedef struct hw_strings{
      u8 sbt;
      char hwStr[20];

   }myHwStrings;
   
   
  myHwStrings const deviceData[10]={
      cHWID,{"FD.DGCLK.R01"},
      cSWVersion,{"1.0.1"},
      cHWRev,{"1.0.1"},
      mfc,{"FiDeNet UKoSis"},
   };
   
   
char tempBuffer[30];   // id yazma için kullanılan ara değişken
u16 counterGen;        //genel sayaç
u16 counterGen2;
u8 readDevIDOk=0;
u8 devIDAddr=10;
char devID[20];

u16 counterData;
u16 counterTemp;


u1 permissionToAttend=1;
u1 isReadDevIdValid=0;
const u8 maxRda2Data=250;

char rda2Data[maxRda2Data];
u1 rda2IntActivity;

char strGen1[10];
u16 genCounter;
u8 genCounter2;
float temperature;
u16 backlightVal;




// cmd tanımları
enum rdaCommand{
AT,
ATI,
ATW,
ATC,
ATHW,
ATHWr,
ATSW,
ATr,
};

#define maxMyCmd 20        //dönen modem cevabı için max char size 
#define cmdNr 10   
                    //dönen cevap sayısı
typedef struct myCommand{
      u8 cmd;
      char str[maxMyCmd];

}myCmmnd;

myCmmnd const myCmd[cmdNr]={
   AT,{"AT\r\n"},
   ATI,{"ATI\r\n"},
   ATW,{"ATW:"},
   ATC,{"ATC\r\n"},
   ATHW,{"ATHW\r\n"}, 
   ATHWr,{"ATHWr\r\n"}, 
   ATSW,{"ATSW\r\n"},
   ATr,{"ATr\r\n"},
   
};



typedef struct timeDef{
   u8 sec; 
   u8 min; 
   u8 hrs; 
   u8 day; 
   u8 month; 
   u8 year; 
   u8 dow;
   u8 temp;
   u16 id;
}myTimeDefs;

myTimeDefs myClock;

Credits

Fikret

Fikret

13 projects • 15 followers
fidenetgaraj.blogspot.com

Comments