Version:0.9 StartHTML:0000000105 EndHTML:0000116792 StartFragment:0000001210 EndFragment:0000116776 mikroIDE
#ifndef DS1307
        #define DS1307 0xD0
#endif
 sbit Soft_I2C_Scl           at RB3_bit;
sbit Soft_I2C_Sda           at RB4_bit;
sbit Soft_I2C_Scl_Direction at TRISB3_bit;
sbit Soft_I2C_Sda_Direction at TRISB4_bit;

sbit Set_b at RC4_bit;
sbit Inr_b at RC5_bit;
sbit Dec_b at RC0_bit;
sbit Log_b at RC1_bit;

sbit LCD_RS at RE0_bit;
sbit LCD_EN at RE2_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISE0_bit;
sbit LCD_EN_Direction at TRISE2_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;

char colon[] = ":";

char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
unsigned short myDate,myHour,myMin,myYear,myMon,myDay;
// for some other testing
char txtSec[10];
char txtMin[10];
char txtHour[10];
char txtWeekDay[10];
char txtDay[10];
char txtMn[10];
char txtYear[5];
char txtDisplay[39];
     char GetHexCipher(unsigned short value) {
  switch (value) {
    case  0 : return '0'; break;
    case  1 : return '1'; break;
    case  2 : return '2'; break;
    case  3 : return '3'; break;
    case  4 : return '4'; break;
    case  5 : return '5'; break;
    case  6 : return '6'; break;
    case  7 : return '7'; break;
    case  8 : return '8'; break;
    case  9 : return '9'; break;
    case 10 : return 'A'; break;
    case 11 : return 'B'; break;
    case 12 : return 'C'; break;
    case 13 : return 'D'; break;
    case 14 : return 'E'; break;
    case 15 : return 'F'; break;
  }
}

void WordToHex(unsigned int number, char *output) {     
// for some testing
int intSec, intMin, intHour, intWeekDay, intMn, intDay, intYear;

void Zero_Fill(char *value) {
        if (value[1] == 0) {
                value[1] = value[0];
                value[0] = 48;
                value[2] = 0;
        }
}

void Write_Time() {
        Soft_I2C_Start();          // issue start signal
        Soft_I2C_Write(DS1307);       // address DS1307
        Soft_I2C_Write(0);            // start from word at address (REG0)
        Soft_I2C_Write(0x80);         // write $80 to REG0. (pause counter + 0 sec)
        Soft_I2C_Write(myMin);            // write 0 to minutes word to (REG1)
        Soft_I2C_Write(myHour);         // write 17 to hours word (24-hours mode)(REG2)
        Soft_I2C_Write(myDay);         // write 2 - Monday (REG3)
        Soft_I2C_Write(myDate);         // write 4 to date word (REG4)
        Soft_I2C_Write(myMon);         // write 5 (May) to month word (REG5)
        Soft_I2C_Write(myYear);         // write 01 to year word (REG6)
        Soft_I2C_Stop();           // issue stop signal

        Soft_I2C_Start();          // issue start signal
        Soft_I2C_Write(DS1307);       // address DS1307
        Soft_I2C_Write(0);            // start from word at address 0
        Soft_I2C_Write(0);            // write 0 to REG0 (enable counting + 0 sec)
        Soft_I2C_Stop();           // issue stop signal
}
           // 28 1 14 till here
           //
           //
           //   /
           // //
           //
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        Soft_I2C_Start();
        Soft_I2C_Write(DS1307);
        Soft_I2C_Write(0);
        Soft_I2C_Start();
        Soft_I2C_Write(0xD1);
        *sec =Soft_I2C_Read(1);
        *min =Soft_I2C_Read(1);
        *hr =Soft_I2C_Read(1);
        *week_day =Soft_I2C_Read(1);
        *day =Soft_I2C_Read(1);
        *mn =Soft_I2C_Read(1);
        *year =Soft_I2C_Read(0);
        Soft_I2C_Stop();
}

void Transform_Time(char  *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
        *sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
        *min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
        *hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
        *week_day =(*week_day & 0x07);
        *day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
        *mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
        *year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}

