Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

External Eeprom

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hi all just got my eeprom from dipmicro and im trying to use it. I have the data sheet and all is wired! Now the hard part. How do i code for it? I know the process (i think):

Write Byte:
1. Send Start
2. Send Address of slave(eeprom) with a R/W end bit (for selecting read/write operation)
3. wait for acknowledgment
4.Send Word Address (where to write to)
5. wait for acknowledgment
6. Send data
7. wait for acknowledgment
8. Send Stop

If someone can give me a idea on how to do steps 1-3 i think i can figure out the rest.

The reason im using external eeprom is just mainly for learning and might use it to like personalize some stuff. This way i never have to reprogram the main chip. I pull the epprom and program it with another main chip and poof there we go lol.

OK Im using ASM but can use SourceBoostC if possible. And the main uC is a 18F1320. But i might change it after i learn the code. Like ill try to port it over.

Thanks in advance!
P.S. The Bold above is just because sometimes people ask what chip and what language and why this and that so i though i would bold it out so it stands out more :D
 
The reason im using external eeprom is just mainly for learning and might use it to like personalize some stuff. This way i never have to reprogram the main chip. I pull the epprom and program it with another main chip and poof there we go lol.

It's easier to just reprogram the main chip, and of course you can't run code from an EEPROM, only read data.
 
Hi all just got my eeprom from dipmicro and im trying to use it. I have the data sheet and all is wired! Now the hard part. How do i code for it? I know the process (i think):

Write Byte:
1. Send Start
2. Send Address of slave(eeprom) with a R/W end bit (for selecting read/write operation)
3. wait for acknowledgment
4.Send Word Address (where to write to)
5. wait for acknowledgment
6. Send data
7. wait for acknowledgment
8. Send Stop

If someone can give me a idea on how to do steps 1-3 i think i can figure out the rest.

The reason im using external eeprom is just mainly for learning and might use it to like personalize some stuff. This way i never have to reprogram the main chip. I pull the epprom and program it with another main chip and poof there we go lol.

OK Im using ASM but can use SourceBoostC if possible. And the main uC is a 18F1320. But i might change it after i learn the code. Like ill try to port it over.

Thanks in advance!
P.S. The Bold above is just because sometimes people ask what chip and what language and why this and that so i though i would bold it out so it stands out more :D

Why not study i2c_test.c in your SourceBoost Examples directory?

Wow, so many quick replies!
 
Last edited:
Here's a thought, since the 18F1320 has no MSSP (it has to be done via software) you could use the RB1 & RB4 I/O pins for the SCK and SDA and capture the I2C data using the PICkit2 Logic Tool.
Also I2C is a static clock, you can't exceed the spec but you can go as slow as DC...
 
Hi ,

Excuse for my english but I can try to help you in C ...

In first there is C code I using to read bite from EEPROM 24C16 :

-----

void eeprom_read () {

ControlBit=( 0b10100000 ); // Control

i2c_start();
i2c_write(ControlBit);
i2c_write(Adr);
i2c_start();
i2c_write(ControlBit | 1);
a=i2c_read(1);
i2c_stop();

}

----------

All this commands are incl. in C compiler I use ( unfortunately i use Atmel AVR ), but I write own procedures too.
 
Kuzito: Seems simple enough will try but after i learn some basics.

bill seems like a small feat but the main question is how? I was actually going to use the RA6 for SCK and a normal I/O pin for SDA. Would that work. The main readon is i might need the RB1 and RB4 later on.

Ill try it using RB1 and RB4 if i can get a code snippit in asm so i know how to start at least lol :D

Should i use it like rs232 and set the baud? If so to what?
How about some software i2c samples.(in asm)

(Going to google)
 
Hi,

I don't use ASM, but there are simple steps how you can send START:

-----

define E24_SDA RA5
define E24_SCL RA6


void My_I2C_START() {

DDRA=0b11111111; // make PIN SDA output

E24_SDA=1;
E24_SCL=1;
My_I2C_DELAY();

E24_SDA=0;
My_I2C_DELAY();

E24_SCL=0;
My_I2C_DELAY();

DDRA=0b11101111; // make PIN SDA intput
}

----
 

Attachments

  • i2c.jpg
    i2c.jpg
    17 KB · Views: 190
there are some more procedures, hope helping you ...

-----

void My_I2C_STOP() {
DDRA=0b11111111;
E24_SDA=0;
My_I2C_DELAY();
E24_SCL=1;
My_I2C_DELAY();
E24_SDA=1;
My_I2C_DELAY();
DDRA=0b11101111;
}

void My_I2C_SEND_1() {
E24_SDA=1;
My_I2C_DELAY();
E24_SCL=1;
My_I2C_DELAY();
E24_SCL=0;
My_I2C_DELAY();
}
void My_I2C_SEND_0() {
E24_SDA=0;
My_I2C_DELAY();
E24_SCL=1;
My_I2C_DELAY();
E24_SCL=0;
My_I2C_DELAY();
}

