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
  • Forget that question..
    I've found a working version...
    Thanks for your help..
    It really helped me alot..

    here's a sample if anyone wants it..

    #include <AT898252.H>
    #include <absacc.h>
    
    /*
     * Return EEPROM Byte at address 'adr'
     */
    unsigned char ReadEEPROM (unsigned int adr)
    {
      unsigned char v;
    
      WMCON |= EEMEN_;   //  enable EEPROM
      v = XBYTE[adr];    //  read value
      WMCON &= ~EEMEN_;  //  disable EEPROM
      return (v);
    }
    
    
    /*
     * Write EEPROM Byte 'val' at address 'adr'
     */
    void WriteEEPROM (unsigned int adr, unsigned char val)
    {
      WMCON |= (EEMEN_ | EEMWE_);     // enable EEPROM and set write bit
      XBYTE[adr] = val;               // write value
      while ((WMCON & EERDY_) == 0);  // wait until value programmed
      WMCON &= ~(EEMWE_ | EEMEN_);    // disable EEPROM and write strobe
    }
    
    
    unsigned char v;
    
    void main (void)
    {
      v = ReadEEPROM (0x200);         // read EEPROM address 0x200
      WriteEEPROM (0x200, 6);         // write 6 to EEPROM address 0x200
      while (1);
    }
    
    

Reply
  • Forget that question..
    I've found a working version...
    Thanks for your help..
    It really helped me alot..

    here's a sample if anyone wants it..

    #include <AT898252.H>
    #include <absacc.h>
    
    /*
     * Return EEPROM Byte at address 'adr'
     */
    unsigned char ReadEEPROM (unsigned int adr)
    {
      unsigned char v;
    
      WMCON |= EEMEN_;   //  enable EEPROM
      v = XBYTE[adr];    //  read value
      WMCON &= ~EEMEN_;  //  disable EEPROM
      return (v);
    }
    
    
    /*
     * Write EEPROM Byte 'val' at address 'adr'
     */
    void WriteEEPROM (unsigned int adr, unsigned char val)
    {
      WMCON |= (EEMEN_ | EEMWE_);     // enable EEPROM and set write bit
      XBYTE[adr] = val;               // write value
      while ((WMCON & EERDY_) == 0);  // wait until value programmed
      WMCON &= ~(EEMWE_ | EEMEN_);    // disable EEPROM and write strobe
    }
    
    
    unsigned char v;
    
    void main (void)
    {
      v = ReadEEPROM (0x200);         // read EEPROM address 0x200
      WriteEEPROM (0x200, 6);         // write 6 to EEPROM address 0x200
      while (1);
    }
    
    

Children
  • If you peek in absacc.h, you'll see:

    #define XBYTE ((unsigned char volatile xdata *) 0)

    This macro is handy if you like to think of memory as an array of bytes and don't want to deal with pointers.

  • Or just look at the _at_ keyword extension.

  • I thought about that -- but figured I might as well stick with generic techniques that work on any compiler. I'll happily use the extensions needed to take advantage of the architecture (bit, using, etc).

    But _at_ doesn't seem to me to add any real convenience over standard C features unless you're pointer-phobic. And it risks losing another thread to the complaint about how you can't use _at_ with initializers :)

  • "But _at_ doesn't seem to me to add any real convenience over standard C features unless you're pointer-phobic."

    It allows you to locate 'constant variables' in code space at known addresses without having to muck about with the linker.....

    "And it risks losing another thread to the complaint about how you can't use _at_ with initializers :)"

    .....but doesn't let you initialise them.

    What's the point of an uninitialised constant?