void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
        switch(week_day){
                case 1: txt="Sun"; break;
                case 2: txt="Mon"; break;
                case 3: txt="Tue"; break;
                case 4: txt="Wed"; break;
                case 5: txt="Thu"; break;
                case 6: txt="Fri"; break;
                case 7: txt="Sat"; break;
        }

        Lcd_Init();
        Lcd_Cmd(_LCD_CLEAR);               // Clear display
        Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off



        Lcd_Chr(1, 1, (hr / 10)   + 48);
        Lcd_Chr(1, 2, (hr % 10)   + 48);
        Lcd_Chr(1,3,':')  ;
        Lcd_Chr(1, 4, (min / 10) + 48);
        Lcd_Chr(1,5, (min % 10) + 48);
        Lcd_Chr(1,6,':')  ;
        Lcd_Chr(1,7, (sec / 10) + 48);
        Lcd_Chr(1,8, (sec % 10) + 48);
         //Lcd_Out(1,1,txt);
        Lcd_Chr(1, 10, (day / 10)   + 48);    // Print tens digit of day variable
        Lcd_Chr(1, 11, (day % 10)   + 48);    // Print oness digit of day variable
        Lcd_Chr(1,12,'/')  ;
     //  Lcd_Chr(1,13, (mn / 10) + 48);
        Lcd_Chr(1,13, (mn % 10) + 48);
        Lcd_Chr(1,14,'/')  ;
        Lcd_Chr(1,15,  (year / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
       Lcd_Chr(1,16,  (year % 10)  + 48);
}

void Set_Time()
{
              myDate=0x20;
                myMon=0x03;
                   myYear=0x14;
              myHour=0x12;
              myMin=0x55;
              myDay= 0x05;
              Write_Time();
}
void Set_Time1()
{
            //  Set_Time();/*

unsigned int t=0;
unsigned char s=0;
  Lcd_Cmd(_LCD_CLEAR);               // Clear display            Delay_ms(100);
        Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Date DD?") ;
 t=1;
while(Log_b==1)
{
if ((Inr_b==0) ){ t++;   }
  if ((Dec_b==0)){ t--;}
     switch(t)
     { case 1 :s=0x01; myDate=s;  break;
     case 2 :s=0x02;myDate=s;break;
     case 3 :s=0x03;  myDate=s;  break;

     case 4 :s=0x04;  myDate=s;  break;
     case 5 :s=0x05;  myDate=s;   break;
     case 6 :s=0x06; myDate=s;    break;
     case 7 :s=0x07;  myDate=s;   break;

     case 8 :s=0x08;  myDate=s;  break;
     case 9 :s=0x09;  myDate=s;  break;
     case 10 :s=0x0A; myDate=0x10;    break;
     case 11 :s=0x0B;  myDate=0x11;    break;

     case 12 :s=0x0C;   myDate=0x12;    break;
     case 13 :s=0x0D;   myDate=0x13;  break;
     case 14 :s=0x0E;   myDate=0x14;   break;
     case 15 :s=0x0F; myDate=0x15;  break;

     case 16 :s=0x10;  myDate=0x16; break;
     case 17 :s=0x11; myDate=0x17;   break;
     case 18 :s=0x12; myDate=0x18; break;
     case 19 : s=0x13 ;  myDate=0x19;    break;

     case 20:s=0x14; myDate=0x20;  break;
     case 21 :s=0x15; myDate=0x21; break;
     case 22 :s=0x16; myDate=0x22; break;
     case 23 :s=0x17; myDate=0x23; break;

     case 24 :s=0x18; myDate=0x24;  break;
     case 25 :s=0x19;  myDate=0x25;  break;
     case 26 :s=0x1A;myDate=0x26;break;
     case 27 :s=0x1B; myDate=0x27; break;

     case 28 :s=0x1C;myDate=0x28;break;
     case 29 :s=0x1D;myDate=0x29;break;
     case 30 :s=0x1E;myDate=0x30;break;
     case 31 :s=0x1F; myDate=0x31; break;
     default :t=1;

     }

      Lcd_Chr(2,1,(((s)/10 )+ 48))    ;
        Lcd_Chr(2,2,(((s)%10) + 48) )   ;
         Delay_ms(100);
}

  Lcd_Cmd(_LCD_CLEAR);            Delay_ms(100);    // Clear display
        Lcd_Cmd(_LCD_CURSOR_OFF);
t=1;            Lcd_Out(1,1,"Month MM?") ;
  while(Log_b==1)
{
if ((Inr_b==0) ){ t++;   }
  if ((Dec_b==0)){ t--;}
     switch(t)
     {
     case 1 :s=0x01; myMon=s;  break;
     case 2 :s=0x02;myMon=s;break;
     case 3 :s=0x03;  myMon=s;  break;

     case 4 :s=0x04;  myMon=s;  break;
     case 5 :s=0x05;  myMon=s;   break;
     case 6 :s=0x06; myMon=s;    break;
     case 7 :s=0x07;  myMon=s;   break;

     case 8 :s=0x08;  myMon=s;  break;
     case 9 :s=0x09;  myMon=s;  break;
     case 10 :s=0x0A; myMon=0x10;    break;
     case 11 :s=0x0B;  myMon=0x11;    break;

     case 12 :s=0x0C;   myDate=0x12;    break;

     default :t=1;

     }

      Lcd_Chr(2,1,(((s)/10 )+ 48))    ;
        Lcd_Chr(2,2,(((s)%10) + 48) )   ;
         Delay_ms(100);
}





      myHour=0x01;
              myMin=0x00;
              myYear=0x14;
            //  myMon=0x02;
              Write_Time();                  //  */
}
//void GetLast2Numers

void Display_Time_Direct(char sec, char min, char hr, char week_day, char day, char mn, char year) {
        intSec = sec;
        intMin = min;
        intHour = hr;
        intWeekDay = week_day;
        intDay = day;
        intMn = mn;
        intYear = year;

        WordToHex(intSec, txtSec);
        WordToHex(intMin, txtMin);
        WordToHex(intHour, txtHour);
        WordToHex(intWeekDay, txtWeekDay);
        WordToHex(intDay, txtDay);
        WordToHex(intMn, txtMn);
        WordToHex(intYear, txtYear);

        strcpy(txtDisplay, "");

        strcat(txtDisplay, "Time: ");
        strcat(txtDisplay, txtYear);
        strcat(txtDisplay, "/");
        strcat(txtDisplay, txtMn);
        strcat(txtDisplay, "/");
        strcat(txtDisplay, txtDay);
        strcat(txtDisplay, " ");
        strcat(txtDisplay, txtHour);
        strcat(txtDisplay, ":");
        strcat(txtDisplay, txtMin);
        strcat(txtDisplay, ":");
        strcat(txtDisplay, txtSec);

        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1,1,txtDisplay);
}

