4

I am working on EEPROM AT24C16 for a particular project. According to the datasheet it must have 2048 addresses and I should be able to write 1024 unsigned int into it [ 1 unsigned int per 2 addresses] but I am only able to write 128 unsigned ints onto 256 addresses.

Is my EEPROM duplicate or I am confused and it has only 256 addresses. please help me to figure out the number of addresses in AT24C16.

Code:

#include<p18f4520.h>
#include<i2c.h>  // Required For In-built I2C Routine


#pragma config OSC = HS   /* Sets the oscillator mode HS */
#pragma config WDT = OFF   /* Turns the watchdog timer off */
#pragma config LVP = OFF   /* Turns low voltage programming off */


#define LCD_PORT  LATB
#define LCD_EN  LATDbits.LATD7
#define LCD_RS  LATDbits.LATD6
#define ADDRESS   0xA0

#define LCD_PORT_DIR  TRISB
#define LCD_EN_DIR  TRISDbits.TRISD7
#define LCD_RS_DIR  TRISDbits.TRISD6

#define I2C_SCL    TRISCbits.TRISC3
#define I2C_SDA    TRISCbits.TRISC4

#define AddressU 0
#define AddressL 1

int TempUpper, TempLower;
long int TempValue;
int TempUpper1, TempLower1;
long int TempValue1;

void DelayMS(unsigned int);
void init_lcd();
void command();
void lcd_data(unsigned char);
void Init_MSSP_I2C();



void DelayMS(unsigned int itime) {
    unsigned int i;
    unsigned char j;

    for (i = 0; i < itime; i++)
        for (j = 0; j < 165; j++);
}

void command() {
    LCD_RS = 0  ;
    LCD_EN = 1  ;
    DelayMS(1);
    LCD_EN = 0  ;
    DelayMS(1);
}
void lcd_data(unsigned char dat) {
    LCD_PORT  = dat ;
    LCD_RS    = 1  ;
    LCD_EN    = 1  ;
    LCD_EN    = 0  ;
    DelayMS(1);
}
void init_lcd() {
    LCD_PORT = 0x38 ;   //LCD 2 Lines
    command();
    LCD_PORT = 0x0E;  //Display On,Cursor On
    command();
    LCD_PORT = 0x01;  // Clear LCD
    command();
    LCD_PORT = 0x06;  //Shift Cursor right
    command();
    LCD_PORT = 0x80;  //line 1, position 1
    command();
}

void lcd_string(char rom *str) {
    while (*str)
    { lcd_data(*str++); }

    DelayMS(1);
}
void Init_MSSP_I2C() {
    I2C_SCL = 1                 ;   //      Initilize MSSP For I2C Send Write '1' RC3/SCL
    I2C_SDA = 1                 ;   //      Initilize MSSP For I2C Send Write '1' RC4/SDA
    OpenI2C(MASTER, SLEW_OFF)      ;  //   Start I2C Stop I2C
    SSPADD = 0x18 ;
}

unsigned int e, th, h, t, o, c = 0, g = 0, temp, tempvalue, fu, add = 0, mv = 0;


void main(void) {
    Init_MSSP_I2C();
    LCD_RS_DIR =  0 ;
    LCD_EN_DIR =  0 ;
    LCD_PORT_DIR = 0 ;
    DelayMS(250);   // Wait on Intilization LCD
    init_lcd();
    lcd_string((rom  char *)"Testing 24C02.. ");    // DPTR in 8051 AS
    TempValue = mv;
    add = 0;

    while (add < 512) {
        TempUpper1 = (TempValue >> 8) & 0x00FF;
        TempLower1 = (TempValue & 0x00FF);
        EEByteWrite(ADDRESS, add, TempUpper1);
        DelayMS(10);
        EEByteWrite(ADDRESS, add + 1, TempLower1);
        DelayMS(10);
        TempValue = TempValue + 1;
        add = add + 2;
    }

    init_lcd();
    lcd_string((rom  char *)"END ");

    while (1);

    while (add < 512) {
        TempUpper = EERandomRead(ADDRESS, add);
        TempLower = EERandomRead(ADDRESS, add + 1);
        DelayMS(10);
        add = add + 2;
        TempValue = 0x0000;
        TempValue = TempUpper;
        TempValue = TempValue << 8;
        TempValue = TempValue | TempLower;
        DelayMS(50);
        g = TempValue;
        fu = g / 10000; // gets 1000's value of number to Segments[1]
        temp = g % 10000;
        th = temp / 1000; // gets 1000's value of number to Segments[2]
        temp = temp % 1000;
        h = temp / 100; // gets 100's of number value to Segments[3]
        temp = temp % 100;
        t = temp / 10; // gets 10's value of number to Segments[4]
        o = temp % 10; // gets 1's value of number to Segments[5]
        LCD_PORT = 0xc2;    //line 1, position 1
        command();
        lcd_data(fu | 0x30) ;
        LCD_PORT = 0xc3;    //line 1, position 1
        command();
        lcd_data(th | 0x30) ;
        LCD_PORT = 0xc4;    //line 1, position 1
        command();
        lcd_data(h | 0x30) ;
        LCD_PORT = 0xc5;    //line 1, position 2
        command();
        lcd_data(t | 0x30) ;
        LCD_PORT = 0xc6;    //line 1, position 3
        command();
        lcd_data(o | 0x30) ;
        DelayMS(1000);
    }

    while (1);
}
Roh
  • 4,598
  • 6
  • 41
  • 86
