RTC PCF 8583 error time with PIC 18f452

General discussion on mikroC.
Post Reply
Author
Message
Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

RTC PCF 8583 error time with PIC 18f452

#1 Post by Arouca » 21 Jul 2010 00:46

Hi.
I'm trying to use a Pic18f452 with an RTC PCF8583 (using I2C library) and an LCD.
The idea is to use several ADC pins and display the time and ADC value on the LCD.
Everithing seem when I use only one ADC pin. I can see the data and time on my LCD and I see the correct analog value from the ADC pin.
The problem appears when I try to use more than one ADC pin. The LCD simply shows the time value as 65:65:65.
It seems that when i try to put one more variable in the code, the RTC (or LCD) simply gets wrong data and don't display the right time values.
There is a maximum number of variables allowed for the pic???? I think that doen't make sense, but if i take off the last variable the board simply gets to work fine again.

blips
Posts: 30
Joined: 11 Nov 2007 22:28

Re: RTC PCF 8583 error time with PIC 18f452

#2 Post by blips » 21 Jul 2010 10:24

Its hard to help you without some code, but perhaps you are putting the 10 bit variable into a 8 bit variable causing overflow. You can disregard the 2 least significant bits by shifting

Code: Select all

foo = Adc_read(0) << 2; // foo will be of value 0 to 255

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#3 Post by Arouca » 21 Jul 2010 11:59

Thank you blips, here goes some code:

If i use like this, everithing is working:

Code: Select all

char *texto1 = "Distanci1";
char *texto2 = "Distanci2";
char txt1[6];
int value_channel_1, value_channel_2, contador,teste;
int num;
char filename[14] = "TESTE00xTXT"; // File names
char a_la_ligne[]={0x0D,0x0A};
char tab[]={0x09};
unsigned char sec, mnt, hr, day, mn, year , year_eeprom;
unsigned char sec_bcd , mnt_bcd , hr_bcd , day_bcd ,mn_bcd , year_bcd , year_eeprom_bcd ,year_eeprom_bits ;
char *txt, tnum[4];
short flag_display_time;
long loop =0;
int i; 

void dormir ()
     {

     WDTCON.SWDTEN=1; //Liga o WDT

     for (i = 0; i < 110; i++){
     

         delay_ms(250);
       PORTD.F0=1;
      delay_ms(250);
      PORTD.F0=0;
      delay_ms(250);
      
      
        asm{clrwdt}; // se usar capslock da erro na compilação é aconselhavel o CLRWDT antes da instrucção SLEEP
                      //o CLRWDT poe o WDT a zeros
        asm{sleep};  //ENTRA E M SLEEP ainda nao foi implementado na parte sem LCD
                      //  //Tenho de dizer que é um comando de assembly. Daí o asm{}
                      }
      WDTCON.SWDTEN=0; //Desliga o WDT   senao ddepois de acordar faz resete
      }

void display_lcd_Bcd (unsigned char bb)
     {  Lcd_Chr_Cp(((bb>>4)& 0x0F)+48);
        Lcd_Chr_Cp((bb&0x0F)+48);
      }

char *R_Trim(char *str1){
  while (*str1 == ' ')
    str1++;
  return str1;
}//~

void Zero_Fill(char *value) {      // fill text repesentation
  if (value[1] == 0) {            //      with leading zero
    value[1] = value[0];
    value[0] = 48;
    value[2] = 0;
  }
}//~