void Init_Main() {     

void Init_Main_Simple() {
     ADCON1 = 0x0F; // page 268, disable analaog
     CMCON = 0x07;
        INTCON2 = 0x80; // disable pull up in port b

        // clears internal latches
       //LATB = 0x03; // enable internal pull ups
        LATA = 0x00;
        LATC = 0x00;
       //LATD = 0x00;
        LATE = 0x00;

        // Make all outputs
        TRISA = 0x00;
       TRISB = 0x00;
        TRISC = 0x00;
       TRISD = 0x00;
       TRISE = 0x00;
              PORTC=0x33;
              TRISC=0x33;

        Lcd_Init();                // Initialize LCD
        Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
        Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off
                  // Lcd_Out(1,1,"Timfdgfhfe");
        Soft_I2C_Init();                        // initialize I2C
        Lcd_Out(1,1,"Time software");
}



void main() {
      Init_Main_Simple();

      Lcd_Out(1,1,"Standalone Data");Lcd_Out(2,1,"Logger BE ETRX") ;   Delay_ms(500);
             Lcd_Cmd(_LCD_CLEAR);               // Clear display
        Lcd_Cmd(_LCD_CURSOR_OFF);
        Lcd_Out(1,1,"Select set or"); Lcd_Out(2,1,"log");
        //      Init_Main();
      //  Write_Time();
                        Set_Time();
    /*  while(1)
      {
      if(Set_b == 0)
       { Set_Time1(); break;}
        else      if(Log_b == 0)
            { Set_Time();break  ;  }


        }      Delay_ms(100);                 */


        while (1) {
                Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);      // read time from RTC(DS1307)
                Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
                //Display_Time_Direct(sec, min1, hr, week_day, day, mn, year);    // prepare and display on LCD
                Display_Time(sec, min1, hr, week_day, day, mn, year);


        }
}