prog_SAHIL
  • 221
  • 3
  • 13
  • AT24C16 has 256 bytes per page. It has 8 pages. Are you just writing to page zero? – Roger Rowland Jun 23 '15 at 08:49
  • I don't know how to flip between pages and write differently on them i am just sequentially incrementing the addresses like 'epwrite(value,address) epwrite(value,address+1)', plz guide me @RogerRowland. – prog_SAHIL Jun 23 '15 at 08:58
  • 1
    The datasheet (which I haven't seen) surely shows you the protocol used to address the memory? For example [see this similar project](http://exploreembedded.com/wiki/A6b:8051_Interfacing:_EEPROM_AT24C16#The_AT24C16) which describes the I2C protocol. – Roger Rowland Jun 23 '15 at 09:00
  • Can you show us the code you have? Without code and schematics all we can do is guess. – Nick Johnson Jun 23 '15 at 09:09
  • I have referred to it @RogerRowland and followed its protocols but it doesn't not seem to help me I am able to write the same data on both [AT24C16A](http://www.atmel.com/images/doc0180.pdf) and [AT24C8A](http://www.atmel.com/images/doc0336.pdf) while both of these have different address configuration. AT14C16A has more adresses but it can hold as much as info as AT24C8A. – prog_SAHIL Jun 23 '15 at 09:12
  • 1
    It's a shame to see this question not receiving a few upvotes. He's run into a problem in the library routines he is using rather than having a coding error per se. This is a typical trap for the uninitiated and the question is a reasonable one. – Russell McMahon Jun 23 '15 at 14:14
  • @NickJohnson Please see my above comment and Majenko's answer which points out what is wrong. – Russell McMahon Jun 23 '15 at 14:17
  • @RogerRowland Please see my above comment and Majenko's answer which points out what is wrong. – Russell McMahon Jun 23 '15 at 14:17
  • @RussellMcMahon Yes, I've seen both. Your point is what? You'd like me to upvote? I'd already upvoted Majenko's answer. – Roger Rowland Jun 23 '15 at 14:25
  • @RogerRowland - Only if it's a good idea in your considered opinion :-). I think he's trying but has had a reasonable drubbing across the board so far. Nothing unfair per se in isolation bit I suspect he'll do well with a little encouragement. – Russell McMahon Jun 23 '15 at 14:27
  • @RussellMcMahon Sure, and please don't take offence but voting is private and is my choice. You vote how you like and let others do the same. – Roger Rowland Jun 23 '15 at 14:29
  • @RogerRowland I wouldn't have it any other way :-). I'm not aware, of course, of who has noticed what and I just wanted to bring a set of information to your attention just in case it had slipped past unnoticed. I have no desire (or ability) 'to bend your arm' on any such matter and have no way of telling how you respond, so it's pretty private. I see there are a few votes happening - and I have no idea who they are from. – Russell McMahon Jun 23 '15 at 14:36
  • @RussellMcMahon Ok, to put you out of your misery, I *did* upvote because I happen to agree with your sentiment if not with your overt rep begging ;-) Please don't be so pushy in future eh? We're (mostly) all grown ups here and can make our own judgements as well as you. – Roger Rowland Jun 23 '15 at 14:40
  • 1
    Got it @RogerRowland . I checked the datasheet and found three pins as p0 , p1 , p2 meaning page control pins. I changed ADDRESS into array containing values A0,A2,A4,A6,A8,AA,AC,AE and now i can access all the pages. THANKS TO ALL GREAT HELP. – prog_SAHIL Jul 08 '15 at 11:23
  • Got it @NickJohnson . I checked the datasheet and found three pins as p0 , p1 , p2 meaning page control pins. I changed ADDRESS into array containing values A0,A2,A4,A6,A8,AA,AC,AE and now i can access all the pages. THANKS TO ALL GREAT HELP. – prog_SAHIL Jul 08 '15 at 11:24

1 Answers1

5

The problem here is actually with the I2C peripheral library, not your code.

The EEByteWrite function is defined as:

signed char EEByteWrite1(  unsigned char control,
                           unsigned char address,
                           unsigned char data );
#define EEByteWrite EEByteWrite1

As you can see the address parameter is an unsigned char which means it can only accept values up to 255. That's the limit you are hitting.

The EERandomRead function is defined similarly.

The built-in EEByte* functions in plib are not suitable for anything more than small EEPROMs with up to 256 addresses.

To access more than 256 addresses you will need to write your own routines that control the target chip appropriately.

Majenko
  • 55,955
  • 9
  • 105
  • 187
  • See my comment on his answer. He's trying to adapt, the question is reasonable and he needs encouraging. Nudge, nudge, ... . – Russell McMahon Jun 23 '15 at 14:15
  • I have tried it but its not working.When I googled it I learnt that EEPROM has 128 pages of 16 bytes each and we can write 16 bytes at a time by sending start bit>>data>>>stop bit and then swap between pages and repeat the same...But i still can't figure that out...what pages ...how to swap...what start/stop bit??? :( – prog_SAHIL Jun 23 '15 at 14:23
  • If you are just writing a single byte at a time you don't need to worry about pages. That's only really for higher speed block transfers. The start and stop bits are all part of the I2C protocol - you'll need to learn how that works first before you apply it to the EEPROM. – Majenko Jun 23 '15 at 14:42
  • Got it @Majenko . I checked the datasheet and found three pins as p0 , p1 , p2 meaning page control pins. I changed ADDRESS into array containing values A0,A2,A4,A6,A8,AA,AC,AE and now i can access all the pages. THANKS TO ALL GREAT HELP. – prog_SAHIL Jul 08 '15 at 11:22
  • Got it @RussellMcMahon . I checked the datasheet and found three pins as p0 , p1 , p2 meaning page control pins. I changed ADDRESS into array containing values A0,A2,A4,A6,A8,AA,AC,AE and now i can access all the pages. THANKS TO ALL GREAT HELP. – prog_SAHIL Jul 08 '15 at 11:22