storing two-byte value in eeprom

Discussion in 'Embedded Systems and Microcontrollers' started by quantumlab, Jan 15, 2009.

  1. quantumlab

    Thread Starter Member

    Dec 4, 2008
    19
    0
    Hi Im trying to store a 10-bit value in my eeprom of the 16f887. To write it to the eeprom it needs to be broken up into two bytes. i have used the following code but there are a few errors. could you help with the code please

    char i, j;
    void main() {
    ANSEL = 0; // Configure AN pins as digital I/O
    ANSELH = 0;
    PORTB = 0; // Initial PORTB value
    TRISB = 0; // Set PORTB as output
    PORTC = 0;
    TRISC = 0;

    j = 257;
    for (i = 0; i < 1; i++)

    void EEPROMWriteInt(int i, long j) // error is here***
    {
    byte Byte1 = ((j >> 0) & 0xFF);
    byte Byte2 = ((j >> 8) & 0xFF);

    EEPROM.write(i, Byte1);
    EEPROM.write(i + 1, Byte2);
    }
    Delay_ms(50);
    for (i = 0; i < 1; i++) {
    PORTB = EEPROM_Read(i); // Read data and display on PORTB
    PORTC = EEPROM_Read(i+1) ;
    Delay_ms(500);
    }
    }
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    At the start of your program you define i and j as global variables of type character but in the process EEPROMWrite you try to redefine i and j as int and long respectfully.
    Either remove the global define or rename the variables i and J in EEPROMWrite to something else.
    Also if j is a character it can't be assigned a value of 257, the maximum value a character can have is 255.
     
  3. quantumlab

    Thread Starter Member

    Dec 4, 2008
    19
    0
    thanks for your help. However errors are still occuring. the problem is the line:

    EEPROMWriteInt(int i, long j)

    it is saying it is undeclared.

    I may need to include EEPROM.h

    when i do this it cannot open the file.

    Where do i find this header file or is it necessary?

    Thanks
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    That error message is saying that you have not declared the EEPROMWriteint function prior to using it. As EEPROMWriteInt is a function that you have written and not a library function you will have to declare it before your program can use it.
    Add the line of code below to declare the EEPROMWriteint function and put it just before main()
    Code ( (Unknown Language)):
    1. void EEPROMWriteInt(int, long);
    You most likely will have to add an include to the EEPROM.h header file to use the eeprom library functions and also any other headers the your C (if it is indeed C) program needs but where they reside I can't say since I have no idea what program you are using.
     
  5. quantumlab

    Thread Starter Member

    Dec 4, 2008
    19
    0
    Hi thanks again for your help. We are using MikroC. The following line is now giving me an error:

    byte Byte1 = ((p_value >> 0) & 0xFF)

    Any idea why?

    The error is:

    ';' expected but Byte1 found
     
  6. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    I don't know if byte is a valid data type in MikroC but its not part of ANSI C. Try using char instead and see if that works.
     
Loading...