I would like to start this topic to help us in use a professional compiler. Here everybody can contribute in making drivers for HiTech compilers. I will start with eeprom drivers, but first some type definitions.
File "types.h", this file I use for inter-compiler compatibility, these definitions are used only for Hi-Tech:
#ifndef _types_h_
#define _types_h_
typedef unsigned char U8;
typedef signed char S8;
typedef unsigned int U16;
typedef signed int S16;
typedef unsigned long U32;
typedef signed long S32;
typedef enum
{
false = 0,
true = 1
}
BOOL;
#endif //_types_h_
Now the eeprom driver.
File "Eeprom.h":
#ifndef _eeprom_h_
#define _eeprom_h_
#include <pic18.h>
#include "Types.h"
#define EepromWrite8 eeprom_write //use hi-tech macro
#define EepromRead8 eeprom_read //use hi-tech macro
void EepromWrite16(U8 address, U16 data)
U16 EepromRead16(U8 address)
void EepromWrite32(U8 address, U32 data)
U32 EepromRead32(U8 address)
void EepromWriteString(U8 address, U8* buffer, U8 len)
void EepromWriteFloat(U8 address, float data)
float EepromReadFloat(U8 address)
void EepromReadString(U8 address, U8* buffer, U8 len)
#endif //_eeprom_h_
File "Eeprom.c":
#define _eeprom_c_
#include "Types.h"
#include "Eeprom.h"
void EepromWrite16(U8 address, U16 data)
{
static U8 i;
for(i = 0; i < 2; ++i)
{
EepromWrite8((U8)(address + i), *((U8 *)(&data) + i));
}
}
U16 EepromRead16(U8 address)
{
static U8 i;
static U16 near data;
for(i = 0; i < 2; ++i)
{
*((U8 *)(&data) + i) = EepromRead8((U8)(address + i));
}
return(data);
}
void EepromWrite32(U8 address, U32 data)
{
static U8 i;
for(i = 0; i < 4; ++i)
{
EepromWrite8((U8)(address + i), *((U8 *)(&data) + i));
}
}
U32 EepromRead32(U8 address)
{
static U8 i;
U32 data;
for(i = 0; i < 4; ++i)
{
*((U8 *)(&data) + i) = EepromRead8((U8)(address + i));
}
return data;
}
void EepromWriteString(U8 address, U8* buffer, U8 len)
{
U8 i;
for(i = 0; i < len ; ++i)
{
EepromWrite8((U8)(address + i), *(buffer + i));
}
}
void EepromWriteFloat(U8 address, float data)
{
static U8 i;
for(i = 0; i < 4; ++i)
{
EepromWrite8((U8)(address + i), *((U8 *)(&data) + i));
}
}
float EepromReadFloat(U8 address)
{
static U8 i;
float data;
for(i = 0; i < 4; ++i)
{
*((U8 *)(&data) + i) = EepromRead8((U8)(address + i));
}
return data;
}
void EepromReadString(U8 address, U8* buffer, U8 len)
{
U8 i;
for(i = 0; i < len; ++i)
{
buffer[i] = EepromRead8((U8)(address + i));
}
}
To use this driver within a project, include "Eeprom.h" in the file you wanna use eeprom functions and build "Eeprom.c" along with other project files.