CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

eeprom 18f252

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

eeprom 18f252
PostPosted: Fri Jun 08, 2012 10:55 am     Reply with quote

hello, I'm going a robot who saves his career.

I was in a foul or write eeprom read here?

I am willing to put the full code but it is long.

thank you in advance
Code:
void fonction_memory() {

   for(i=0;i<=255;++i)
   {
     variable=donnee[i];
    write_eeprom(i, variable2);
    donnee[i]=donnee[i]>>8;
     variable=donnee[i];
    i=i+1;
     write_eeprom(i, variable); 
   }
}

void fonction_repeat() {

   for(i=0;i<=255;++i)
   {
     variable=read_eeprom(i);
     donnee[i]=variable;
     i=i+1;
    variable2=read_eeprom(i);
    variable2=variable2<<8;
     donnee[i]=donnee[i]||variable2; 
   }
}






PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 08, 2012 6:36 pm     Reply with quote

I don't know exactly what you are trying to do. But I think you want to
write 16-bit values to internal eeprom.

Look at this driver file, included with the CCS compiler:
Quote:

c:\program files\picc\drivers\internal_eeprom.c

It has functions to read and write a 16-bit unsigned integer to eeprom:
Code:

write_int16_eeprom()
read_int16_eeprom()
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sat Jun 09, 2012 1:04 am     Reply with quote

One little comment on the loops. If 'i' is an in8 (default 'int' size), then the loops will never terminate.
When an int8 counts, it wraps at the end. So:

254
255
0
1
2

etc..
This will always be <=255......

Best Wishes
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Sat Jun 09, 2012 2:47 am     Reply with quote

yes I try to put 16 bits in the eeprom. I put the complete code. This is a remote controlled robot using infrared (RC5).
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Thu Jun 14, 2012 1:53 pm     Reply with quote

hello I can not exit the loop, how to proceed, thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 14, 2012 1:59 pm     Reply with quote

Quote:
hello I can not exit the loop, how to proceed, thank you


Ttelmah told you:
Quote:
If 'i' is an in8 (default 'int' size), then the loops will never terminate.


So, what is the next larger data type than 'int8' ? What is the next size ?
Look in the CCS manual and answer that question. Then change your
declaration of 'i' to the next larger size.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look in this section:
Quote:
Basic and Special types
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Jun 14, 2012 2:27 pm     Reply with quote

this was funny....
_________________
CCS PCM 5.078 & CCS PCH 5.093
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Fri Jun 15, 2012 7:51 am     Reply with quote

[quote="PCM programmer"][quote]
Ttelmah told you:
Quote:
If 'i' is an in8 (default 'int' size), then the loops will never terminate.


yes I know, I did see his message but 'i' is long! So I do not see or come to the problem.

watching:

int mot_commande[] = {0, 0, 0, 0};
long donnee[128], i=0;
char variable;
long variable2;
int mot_effectue = 10;
int vitesse = 5;
long time=0;


thank
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Jun 15, 2012 8:19 am     Reply with quote

Edit**nevermind...

try printing out the value of "i" to serial... see what its doing.
_________________
CCS PCM 5.078 & CCS PCH 5.093
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Wed Jun 20, 2012 7:04 am     Reply with quote

hello
Sorry I can not display it.
Yet I have always not solved my problem.
I have two dc motors that I control via infrared (RC5) I have two push buttons, one to store, the other to play again. I stored the data length and direction in the eeprom but I can not do it. if someone can help me as impossible.
Code:

int mot_commande[] = {0, 0, 0, 0};                  // {MOT_D_AV, MOT_D_AR, MOT_G_AV, MOT_G_AR} [0, 10]
long donnee[128], i=0;
char variable;
long variable2;
int mot_effectue = 10;                           // [0, 10]
int vitesse = 5;
long time=0;

void fonction_memory() {

   for(i=0;i<=255;++i)
   {
     variable=donnee[i];
    write_eeprom(i, variable2);
    donnee[i]=donnee[i]>>8;
     variable=donnee[i];
    i=i+1;
     write_eeprom(i, variable); 
   }
}

void fonction_repeat() {

   for(i=0;i<=255;++i)
   {
     variable=read_eeprom(i);
     donnee[i]=variable;
     i=i+1;
    variable2=read_eeprom(i);
    variable2=variable2<<8;
     donnee[i]=donnee[i]||variable2; 
   }
}

