This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ReadWrite EEPROM in 89S8252

I got a code, and modified it to run..
But i can't seem to get the code correct.
it has to do with XDATA but i can't seem to get it right.

#include <AT898252.H>
#include "lcd.c"


BYTE ReadEEPROM(int addr)
{
	char EEdata;
	WMCON |= 0x08; // Enable EEPROM memory space

	EEdata = addr; // --> how do u write here?

	WMCON &= 0xF7; // Disable EEPROM memory space
	return(EEdata);
}

// To write a single byte to a location in EEPROM.

void WriteEEPROM(int addr, char EEdata)
{
	WMCON |= 0x08; // Enable EEPROM memory space
	WMCON |= 0x10; // Enable EEPROM memory write

	addr = EEdata; --> how do u write here?

	msdelay(15);
	WMCON &= 0xEF; // Disable EEPROM memory write
	WMCON &= 0xF7; // Disable EEPROM memory space
}


here's the assembly version..it worked..
but didn't manage to convert it into C.


WMCON DATA 96h ; watchdog and memory control register
EEMEN EQU 00001000b ; EEPROM access enable bit
EEMWE EQU 00010000b ; EEPROM write enable bit
WDTRST EQU 00000010b ; EEPROM RDY/BSY bit
ADDRESS EQU 10H
DATAS EQU 0AAH

; EEPROM read example.

;orl WMCON, #EEMEN ; enable EEPROM accesses
;mov dptr, #ADDRESS ; address to read
;movx a, @dptr ; read EEPROM
;xrl WMCON, #EEMEN ; disable EEPROM accesses

; EEPROM write example, utilizing fixed delay for write cycle.
; Delay is worst case (10 ms). Code for delay is not shown.
; Write is followed by verify (read and compare), but code to handle
; verification failure is not shown.


orl WMCON, #EEMEN ; enable EEPROM accesses
orl WMCON, #EEMWE ; enable EEPROM writes

mov dptr, #ADDRESS ; address to write
mov a, #DATAS ; data to write
movx @dptr, a ; write EEPROM

xrl WMCON, #EEMWE ; disable EEPROM writes
xrl WMCON, #EEMEN ; disable EEPROM accesses

Parents
  • // --> how do u write here?

    You have an address in xdata space at which you wish to read and write. A C pointer is an address. I'd use a pointer rather than an int. To turn a pointer into the thing at which it points, you "dereference" the pointer with the "*" operator:

    char* p;
    *p = myChar;

    typedef
        volatile BYTE xdata
        Eeprom;
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	char EEdata;
    	WMCON |= 0x08; // Enable EEPROM memory space
    
    	EEdata = *addr; // Note the '*'
    
    	WMCON &= 0xF7; // Disable EEPROM memory space
    	return(EEdata);
    }
    
    

    I'd make the style suggestion that your control bit constants should have symbolic definitions. And it's idiomatic to clear bits with an "&= ~flag", which falls naturally out of those symbolic constants. Add a little bit of consistency in the parameter and return types, and:

    typedef
        volatile BYTE xdata
        Eeprom;
    
    #define EepromEnable     0x08
    #define EepromWriteEn    0x10
    
    // Address copied from asm example
    
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	BYTE eeData;
    
    	WMCON |= EepromEnable; // Enable EEPROM memory space
    
    	EEdata = *addr; // Note the '*'
    
    	WMCON &= ~EepromEnable; // Disable EEPROM memory space
    
    	return eeData;
    }
    
    void WriteEEPROM(Eeprom* addr, BYTE eeData)
    {
    	WMCON |= (EepromEnable |
                      EepromWriteEn);
    
    	*addr = eeData; // Note the '*'
            msdelay(15);
    
    	WMCON &= ~(EepromEnable |
                       EepromWriteEn);
    }
    
    

    I'm tempted to define macros for the enable/disable as well, just to clean it up a bit more.


    #define EepromEnableRead  WMCON |=  EepromEnable
    #define EepromEnableWrite WMCON |=  (EepromEnable | EepromWriteEn)
    #define EepromDisable     WMCON &= ~(EepromEnable | EepromWriteEn)
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	BYTE eeData;
    
            EepromEnableRead;
    
    	EEdata = *addr; // Note the '*'
    
            EepromDisable;
    
    	return eeData;
    }
    
    void WriteEEPROM(Eeprom* addr, BYTE eeData)
    {
            EepromEnableWrite;
    
    	*addr = eeData; // Note the '*'
            msdelay(15);
    
    	EepromDisable;
    }
    
    
    

    No comments needed if the names are sufficiently self-explanatory.

Reply
  • // --> how do u write here?

    You have an address in xdata space at which you wish to read and write. A C pointer is an address. I'd use a pointer rather than an int. To turn a pointer into the thing at which it points, you "dereference" the pointer with the "*" operator:

    char* p;
    *p = myChar;

    typedef
        volatile BYTE xdata
        Eeprom;
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	char EEdata;
    	WMCON |= 0x08; // Enable EEPROM memory space
    
    	EEdata = *addr; // Note the '*'
    
    	WMCON &= 0xF7; // Disable EEPROM memory space
    	return(EEdata);
    }
    
    

    I'd make the style suggestion that your control bit constants should have symbolic definitions. And it's idiomatic to clear bits with an "&= ~flag", which falls naturally out of those symbolic constants. Add a little bit of consistency in the parameter and return types, and:

    typedef
        volatile BYTE xdata
        Eeprom;
    
    #define EepromEnable     0x08
    #define EepromWriteEn    0x10
    
    // Address copied from asm example
    
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	BYTE eeData;
    
    	WMCON |= EepromEnable; // Enable EEPROM memory space
    
    	EEdata = *addr; // Note the '*'
    
    	WMCON &= ~EepromEnable; // Disable EEPROM memory space
    
    	return eeData;
    }
    
    void WriteEEPROM(Eeprom* addr, BYTE eeData)
    {
    	WMCON |= (EepromEnable |
                      EepromWriteEn);
    
    	*addr = eeData; // Note the '*'
            msdelay(15);
    
    	WMCON &= ~(EepromEnable |
                       EepromWriteEn);
    }
    
    

    I'm tempted to define macros for the enable/disable as well, just to clean it up a bit more.


    #define EepromEnableRead  WMCON |=  EepromEnable
    #define EepromEnableWrite WMCON |=  (EepromEnable | EepromWriteEn)
    #define EepromDisable     WMCON &= ~(EepromEnable | EepromWriteEn)
    
    BYTE ReadEEPROM(Eeprom* addr)
    {
    	BYTE eeData;
    
            EepromEnableRead;
    
    	EEdata = *addr; // Note the '*'
    
            EepromDisable;
    
    	return eeData;
    }
    
    void WriteEEPROM(Eeprom* addr, BYTE eeData)
    {
            EepromEnableWrite;
    
    	*addr = eeData; // Note the '*'
            msdelay(15);
    
    	EepromDisable;
    }
    
    
    

    No comments needed if the names are sufficiently self-explanatory.

Children
No data