[PIC] EEPROM Reading issue

Status
Not open for further replies.

Vibrant2020

Newbie level 6
Joined
Sep 23, 2020
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
155
Hi all,
I am facing trouble in reading AT24C32 EEPROM from PIC MCU over I2C.

EEPROM : AT24C32
PIC MCU: PIC16F1938
I2C Module Interface
IDE: MPLAB V8.60
COMPILER : HI TECH C

The code is as follows:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
unsigned char EEPROM_ADDR;
unsigned long chrData[300];
 
void ReadXTEEPROM(void){
 
unsigned long k = 0;
 
       for(int i = 0; i < 9; i++){
             EEPROM_ADDR = i * 32;
 
              I2C_Master_Start();
              I2C_Master_Write(0xA0);
              I2C_Master_Write((EEPROM_ADDR >> 8) & 0xFF);
              I2C_Master_Write((EEPROM_ADDR) & 0xFF);
              I2C_Master_RepeatedStart();
              I2C_Master_Write(0xA1);
 
              long j;
              for(j = 0; j < 32; j++){ // 32 bytes / page
                    chrData[k] = I2C_Master_Read(0); // ACK
                    k++;
              }
              chrData[k] = I2C_Master_Read(1); //NACK
              j = 0;
              I2C_Master_Stop();
              __delay_ms(10);
        }
   k = 0;
}



If I run this code, I am able to get only up to 170 bytes from EEPROM. If I replace the "k" with "j" in the for loop, i am able to read entire bytes (288 bytes) in pagewise. That is, put break point in debugging mode at the line "j = 0", the data read per page and displayed correctly.

What could be the problem? Am I missing anything in the code?

Advance thanks

Vibrant2020
--- Updated ---

Added,: There are 300 bytes available in the EEPROM.
 

Hi,

Your address is "char", but char usually is a byte with range of 0...255. I think you need uint16 at least.
Your EEPROM data is "long" usually is 16 bit, but EEPROM data usually are byte oriented. So here a "char"may be used.

But I don't be familiar with your compiler...and there are 16bit oriented EEPROMs.
So please check both

Klaus

Added:
Sorry, just realized that EEPROM addres is the I2C address. Char is suitable..
Does it compile without warnings? Maybe you're running out of memory.

Indeed I personally don't like the "int" and "long", because it depends on compiler and/or device what variable size is used.
 
Last edited:

I'm not certain but you might have a problem with lines 9 and 13 overflowing the char sized variable. Try making EEPROM_ADDR an unsigned int. The maximum value an unsigned char can hold is 255 (0xFF) but 9 * 32 = 288.

Brian.

[edit] - sorry Klaus, I cross posted with the same comment.
 

Hi,
[edit] - sorry Klaus, I cross posted with the same comment.
No problem. You see I'm not really awake yet... ;-)

******
I recommend to define EEPROM address inside the function, not globally.
...and to define all function variables at the beginning of the function.

Klaus
 

Hi,
I found that the maximum variable length is 80. If I go beyond, strange things are happenning. So, I set the maximum length is 80 and read in 45 steps (32 bytes per page) using "for loop". Now everything works fine. I can store the bitmap data in to the AT24C32 EEPROM and Read back and print on Thermal Printer (TTL).

But I have to replace this EEPROM with AT24C512 in a day or two. I will update once the test is complete.

Note ***: I am able to print the bitmap only on left justification (default). It would be better if you can help to print it in center justification. I tried ESC a 1 (as per the datasheet for text alignment). It works correctly on texts and even barcodes. But not on image.

Thank you all.

Vibrant2020
 

Hi,

Sadly you don't refer to our posts and you don't show your new code.

I don't know what all this has to do with bitmap, left or right justification and ESC codes...

For me this is a rather confusing post.

Klaus
 

Hi,
Actually I was trying to print bitmap image (monochrome bmp file) on Thermal Printer RP203 (TTL interface) from PIC16F1938. Since no memory available on PIC MCU to store huge data, I have used EEPROM to store the image file.

Procedure to prepare image before writing in to EEPROM:

1. Select your image and Save image as monochrome bmp file. The width (pixels) of the image should be multiples of 8. Height no matter.
2. Convert bmp file in to data file and save data file as header (*. h) file
3. Include into your project file

Now you can save the data file into EEPROM using I2C.

This data file can be read back from the EEPROM whenever required and send it to Thermal Printer to print the image.

My new code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
unsigned long chrData[32];
 
void ReadXTEEPROM(void){
 
unsigned long k = 0;
unsigned char EEPROM_ADDR;
 
// This is ESC/POS command for printing the bitmap on the Thermal Printer (EPSON)
// Serial (TTL) communication
 
putch(0x1D); // GS
putch(0x76); // v
putch(0x00); // 0
putch(0x30); // m = 0
putch(0x10); // xL = width of the image in bytes. 128 pixels width / 8 = 16 bytes
putch(0x00); // xH
putch(0x5A); // yL = height of the image in pixels. 90 pixels
// Now we have to supply the image data d1,d2,.....dk
 
// I already converted the image file
//(monochrome bmp file) in to data file and
//stored in the AT24C32 EEPROM. Read EEPROM again to print.
 
       for(int i = 0; i  < 45; i++){ // 1440 bytes to read (45 x 32)
             EEPROM_ADDR = i * 32;
 
              I2C_Master_Start();
              I2C_Master_Write(0xA0);
              I2C_Master_Write((EEPROM_ADDR >> 8) & 0xFF);
              I2C_Master_Write((EEPROM_ADDR) & 0xFF);
              I2C_Master_RepeatedStart();
              I2C_Master_Write(0xA1);
 
              long j;
              for(j = 0; j < 32; j++){ // 32 bytes / page
                    chrData[k] = I2C_Master_Read(0); // ACK
                    k++;
              }
              chrData[k] = I2C_Master_Read(1); //NACK
              j = 0;
              I2C_Master_Stop();
              __delay_ms(10);
 
             // Printing bitmap. i. e. Send the read data serially to the Thermal Printer
              for(int x = 0; x < 32; x++){
                   putch(chrData[x]);
              }
        }
   k = 0;
}


Hope I cleared your confusion. Still you require more details, please let me know.

Thank you,
Vibrant2020
 
Last edited:

Hi,

O.K.
you ignored our posts.

Why do you ask for help then?

Klaus
 

Isn't going to work Vibrant2020, for the reasons Klaus and myself already gave you. You may not get a compiler error but you will be reading the wrong areas of EEPROM. The problem will be even worse if you use a bigger EEPROM.

Brian.
 

Your address is "char", but char usually is a byte with range of 0...255. I think you need uint16 at least.

I have converted my char to uint16_t in the new code. Also EEPROM variable declared inside the function as you told. Initially I didn't change the EEPROM variable from "unsigned char" to uint since it was a trial and I am only giving starting EEPROM address 0x00. But after successful result obtained , I changed it to uint16_t to accommodate large EEPROM address dynamically.

you ignored our posts.
No no. I never ignored ur post and tried and implemented your suggestion. Everything goes fine. Will update you in a day or two regarding big EEPROM.

If you have anything to say, let me know

Thank you.
Vibrant2020
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…