Hi Eneshi,
how to use I²C you can learn for example at the Elektor Homepage:
www.elektor.de. Maybe there is a similar french page. There's a forum
for the Renesas R8C/13. This Controller is similar to the M16C.
The ATMEL AT24C16 is used in the following way:
your have to know the address of the EEPROM, for example
deviceadress_write = 0xa0;
deviceadress_read = 0xa1;
the page your want to read / write is:
0 <= addr <= 2048
writing with function Write_Byte(addr-1, BYTE data);
reading with Read_Byte(address);
the functions in detail:
void Write_Byte(int adress, BYTE data)
{
BYTE page = 0x00 | (int) (adress / 256);
page <<=1;
Send_Startcondition();
Send_Byte(page | deviceadress_write);
waitms = 5;
Send_Byte(adress & 0xff);
Send_Byte(data);
Send_Stopcondition();
waitms = 0;
};
BYTE Read_Byte(int adress)
{
BYTE page = 0x00 | (int) (adress / 256);
page <<=1;
waitms = 0;
Send_Startcondition();
Send_Byte(page | deviceadress_write); // device adress
Send_Byte(adress & 0xff); // word adress
Send_Startcondition();
Send_Byte(deviceadress_read); // device adress and next: read
BYTE res = Get_Byte();
Send_Stopcondition();
return res;
};
Regards,
Frank
just saw there's a french elektor site www.elektor.fr. Search for "I2C"
in the R8C/13-forum. Maybe the M16C has hardware-based I²C? The R8C/13
uses Software I²C. By the way: The R8C/13 has 4KByte of user accessible
ROM. So often an external EEPROM is not necessary. It's programmed in a
simple way via the Renesas FDT.
Thanks for your reply. I understand what you show me and I understand
the I²C principe but I have to use the Renesas sample program.
In this program, there is some function for reading and wrting on the
EEprom but actually I don't know how to use them
the eeprom_operation function in the file i2c_function.c is defined
like this :
unsigned char eeprom_operation(unsigned char slave, unsigned char
memaddr,
unsigned char *buffer, unsigned char length,
unsigned char rw)
iic_length=length;/* set data (bytes) in operation */
14
iic_pointer=buffer;/* set buffer pointer */
15
iic_memaddr=memaddr;/* set memory address */
16
if(rw==0){/* write operation */
17
if(iic_length>BYTE_LIMIT){/* limit the data (bytes) in */
18
/* page write operation */
19
iic_length=BYTE_LIMIT;
20
}
21
iic_mode=MODE_WRITE;
22
submode=WRITE_MEMADDR;
23
}
24
else{/* read operation */
25
iic_mode=MODE_READ;
26
submode=READ_MEMADDR;
27
}
28
s10=0xE0;/* start condition */
29
s00=iic_slave;
30
asm("popc FLG");/* restore FLG register */
31
return(1);/* success starting operation */
32
}
33
}
I've read the eeprom documentation again and I think the eeprom adress
can be write like this : #define EEPROM_ADDR 0b01010001
because in the documentation the eeprom control code is 1010, then it's
the block select bits on 3 bits and then the R/W bit (0 for writing and
1 for reading)
the eeprom_operation function manage two other function :
master_transfer and master_receive we can read in the comment that
theses function write and read on the eeprom, but in order to use them I
think I have to use the eeprom_operation function
have you any idea for using these functions please ?
Thank !
ok, i don't have the time at this moment to really investigate the code,
but here is my estimation:
>> unsigned char eeprom_operation(unsigned char slave, unsigned char>> memaddr,>> unsigned char *buffer, unsigned char length,>> unsigned char rw)
slave: adress of slave
memaddr: page you want to read of / write to
buffer: contains data to read / write
length: lenght of buffer (number of elements of type unsigned char)
rw read or write flag
use for write "Hello" on eeprom with slave address 0xa0 @ address 0x00:
char *hello = "Hello"; // 5 characters -> length = 5
eeprom_operation(0xa0, 0x00, hello, 5, 1);
Frank
I've trying your code but actually i've got two little errors as the
program don't know a variable. I'm currently trying to fix it.
Thanks for your help !
It works !!!... but I don't know how !
I fix the calling for function like this :
char hello[8] = {'H','e','l','l','o','B','o','y'};
char x[8] = {'0','0','0','0','0','0','0','0'};
eeprom_operation(0xa0, 0x00, hello, 8, 0); //writing
eeprom_operation(0xa0, 0x10, x, 8, 1); //reading
for (i=0; i<8; i++)
{
x[i]=x[i+8]; //without this, it displays only 0000000
}
DisplayString(LCD_LINE2,x); //disply on the lcd
I associate the writing with the button 1 and reading/displaying with
button2
but when I start the program, if i press directly the button 2, it
displays HelloBoy as if I have push the button1 and I don't know why and
how to fix it, have you any idea ?
Thanks a lot for your help !
wow! i am sure you'll fix the remaining problems.
char hello[] = 'hello' or hello = "hello" is not working?
the reading adress is one higher then writing
deviceadress_write = 0xa0;
deviceadress_read = 0xa1;
maybe your [i] = [i+8] has something to do with that?
Regards,
Frank
char hello[] display a strange character and hello = "hello" display
compilation error
it seems when I change reading adress from 0xa0 to 0xa1 nothing else
happened it's the same as 0xa0
and if I delete the line [i]= [i+8] I only display 00000000 on the lcd
that's why iI've add this line
I will add somen I²C temperatures sensors :
a AD5161 and a AD5252
In order to display the data from the sensors to the LCD, I have to
convert Data into a char string.
I think I will procede like this :
Binary data in char string ---> Binary data in Int string
an int variable (J) = Intstring[0]*128 +
Intstring[1]*64.....Intstring[7]*1
i=0;
intstring2[8];
while (J!=0){
instring2[i]=J%10;
J=J/10;
i++;}
char string[8]={'0','0','0','0','0','0','0','0'}
for (i=0;i<8;i++) {
char string[i]=intstring2[i]; }