Comparing an array in eeprom (codevision)

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Happy new year clever people.

The following code snippet is the relevant bits from my cat-door controller.

ATMEGA8
Self-designed PCB
CodevisionAVR

It reads characters from an RFID reader, storing them in an array called incoming[].

Once the incoming[] array has 14 chars in it, it then checks against the known tags, and opens the door if it

finds a match.

Now I want to make the system learn a tag, rather than have it hard-coded.

So I make some storage space in eeprom, and store the tag number in there.
I know this part works OK - I have printf-ed it to a terminal. Yay!

However, the function "int compare_tag(char target[])", which worked fine with the hard-coded tags,
doesn't work with the eeprom-stored tags - I get the compiler error:

"Error: function argument #1 of type 'eeprom unsigned char [14]' is incompatible with required parameter of type

'unsigned char []' "

Main question:
How can I test my incoming[] against the eeprom stored tags - tag0[], tag1[], tag2[] etc

Bonus question:
You will see in the main() function that to store the tags is basically the same thing repeated 3 times. Its not really a problem at this stage, but what if I wanted to store 10 or 20 tags? I was taught that 'when you repeat code, you are making a mistake', but I can't think how to do it otherwise. Perhaps one massive array of 300 chars, which I search in 14-byte blocks? Suggestions?



//--------------------------------------------------------------------------------------------


// Declare your global variables here

int loop = 0;
int mode = 1;           // 0 = in/out, 1 = in only, 2 = learn

char c;                 // a place to put the incoming chars from the rfid reader

int read_in_progress = 0;
int bytes_read = 0;

int tag_to_be_checked =0;

int waiting_entry =0;      // who's at the door?

char incoming[14];

eeprom int num_tags = 0;
eeprom char  tag0[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
eeprom char  tag1[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
eeprom char  tag2[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
eeprom char  tag3[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//examples of hard-coded tags
char tiggy[14]={2,52,69,48,48,51,52,51,56,53,50,13,10,3};
char monty[14]={2,52,69,48,48,51,52,65,65,50,57,13,10,3};


void main(void)
{


while (1)
      {
        incoming_chars();                       	   // check for incoming characters

        if ( (tag_to_be_checked == 1) && (mode == 2) )     // if we have a 14 byte array full up, 
							   // and we are in 'learn' mode...
        {
           if (num_tags == 0)                              // there are no tags stored
           {
                for ( loop = 0 ; loop < 14 ;loop++)
                {
                    tag0[loop] = incoming[loop];           //store in position tag0
                    delay_ms(2);
                }
           
           }
           
           if (num_tags == 1)                             // there is 1 tag stored
           {
                for ( loop = 0 ; loop < 14 ;loop++)
                {
                    tag1[loop] = incoming[loop];          //store in position tag1
                    delay_ms(2);
                }
           
           } 
           
           if (num_tags == 2)                            // there are two tags stored
           {
                for ( loop = 0 ; loop < 14 ;loop++)
                {
                    tag2[loop] = incoming[loop];        //store in position tag2
                    delay_ms(2);
                }
           
           }          
           
      
           num_tags++;                                 // we have increased the number of stored tags
           mode = 1;					//tag stored, back to IN-ONLY mode
           IN_OUT_LED = 0;
           IN_ONLY_LED = 1;				
        }


	//in here are routines to open the door, etc
	//removed as irrelevant to the question!


}//end main



//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
void incoming_chars(void)
{    

    if (rx_counter)                                   // a character has come in
    {

        c = getchar();                                //retrieve the character
        bytes_read++;

        if ( c == 02 )                                //we got an STX - start filling the 'incoming' array
        {
            read_in_progress = 1;                     //right, we're off...
        }

        incoming[bytes_read-1] = c;                   //put the char into the array

        if (c == 03)                                  //we got an ETX
        {
            read_in_progress = 0;                     //finished, yay!
            tag_to_be_checked = 1;		      //this flags tells the main routine that there is 
						      //a full 14-char array ready to be compared
            bytes_read = 0;
        }

    } // end rx_counter

}

//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
int compare_tag(char target[])
{
    for (loop = 0 ; loop < 14 ; loop++)        // loop thru incoming[], and compare with the tag called 'target'
    {
        if (incoming[loop] != target[loop])    // if any difference...
        {
            return 0;                          // it's not this tag!
        }
    }

    return 1;                                  // if you get here, it must be this tag! yay!
}


//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
int find_ID(void)
{

    if ( compare_tag(tag0) == 1 )		<--- this is where the error appears
    {
        printf("It's tag 0!\r\n");
        return (1);
    }

    if ( compare_tag(tag1) == 1 )		<--- this is where the error appears
    {
        printf("It's tag 1!\r\n");
        return (2);
    }


    if ( compare_tag(tag2) == 1 )		<--- this is where the error appears
    {
        printf("It's tag 2!\r\n");
        return (3);
    }

}

//--------------------------------------------------------------------------------------------

I have left out all the servo control and user-interface button-reading stuff, in order to show you the relevant

stuff without clutter. If anybody needs the full code, it's attached as a file.

Thanks as always!

Pete

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have do something like eeprom_read_byte (& (yourvariable[i])); to read data from the eeprom.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All that you need to do is tell he compiler that the target[] array is in eeprom:

int compare_tag(eeprom char target[])
{
    for (loop = 0 ; loop < 14 ; loop++)        // loop thru incoming[], and compare with the tag called 'target'
    {
        if (incoming[loop] != target[loop])    // if any difference...
        {
            return 0;                          // it's not this tag!
        }
    }

    return 1;                                  // if you get here, it must be this tag! yay!
}

You can make a similar function to read in the tag in the first place.

Four tags are no great problem to hard code. If you intend to have 36 tags, you could access the eeprom in a two-dimensional array.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
All that you need to do is tell he compiler that the target[] array is in eeprom:

int compare_tag(eeprom char target[])

Four tags are no great problem to hard code. If you intend to have 36 tags, you could access the eeprom in a two-dimensional array.

David.

Thanks, I tried that. Unfortunately, it still gives me the same error message.

According to the codevision help, once the variable is declared as eeprom, you can just access it directly like any other variable.

I'd be pleased to hear any other suggestions.

Thanks
Pete

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
... access the eeprom in a two-dimensional array.

David.

... and that's the answer!

I implemented the 2D array, and it works perfectly!

The code is much simpler, too - much more logical.

Thank you so much for your help!
Pete

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you resolved the eeprom syntax?

I am convinced that I had the obvious syntax.
So I tried it myself. Sure enough, CV barfs at 'compare_tag(tag0)'.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rewolff wrote:
You have do something like eeprom_read_byte (& (yourvariable[i])); to read data from the eeprom.

No the thread title says "Codevision". Its integration of Harvard multiple memory spaces is FAR better than the generic GCC which needs "helper functions" like pgm_read_byte() and eeprom_read_byte().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Have you resolved the eeprom syntax?

David.

Nope - when I implemented a 2D array, it just worked - I just access it like its in RAM, and Codevision magically takes care of the eeprom access!

Thanks
Pete