void fonction_decode() {

   for(i=0;i<=255;++i)
   {
    variable=donnee[i];
     variable=variable&&7;

       switch(variable) {
            case 1:   
             
              mot_avancer();
               delay_ms(500);
               break;
            case 2:
               
               mot_reculer();
               delay_ms(500);
               break;
            case 3:
               mot_arreter();
               delay_ms(500);
               break;
            case 4:
               
               mot_rotation_gauche();
               delay_ms(500);
               break;
            case 5:
               
               mot_rotation_droite();
               delay_ms(500);
               break;
            case 6:
             
               mot_virage_gauche();
               delay_ms(500);
               break;
            case 7:
             
               mot_virage_droite();
               delay_ms(500);
               break;
         
            default:
               break;
         }

         variable2=donnee[i];
         variable2=variable2>>3;
         time=0;
         while(time!=variable2);
     
   }
}
//////PROGRAMMME

  while (input(REC_IR) == 1) // Attend bit de START
     {
      if(input(PIN_C7))
         {
            fonction_memory();
         output_high(LED_VERTE);
         }
       if(input(PIN_C5))
         {
            fonction_repeat();
            fonction_decode();
         }
      }
switch(commande) {
            case CODEIR_AV:   
             
              mot_avancer();
           donnee[i]=donnee[i]+1;           //// ICI
               delay_ms(500);
               break;
            case CODEIR_AR:
               
               mot_reculer();
           donnee[i]=donnee[i]+2;            ////ICI
               delay_ms(500);
               break;
            case CODEIR_ST:
               
               mot_arreter();
            time=time<<3;
            donnee[i]=donnee[i]||time;                               ///// TIME
            time=0;
            i++;
               delay_ms(500);
               break;
            case CODEIR_RG:
               
               mot_rotation_gauche();
            donnee[i]=donnee[i]+4;                /// ICI
               delay_ms(500);
                break;
            case CODEIR_RD:
               
               mot_rotation_droite();
            donnee[i]=donnee[i]+5;      ///ICI
               delay_ms(500);
               break;
            case CODEIR_VG:
             
               mot_virage_gauche();
           donnee[i]=donnee[i]+6;            //// ICI
               delay_ms(500);
               break;
            case CODEIR_VD:
             
               mot_virage_droite();
           donnee[i]=donnee[i]+7;                           //////////ICI
               delay_ms(500);
               break;
           
         
   
            default:
               break;
         }


Last edited by will on Wed Jun 20, 2012 8:34 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Wed Jun 20, 2012 8:08 am     Reply with quote

Just going through the write routine - comments inline.
Code:

void fonction_memory(void) {
    for(i=0;i<=255;++i) {
      variable=donnee[i];
      //i is moving forwards by _2_ every loop - you increment both in the for
      //and at the end of the loop. You will access donnee[0], then donnee[2].
      write_eeprom(i, variable2);
      //Er. You have put the LSB of the value into variable, then write
      //variable2.....
      donnee[i]=donnee[i]>>8;
      //Do you mind that this will destroy the stored value?.
      variable=donnee[i];
      i=i+1;
      write_eeprom(i, variable);
   }
}

//Alternative:
void fonction_memory(void) {
    long val;
    int index;
    for(i=0;i<128;i++) {
      index=i*2;
      val=donnee[i]; //Will access values from locations 0 to 127.
      write_eeprom(index, make8(val,0)); //LSB
      write_eeprom(index+1,make8(val,1)); //MSB Write the two bytes
   }
}


You probably have similar problems elsewhere - haven't looked.

Best Wishes
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Wed Jun 20, 2012 8:37 am     Reply with quote

pffffffffff a big thank
and then you can not help me fix because I'm lost ..

a big thank you and sorry ..
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Wed Jun 20, 2012 8:48 am     Reply with quote

I have posted a fixed version for this function. Look for similar mistakes elsewhere.
will



Joined: 28 May 2012
Posts: 24
Location: france

View user's profile Send private message AIM Address

PostPosted: Wed Jun 20, 2012 10:02 am     Reply with quote

thank ,
why do you * 2? => index = i * 2
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Wed Jun 20, 2012 11:39 am     Reply with quote

Because 'i', addresses an array of int16's, and it takes two bytes of the EEPROM to store an int16. Hence two writes for each value in 'i', and the byte address used has to be 2*i.
This is the core problem from the start of the thread. Even if you used the CCS code to write an int16, the _address_ must move forward by 2 for each value written.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group