//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time(char *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
  // Soft_I2c_Config(&PORTB,2,1);
  Soft_I2C_Start();
  Soft_I2C_Write(0xA0);
  Soft_I2C_Write(2);
  Soft_I2C_Start();
  Soft_I2C_Write(0xA1);
  *sec = Soft_I2C_Read(1);
  *mnt = Soft_I2C_Read(1);
  *hr = Soft_I2C_Read(1);
  *day = Soft_I2C_Read(1);
  *mn = Soft_I2C_Read(0);
  Soft_I2C_Stop();
}//~
//-------------------- Formats date and time
void Transform_Time(char  *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
  *sec  =  ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);
  *mnt  =  ((*mnt & 0xF0) >> 4)*10 + (*mnt & 0x0F);
  *hr   =  ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F);
  *year =  (*day & 0xC0) >> 6;
  *day  =  ((*day & 0x30) >> 4)*10 + (*day & 0x0F);
  *mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);

}//~
//-------------------- Output values to LCD
void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
  char *tc;
   ByteToStr(day, tnum);
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   LCD_Out(1,7, tc);
   ByteToStr(mn, tnum);
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   LCD_Out(1,10, tc);
   year_eeprom=Eeprom_read(0);
   year_eeprom_bits=year_eeprom &  3 ;
   if (year != year_eeprom_bits){year_eeprom++ ; Eeprom_write(0,year_eeprom);}
   year_eeprom_bcd=Dec2Bcd(year_eeprom);
   Lcd_Cmd(128+14);
   display_lcd_bcd(year_eeprom_bcd);
   ByteToStr(hr,tnum);
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   LCD_Out(2,7,tc);
   ByteToStr(mnt,tnum);
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   LCD_Out(2,10,tc);
   ByteToStr(sec,tnum);
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   LCD_Out(2,13,tc);
   Lcd_Chr(2,15,' ');
   flag_display_time=1;


}//~

void convers_char_to_write_to_mmc(char m)
    {   ByteToStr(m,tnum);
        tnum[0]=tnum[1];
        tnum[1]=tnum[2];
        if (tnum[0]==' ') {tnum[0]='0';}
        if (tnum[1]==' ') {tnum[1]='0';}
        Mmc_Fat_Write(tnum,2) ;
    }

void write_time_and_day_on_mmc_file()
      {  Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
         transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
            convers_char_to_write_to_mmc(hr);
            Mmc_Fat_Write(":",1);
            convers_char_to_write_to_mmc(mnt);
            Mmc_Fat_Write(":",1);
            convers_char_to_write_to_mmc(sec);
            Mmc_Fat_Write(" ",1);
            Mmc_Fat_Write(a_la_ligne,2);
           }
//-------------------- display time/ data to Lcd ---------------------------------------
void see_time()
    {
         txt = "DATE: ";
         LCD_Out(1,1,txt);
         Lcd_Chr(1,9,':');
         Lcd_Chr(1,12,':');
         txt = "TIME: ";
         LCD_Out(2,1,txt);
         Lcd_Chr(2,9,':');
         Lcd_Chr(2,12,':');
         Lcd_Chr(2,15,' ');
         txt = "20";
         LCD_Out(1,13,txt);
         Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
         Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
         Display_Time(sec, mnt, hr, day, mn, year);
    }
void see_time2()
    {

         Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
         Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);

    }





// WRITE ADC VALUE TO THE MMC/SD

void write_adc_value_on_mmc(int m)     //m = valeur de l'echantillon
  { intToStr(m, txt1);
    //Mmc_Fat_Write("  ",2);
    Mmc_Fat_Write(&txt1[0],1);
    Mmc_Fat_Write(&txt1[1],1);
    Mmc_Fat_Write(&txt1[2],1);
    Mmc_Fat_Write(&txt1[3],1);
    Mmc_Fat_Write(&txt1[4],1);
    Mmc_Fat_Write(&txt1[5],1);
    Mmc_Fat_Write(tab,1);   //To have a TAB, i.e., to make a new column
 //    Mmc_Fat_Write(a_la_ligne,2); //To change the line
    }

