HELP WITH FUNCTIONS

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
pumper
Posts: 289
Joined: 10 Jan 2011 17:37

HELP WITH FUNCTIONS

#1 Post by pumper » 06 May 2024 23:18

I am using PIC18F47K42 which has not been updated for the eeprom so I am trying to write my own code.

When I use the following code it compiles and runs on hardware without any errors.

Code: Select all

My_EEPROM_Write(0x0000, Highest(MILES));
My_EEPROM_Write(0x0001, Higher(MILES));
My_EEPROM_Write(0x0002, Hi(MILES));
My_EEPROM_Write(0x0003, Lo(MILES));

Highest(MILESt) = My_EEPROM_Read(0x0000);
Higher(MILESt) = My_EEPROM_Read(0x0001);
Hi(MILESt) = My_EEPROM_Read(0x0002);
Lo(MILESt) = My_EEPROM_Read(0x0003);

But when I use this call the compiler goes crazy with errors, can you see what is wrong.
This is just a collection of calls I want to make with one call

Code: Select all

Get_My_EEPROM_Write();//CONTAINS A LIST OF DATA TO WRITE TO THE EEPROM
Get_My_EEPROM_Read(); //CONTAINS A LIST OF DATA TO READ FROM THE EEPROM




------------------------------------------------------------------------------------------------------

Code: Select all

//The following is in a separate folder called MY_EEPROM_READ_WRITE.c
//it is #include "MY_EEPROM_READ_WRITE.c"

void Get_My_Eeprom_Write()
{
  My_EEPROM_Write(0x0000, Lo(MILES));       //MILES 32 BIT
  My_EEPROM_Write(0x0001, Hi(MILES));
  My_EEPROM_Write(0x0002, Higher(MILES));
  My_EEPROM_Write(0x0003, Highest(MILES));
}  //END OF EEPROM WRITE
//--------------------------------------------------------------------------
void Get_My_Eeprom_Read()
{
 Highest(MILES) = My_EEPROM_Read(0x0000);
 Higher(MILES = My_EEPROM_Read(0x0001);
 Hi(MILES) = My_EEPROM_Read(0x0002);
 Lo(MILES) = My_EEPROM_Read(0x0003);
}   //END OF EEPROM READ


void My_EEPROM_Write(unsigned int address, unsigned short My_Data)
   {
    //Set the EEPROM address to write TO THEN WRITE DATA
    NVMCON1 = 0;  //reset all NVMCON1 to 0
    NVMADRL = address;  //Lo address      address is 16 bit
    NVMADRH = address>>8;  //Hi address
    NVMDAT = My_Data;   //data to put in above address   8 BIT

    NVMCON1.WREN = 1; //erase and refresh
    INTCON0.GIE = 0;  //STOP INTERRUPTS
    NVMCON2 = 0x55;   //UNLOCK
    NVMCON2 = 0xAA;   //SEQUENCE
    NVMCON1.WR = 1; //INITIATES ERASE/PROGRAM CYCLE
    while(NVMCON1.WR)  //WHILE TRUE =1

    NVMCON1.WREN = 0; //INHIBITS PROGRAMMING/ERASING
    INTCON0.GIE = 1;  //TURN ON INTERRUPTS
    //--------------------------------------------------------------------------
   }

unsigned short My_EEPROM_Read(unsigned int address)
   { //unsigned short My_DatA;
     NVMCON1=0;  //reset all to 0
     NVMADRL = address;
     NVMADRH = address>>8;
     NVMCON1.RD = 1;  //ISSUE READ, will clear when read
     My_Data = NVMDAT;
     return My_Data;
     //-------------------------------------------------------------------------
   } 


paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: HELP WITH FUNCTIONS

#2 Post by paulfjujo » 07 May 2024 18:05

Hello,



Are MILES and MILESt variables declared as Global ?
long MILES;
long MILESt;

why did you put My_DatA as a comment ?
Be carrefull !
My_DatA is not the same variable as My_Data !


unsigned short My_EEPROM_Read(unsigned int address)
{ //unsigned short My_DatA; <---- ?
NVMCON1=0; //reset all to 0
NVMADRL = address;
NVMADRH = address>>8;
NVMCON1.RD = 1; //ISSUE READ, will clear when read
My_Data = NVMDAT;
return My_Data;
//-------------------------------------------------------------------------
}


Avoid to use same variable name passed into the function
as used variable in main program.... Confusion risk!

void My_EEPROM_Write(unsigned int address, unsigned short My_Data)

Post Reply

Return to “mikroC PRO for PIC General”