void My_I2C_SEND_BITE_8(unsigned char data) {
DDRA=0b11111111;
for (i = 0; i < 8; i++)
{
if(data&0x80) My_I2C_SEND_1();
else My_I2C_SEND_0();
data<<=1;
}
DDRA=0b11101111;
My_I2C_DELAY();
E24_SCL=1;
My_I2C_DELAY();
if (E24_SDA_IN==0) ACK=1; // NO ACK
else ACK=0;
E24_SCL=0;
My_I2C_DELAY();
}


-------


p.s. I think you must no "waiting for ACK", you must check is there ACK or not. But if you no check for ACK code will work too, only when you have connected eeprom you will read correct data, if no connected - you will read "00" of "FF"
 
would this make a Start Command in basic

Code:
	list	p=18F1320
	include	<p18F1320.inc>
	CONFIG	OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF

	cblock	0x00

	endc

SDA2	Equ 7
SCL2	Equ 6

	org	0x0000
init 
	bsf	OSCCON,IRCF2	;set to 8MHz clock
	bsf	OSCCON,IRCF1
	bsf	OSCCON,IRCF0
main
	call 	I2C_Start
	goto	main

I2C_Start
	clrf	TRISA 
	clrf	PORTA

	BSF 	PORTA, SDA2
	BSF 	PORTA, SCL2
	call	Delay5us

	BCF 	PORTA, SDA2
	call	Delay5us

	BCF 	PORTA, SCL2
	call	Delay5us

	BCF 	LATA, SDA2
	return
Delay5us
	bra 	$+2
	bra 	$+2
	return
	end
 
Last edited:
Looks good ... :)

Some reccomendations :

- you must have connected ressistor 2-5K between SDA and SCL lines to VCC
- when I try code, I attach two LED diodes to SDA and SCL lines and when I make my delay 1 sec, I check clearly how my code work. After I change back to milliseconds.

If you only send start and send ControlBit you must receive ACK.
This is first step what means that you code work.

And first if you have eeprom programmer write in eeprom address 0 in example 0x14 and try until you read same - 0x14.
 
I have both resistors already on and that LED thing sound COOL! i will surely try it. And ill try to check ACK this way i know if there is a error. Ill have to make it a interrupt.
Fixed and added some stuff. How does this look?
Code:
	list	p=18F1320
	include	<p18F1320.inc>
	CONFIG	OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF

	cblock	0x00
		datatmp
	endc

SDA2	Equ 7
SCL2	Equ 6

	org	0x0000
init 
	bsf	OSCCON,IRCF2	;set to 8MHz clock
	bsf	OSCCON,IRCF1
	bsf	OSCCON,IRCF0
main
	call 	I2C_Start
	goto	main

I2C_Start
	clrf	TRISA 
	clrf	PORTA

	BSF 	PORTA, SDA2
	BSF 	PORTA, SCL2
	call	Delay5us

	BCF 	PORTA, SDA2
	call	Delay5us

	BCF 	PORTA, SCL2
	call	Delay5us

	BSF 	TRISA, SDA2
	return

I2C_SendByte
	movwf	datatmp
	movlw	0x08
Loop_X
	btfss 	datatmp, 0
	call	I2C_Send0
	btfsc	datatmp, 0
	call	I2C_Send1
	
	RLNCF	datatmp
	DECFSZ	W
	goto 	Loop_X
	return

I2C_Send1
	bcf 	TRISA, SDA2

	bsf 	PORTA, SDA2	
	call 	Delay5us

	bsf 	PORTA, SCL2
	call	Delay5us

	bcf 	PORTA, SCL2
	call	Delay5us

	bsf 	TRISA, SDA2
	return

I2C_Send0
	bcf 	TRISA, SDA2

	bcf 	PORTA, SDA2	
	call 	Delay5us

	bsf 	PORTA, SCL2
	call	Delay5us

	bcf 	PORTA, SCL2
	call	Delay5us

	bsf 	TRISA, SDA2
	return

Delay5us
	bra 	$+2
	bra 	$+2
	return
	end
 
Last edited:
cool ... ;)

I understand partially your code, but no fully ...

But if you execute all nessesary steps code must work.

Only one - please reminder change SDA line to Input after send ControlBit and then check for ACK (eeprom must pull SDA line to 0V). This mean that eeprom receive your StartBit and ControlBit and waiting for next command.
 
I think there is one error:
---
1. Send Start
2. Send Address of slave(eeprom) with a R/W end bit (for selecting read/write operation)
3. wait for acknowledgment
4.Send Word Address (where to write to)
5. wait for acknowledgment
6. Send data
7. wait for acknowledgment
8. Send Stop
------

I thing that "7. wait for acknowledgment " must be "7. send acknowledgment" because there PIC must send or no send acknowledgment to eeprom.
But no sure - better check datasheet eeprom.

p.s. there is dinner time... wish success and will waiting for news that you run all.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top