void select_file()
   {

   Lcd_Out(2,1," NEW FILE...   ");
   loop=1001;
      while (loop<1000000)
            {LongToStr(loop, filename);
            filename[8]='T';filename[9]='X';filename[10]='T';
            if (!Mmc_Fat_Assign(filename, 1)) {
                                               Lcd_Chr(2,1,filename[0]);
                                               Lcd_Chr_CP(filename[1]);
                                               Lcd_Chr_CP(filename[2]);
                                               Lcd_Chr_CP(filename[3]);
                                               Lcd_Chr_CP(filename[4]);
                                               Lcd_Chr_CP(filename[5]);
                                               Lcd_Chr_CP(filename[6]);
                                               Lcd_Chr_CP(filename[7]);
                                               Lcd_Out_CP(".TXT");
                                               Mmc_Fat_Assign(&filename, 0xA0);
                                               mmc_Fat_Rewrite();
                                               Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
                                               Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
                                               year=Eeprom_read(0)+2000;
                                               mmc_Fat_Set_File_Date( year,mn,day,hr,mnt,sec);
                                               Mmc_Fat_Write(a_la_ligne,2);
                                               loop=1000000;
                                              }
            loop=loop+1001;
            }
        }


void select_file2()
   {

//   Lcd_Out(2,1," NEW FILE...   ");
   loop=1001;
      while (loop<1000000)
            {LongToStr(loop, filename);
            filename[8]='T';filename[9]='X';filename[10]='T';
            if (!Mmc_Fat_Assign(filename, 1)) {
  //
                                               Mmc_Fat_Assign(&filename, 0xA0);
                                               mmc_Fat_Rewrite();
                                               Read_Time(&sec,&mnt,&hr,&day,&mn,&year);
                                               Transform_Time(&sec,&mnt,&hr,&day,&mn,&year);
                                               year=Eeprom_read(0)+2000;
                                               mmc_Fat_Set_File_Date( year,mn,day,hr,mnt,sec);
                                               Mmc_Fat_Write(a_la_ligne,2);
                                               loop=1000000;
                                              }
            loop=loop+1001;
            }
        }







 //----------------INIT FILE

void init_file()
  {  Lcd_Out(1,1,"INIT MEMORY ");
  //--- init the FAT library
     Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
     // use fat16 quick format instead of init routine if a formatting is needed
       if (!Mmc_Fat_Init(&PORTC, 2)) {
       // reinitialize spi at higher speed
     Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
                                     Lcd_Out_Cp("OK");
                                     select_file();
                                     //M_Create_New_File();

                                   }
      //Mmc_Fat_Init Returns 0 if initialization is successful, 1 if boot sector was not found and 255 if card was not detected.
        else {Lcd_Out_Cp("FAIL");
              Lcd_Out(2,1,"SYSTEM STOPPED");
              while(1);}
        }
        
  void init_file2()
  {
  //--- init the FAT library
     Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
     // use fat16 quick format instead of init routine if a formatting is needed
       if (!Mmc_Fat_Init(&PORTC, 2)) {
       // reinitialize spi at higher speed
     Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);

                                     select_file2();
                                     //M_Create_New_File();

                                   }
      //Mmc_Fat_Init Returns 0 if initialization is successful, 1 if boot sector was not found and 255 if card was not detected.
        }
//-------------------- Setup time/Date RTC------
void setup_RTC()
   {      Lcd_Out(1,1,"SETUP TIME/DATE:");
          delay_ms(1000);
          Lcd_Cmd(Lcd_Clear);
          Lcd_Out(1,1,"DATE:");
          Lcd_Out(2,1,"TIME:");
          hr=12;
          mnt=0;
          sec=0;
          year=7;
          mn=1;
          day=1;
          hr_bcd=Dec2Bcd(hr);
          mnt_bcd=Dec2Bcd(mnt);
          sec_bcd=Dec2Bcd(sec);
          year_bcd=Dec2Bcd(year);
          mn_bcd=Dec2Bcd(mn);
          day_bcd=Dec2Bcd(day);
          Lcd_CMD(192+7);
          display_lcd_Bcd(hr_bcd);
          Lcd_Chr_Cp(':');
          display_lcd_Bcd(mnt_bcd);
          Lcd_Chr_Cp(':');
          display_lcd_Bcd(sec_bcd);
          Lcd_CMD(128+7);
          display_lcd_Bcd(day_bcd);
          Lcd_Chr_Cp('/');
          display_lcd_Bcd(mn_bcd);
          Lcd_Chr_Cp('/');
          display_lcd_Bcd(year_bcd);
          while   (Button(&PORTB,7,1,0)==255);
          while   (Button(&PORTB,7,1,0)==0)   {
                                               if  (Button(&PORTB,6,1,0)==255) {day++ ; if (day==32) {day=1;}}
                                               if  (Button(&PORTB,5,1,0)==255) {day-- ; if (day==255) {day=31;}}
                                                 Delay_ms(200);
                                                 day_bcd=Dec2Bcd(day);
                                                 Lcd_CMD(128+7);
                                                 display_lcd_Bcd(day_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(mn_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(year_bcd);
                                             }
          while   (Button(&PORTB,7,1,0)==255);
          while   (Button(&PORTB,7,1,0)==0)   {
                                               if  (Button(&PORTB,6,1,0)==255) {mn++ ; if (mn==13) {mn=1;}}
                                               if  (Button(&PORTB,5,1,0)==255) {mn-- ; if (mn==255) {mn=12;}}
                                                 Delay_ms(200);
                                                 mn_bcd=Dec2Bcd(mn);
                                                 Lcd_CMD(128+7);
                                                 display_lcd_Bcd(day_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(mn_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(year_bcd);
                                             }
          while   (Button(&PORTB,7,1,0)==255);
          while   (Button(&PORTB,7,1,0)==0)   {
                                               if  (Button(&PORTB,6,1,0)==255) {year++ ; if (year==51) {year=0;}}
                                               if  (Button(&PORTB,5,1,0)==255) {year-- ; if (year==255) {year=50;}}
                                                  Delay_ms(200);
                                                 year_bcd=Dec2Bcd(year);
                                                 Lcd_CMD(128+7);
                                                 display_lcd_Bcd(day_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(mn_bcd);
                                                 Lcd_Chr_Cp('/');
                                                 display_lcd_Bcd(year_bcd);
                                              }
         Eeprom_Write(0,year);
         if (year.F0==1) {day_bcd.F6=1;}
         if (year.F1==1) {day_bcd.F7=1;}
         while   (Button(&PORTB,7,1,0)==255);
         while   (Button(&PORTB,7,1,0)==0)  {
                                               if  (Button(&PORTB,6,1,0)==255) {hr++ ; if (hr==24) {hr=0;}}
                                               if  (Button(&PORTB,5,1,0)==255) {hr-- ; if (hr==255) {hr=23;}}
                                                Delay_ms(200);
                                               Lcd_CMD(192+7);
                                               hr_bcd=Dec2Bcd(hr);
                                               display_lcd_Bcd(hr_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(mnt_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(sec_bcd);
                                             }
         while   (Button(&PORTB,7,1,0)==255);
         while   (Button(&PORTB,7,1,0)==0)  {
                                               if  (Button(&PORTB,6,1,0)==255) {mnt++ ; if (mnt==60) {mnt=0;}}
                                               if  (Button(&PORTB,5,1,0)==255) {mnt-- ; if (mnt==255) {mnt=59;}}
                                                Delay_ms(200);
                                               Lcd_CMD(192+7);
                                               mnt_bcd=Dec2Bcd(mnt);
                                               display_lcd_Bcd(hr_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(mnt_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(sec_bcd);
                                             }


          while   (Button(&PORTB,7,1,0)==255);
          while   (Button(&PORTB,7,1,0)==0)   {
                                               if  (Button(&PORTB,6,1,0)==255) {sec++ ; if (sec==60) {sec=0;}}
                                               if  (Button(&PORTB,5,1,0)==255) {sec-- ; if (sec==255) {sec=59;}}
                                                Delay_ms(200);
                                               Lcd_CMD(192+7);
                                               sec_bcd=Dec2Bcd(sec);
                                               display_lcd_Bcd(hr_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(mnt_bcd);
                                               Lcd_Chr_Cp(':');
                                               display_lcd_Bcd(sec_bcd);
                                             }

   Soft_I2C_Start();
   Soft_I2C_Write(0xA0);
   Soft_I2C_Write(0);
   Soft_I2C_Write(0x80);
   Soft_I2C_Write(0);
   Soft_I2C_Write(sec_bcd);
   Soft_I2C_Write(mnt_bcd);
   Soft_I2C_Write(hr_bcd);
   Soft_I2C_Write(day_bcd);
   Soft_I2C_Write(mn_bcd);
   Soft_I2C_Stop();
   Soft_I2C_Start();
   soft_I2C_Write(0xA0);
   Soft_I2C_Write(0);
   Soft_I2C_Write(0);
   Soft_I2C_Stop();
      }


void main() {


TRISB=255;
Soft_I2c_Config(&PORTB,2,1);
Delay_ms(500);

teste=PORTB,4;  //PORTB 4 é onde estão os transistores (switch) para saber se o LCD está conectado

if  (teste>100) {




ADCON1 = 8; //para que Vref+(se quisermos por o adc de 0->3,3V) e Vref- estejam configurados no pin 4 e 5
TRISA  = 0xFF;  //PORT A is an input
PORTC = 0;
TRISC = 0b11010011;
Delay_ms(100);
Lcd_Init(&PORTD);         // Initialize LCD connected to PORTD
Delay_ms(100);
Lcd_Cmd(Lcd_CLEAR);       // Clear display
Delay_ms(200);
Lcd_Cmd(Lcd_CURSOR_OFF);  // Turn cursor off
num=0;
TRISB.F0=1;
TRISB.F3=1;




if  (Button(&PORTB,7,1,0)==255) //CONDICAO COM LCD
 {setup_rtc();}
Delay_ms(1000);
Lcd_Cmd(Lcd_Clear);
init_file();
Lcd_Cmd(Lcd_Clear);
flag_display_time=0;
TRISB=255;

while (num < 1) {
      see_time();
      delay_ms(500);
  
      value_channel_1=Adc_read(0);     //get results for AD convertion
      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto1);
      LCD_CMD(208);  // segunda linha
      IntToStr(value_channel_1, txt1);
       LCD_Chr_CP(txt1[0]);
       LCD_Chr_CP(txt1[1]);
       LCD_Chr_CP(txt1[2]);
       LCD_Chr_CP(txt1[3]);
       LCD_Chr_CP(txt1[4]);
       LCD_Chr_CP(txt1[5]);
       Delay_ms(500);
      
       value_channel_2=Adc_read(1);     //get results for AD convertion
      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto2);
      LCD_CMD(208);  // segunda linha
      IntToStr(value_channel_2, txt1);
       LCD_Chr_CP(txt1[0]);
       LCD_Chr_CP(txt1[1]);
       LCD_Chr_CP(txt1[2]);
       LCD_Chr_CP(txt1[3]);
       LCD_Chr_CP(txt1[4]);
       LCD_Chr_CP(txt1[5]);
       Delay_ms(200);
       write_adc_value_on_mmc(value_channel_1);     //m = valeur de l'echantillon
       write_adc_value_on_mmc(value_channel_2);
       write_time_and_day_on_mmc_file();
  
     delay_ms(500);
     PORTD.F0=1;
      delay_ms(1000);
      PORTD.F0=0;
      delay_ms(500);
     }
    }
  }
BUT, as soon as i put the new variavles (code added in red) the display time comes wrong:

Code: Select all

char *texto1 = "Distanci1";
char *texto2 = "Distanci2";
[color=#FF0000]char *texto3 = "Distanci3";
char *texto4 = "Distanci4";
char *texto5 = "Distanci5";[/color]
//char *emsleep = "SLEEPING";
char txt1[6];
//char *txt2;
int value_channel_1, value_channel_2, contador,teste;
int num;
//novas variaveis
char filename[14] = "TESTE00xTXT"; // File names
char a_la_ligne[]={0x0D,0x0A};
char tab[]={0x09};
unsigned char sec, mnt, hr, day, mn, year , year_eeprom;
unsigned char sec_bcd , mnt_bcd , hr_bcd , day_bcd ,mn_bcd , year_bcd , year_eeprom_bcd ,year_eeprom_bits ;
char *txt, tnum[4];
short flag_display_time;
long loop =0;
int i;  //para o ciclo do slepp

[color=#80FF00](NO CHANGES IN THE FUNCTIONS)[/color]

void main() {


TRISB=255;
Soft_I2c_Config(&PORTB,2,1);
Delay_ms(500);

teste=PORTB,4;  //PORTB 4 é onde estão os transistores (switch) para saber se o LCD está conectado

if  (teste>100) {




ADCON1 = 8; //para que Vref+(se quisermos por o adc de 0->3,3V) e Vref- estejam configurados no pin 4 e 5
TRISA  = 0xFF;  //PORT A is an input
PORTC = 0;
TRISC = 0b11010011;
Delay_ms(100);
Lcd_Init(&PORTD);         // Initialize LCD connected to PORTD
Delay_ms(100);
Lcd_Cmd(Lcd_CLEAR);       // Clear display
Delay_ms(200);
Lcd_Cmd(Lcd_CURSOR_OFF);  // Turn cursor off
num=0;
TRISB.F0=1;
TRISB.F3=1;




if  (Button(&PORTB,7,1,0)==255) //CONDICAO COM LCD
 {setup_rtc();}
Delay_ms(1000);
Lcd_Cmd(Lcd_Clear);
init_file();
Lcd_Cmd(Lcd_Clear);
flag_display_time=0;
TRISB=255;

while (num < 1) {
      see_time();
      delay_ms(500);
      
      value_channel_1=Adc_read(0);     //get results for AD convertion
      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto1);
      LCD_CMD(208);  // segunda linha
      IntToStr(value_channel_1, txt1);
       LCD_Chr_CP(txt1[0]);
       LCD_Chr_CP(txt1[1]);
       LCD_Chr_CP(txt1[2]);
       LCD_Chr_CP(txt1[3]);
       LCD_Chr_CP(txt1[4]);
       LCD_Chr_CP(txt1[5]);
       Delay_ms(500);
       
       
       value_channel_2=Adc_read(1);     //get results for AD convertion

      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto2);
      LCD_CMD(208);  // segunda linha
      IntToStr(value_channel_2, txt1);
       LCD_Chr_CP(txt1[0]);
       LCD_Chr_CP(txt1[1]);
       LCD_Chr_CP(txt1[2]);
       LCD_Chr_CP(txt1[3]);
       LCD_Chr_CP(txt1[4]);
       LCD_Chr_CP(txt1[5]);
       Delay_ms(200);
       [color=#FF0000]  LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto3);
      Delay_ms(200);
      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto4);
      Delay_ms(200);
      LCD_CMD(144); // Print text to LCD, 2nd row, 1st column
      Lcd_Out_Cp(texto5);
      Delay_ms(200);[/color]
       write_adc_value_on_mmc(value_channel_1);     //m = valeur de l'echantillon
       write_adc_value_on_mmc(value_channel_2);
       write_time_and_day_on_mmc_file();
     delay_ms(500);
     PORTD.F0=1;
      delay_ms(1000);
      PORTD.F0=0;
      delay_ms(500);
    }
    }
  }
I simple cannot understand where is the error. As soon as i delete the new variables, everithing start to work again...

Edited by Administrator: Added code Tag!

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: RTC PCF 8583 error time with PIC 18f452

#4 Post by tihomir.losic » 21 Jul 2010 16:39

Hello,

please, try to use our examples for RTC and ADC, which I attached with this reply
(projects are adapted for PIC18F452), and combine it in order to make desired project.

I hope it will help to you.

Best regards,

Losic Tihomir
Attachments
RTC.rar
(53.5 KiB) Downloaded 220 times
ADC_on_LCD.rar
(38.14 KiB) Downloaded 156 times
mikroElektronika [Support team]

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#5 Post by Arouca » 21 Jul 2010 18:58

Dear Tihomir:

Thank you very much for you help.

Tomorrow i will try to do that.

I will say something as soon as i try..

Thank you

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#6 Post by Arouca » 31 Jul 2010 00:00

Dear tihomir:
I have checked the files you sent. your suggestion is (if i'm not wrog) to use the I2C communication library by connecing the PCF8583 directly to the PORTC (RC3 and RC4)... Right? Unfurtunatelly i cannot do that, because i'm using PORTC to control a SD/MMC card. So i only can control the RTC by the Soft_I2C library.

The problem is always the same. As soon as i declare a new variable the hour in the LCD display look like this:
65:65:65
The same behaviour appeans if i give the instruction to write the time on the SD/MMC card..

Do you know what could be appening???? Looks like a limitation on the number of variables (but for me this explanation looks stupid)

Regards

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: RTC PCF 8583 error time with PIC 18f452

#7 Post by Sobrietytest » 31 Jul 2010 01:56

Hi Arouca, I wrote a program very similar to yours and it works very well. The main difference was that I declared all the variables as their correct size rather than using pointers. The advantage is that the compiler will tell you if you have exceeded the memory space before you start.

Pointers are a great facility in the C language but they are not so good for embedded programming because you have limited memory - I never use pointers with PIC's and I take great care that values do not exceed the space allowed by the variable, if you use pointers it means that you have not taken control of your variable sizes and the program is slopping around using uncontrolled amounts of the memory stack.

I'm guessing that this is the problem with your code; when the program is running it is probably dynamically allocating variables into a memory space that is too small. If you use fixed variables this cannot happen.

This may mean you have to specify a PIC with more memory. When I specify a PIC I choose one that has all the necessary peripherals and then find the version that has the largest memory - not least because my clients always want more features added later!

ST

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#8 Post by Arouca » 01 Aug 2010 16:54

Dear Sobrietytest.
Thank you for your suggestion. It makes sense...
I will try know to use no more pointers and use the fixed variables.
It will be the first time so i have to read a little bit.
Any help about how to change the code will be great.

Thank you very much

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#9 Post by Arouca » 10 Aug 2010 23:00

Dear all, I'm back again.
As i'm not a programing expertise, i simple didn't manage to apply the Sobrietytest suggestions (declared all the variables as their correct size rather than using pointers).
Could someone give me an example of out to do it?

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: RTC PCF 8583 error time with PIC 18f452

#10 Post by Sobrietytest » 11 Aug 2010 11:52

Arouca, I have included a .c file for an inclinometer/time logger which I designed a couple of years ago, the logger measures angle and records it on an SD card along with a time stamp. The code works in concert with a Windows interface which I wrote for purpose, the PIC sends a menu to the PC which directs the user to each particular function. The logging interval can be preset by the user and the RTC is programmed from the PC clock by clicking a 'time set' button on the Windows software.

The code was written in MikroC for dsPIC v4.0, therefore some of the functions may seem unfamiliar or obsolete and SPI channel 2 is running in 16-bit mode.

You can see that the entire program has been written without pointers; almost all of the variables are declared in the header and there are a few private variables within the functions. On the specified PIC the code only uses 17% of RAM and 9% of ROM, which proves that the use of pointers to save memory isn't that important with embedded technology.

***PLEASE NOTE THAT THE ENCLOSED FILE IS SUBJECT TO INTERNATIONAL COPYRIGHT. YOU MAY NOT COPY OR RE-USE ANY PART OF THE CODE WITHOUT EXPRESSED PERMISSION FROM THE AUTHOR. THE FILE IS PROVIDED FOR DEMONSTRATION/EDUCATIONAL PURPOSES ONLY***
Attachments
Inclinometer360_128.zip
(4.94 KiB) Downloaded 169 times

Arouca
Posts: 7
Joined: 21 Jul 2010 00:31

Re: RTC PCF 8583 error time with PIC 18f452

#11 Post by Arouca » 11 Aug 2010 14:35

Dear Sobrietytest, thank you again and again.
You are being very gently.
As soon as i make the changes i will say something.
Thank you

Post Reply

Return to “mikroC General”