Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Creating variables with the address specified

Status
Not open for further replies.

azadfalah

Full Member level 2
Joined
Aug 13, 2016
Messages
145
Helped
1
Reputation
2
Reaction score
2
Trophy points
1,298
Activity points
2,423
Hello friends

I want through serial communication change the value of a byte

like this

Code:
void rtc_write(unsigned char address,unsigned char data)
{
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
}

but i don't know How to have a specified address for a byte on micro AVR

AVR ATMEGA64 8mhz
 

hi
didnt get it means what byte you want to change ? can you pls elaborate

- - - Updated - - -

Hello friends

I want through serial communication change the value of a byte

like this

Code:
void rtc_write(unsigned char address,unsigned char data)
{
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
}

but i don't know How to have a specified address for a byte on micro AVR

AVR ATMEGA64 8mhz



here you can directly send address and data
Code:
//for example in main loop
  unsigned char address_to be_send = 0x05;
  unsigned char data_to be_send = 0x01;
int main ()
{
  // one method dirctly send if same add and data
  rtc_write(0x05,0x01);  // 1st method

  // second method prefer this one 
  rtc_write(address_to be_send,data_to be_send);  // 2nd method
}


or something else is your doubt ?

- - - Updated - - -
 
Last edited:

hi
didnt get it means what byte you want to change ? can you pls elaborate

- - - Updated - - -


hi,

I want change the value of a byte (char variable) Through serial communication by variable address

So I want to know how to make the pre-specified address of a variable the code was an example
 

Hi
when you create any variable in .c file compiler will assign address to it , you can access its address through pointer. Pointer may you can pass to the function.
is this is your doubt ? or what exactly you wanted do? you can simply sketch ....
 
OK,

I want change the value of a variable through serial communication , But through the variable address on micro AVR

Code:
i2c_start();
// the code on micro master
i2c_write(0xd0);
i2c_write(address);  // how can i make Fixed address on slave for a variable To change the value by serial
i2c_write(data);
i2c_stop();
 

Something like that:
Code:
char address, value;
if (RX.DataReceived == SET)
   {
      scanf(RX.Buf, "Write: %u, %u", &address, &value);
      IC2_EEPROM_WRITE (address, value);
      RX.DataReceived = RESET;
   }
 
hi
see for I2c communication you have master and multiple slaves (multi master is also there). each slave have there fixed address. as per my undersatnding your having multiple slaves and you wanted to change value of variable address to communicate with different slave is it ? or you wanted to change the address of slave device?
 

i want Change the value of a variable in slave

Example i want change the value of x in slave micro ,

in master i want write data on the address x (write on x) in slave

Code:
i2c_start();
i2c_write(0xd0);
i2c_write(address); // but i dont have the x address on the slave micro
i2c_write(data);
i2c_stop();

so in slave micro how can i make x with Fixed address to use it for change x from master micro

//I did not put the receiver code
 

hi

ok got it like in slave device you wanted to write on different addresses check out the below image
24XX64-EEPROM-Byte-Write-1024x258.png

- - - Updated - - -

here

Code:
i2c_start();  // your inserting start bit 
i2c_write(0xd0);  // writing control byte i.e write + address of slave device
i2c_write(address); // here your addrees will be there where you want to write data
i2c_write(data);  // actual data
i2c_stop();
 

Ok. I understand. You have a variable named address used in RTC I2C function and you want to set the value of the variable named address using UART. Right ? If yes, Use Serial Interrupt to receive serial data and then if the received data is non ascii then assign that byte to the variable named address or if the received byte is ascii then subtract 48 from it and assign it to the variable named address.

If data received from UART is non ascii

Code:
void interrupt() {
     address = UART_Read();    
}

If data received from UART is ascii

Code:
void interrupt() {
     address = UART_Read() - 48;    
}
 
so for example you have EEPROM

Code:
#define SLAVE_ADD_WRITE 0x0d



char array_of_add[10] = {0x0A,0x11,0x22..}; // different addresses to write data on
char array_of_data[10] = {0x01,0x02...}; // different addresses to write data on
int main()
{
  char address;
  char data;
  
   address = array_of_add[0] ;
   data = array_of_data[0]
   rtc_write(address , data);  // data (0x01) witten on 0x0A address

   address = array_of_add[1] ;
   data = array_of_data[1]
   rtc_write(address , data);  // data (0x02) witten on 0x11 address

many methods are there through pointers , etc .you need to choose as per requirment
 
Friends
Nevermind sure RTC

i want make serial communication between Two microcontroller
want Change the value of a variable in slave by slave variable address (not slave ID , Variable adress in RAM ) but i don,t know how make variable With fixed address on slave micro to change it from master
 

hi
for changing value of variable which is at slave side ok...

I can suggest one thing I am not sure if it works ..
at slave side in I2c code you need to extract address and data, my suggestion to you is you send any dummy address i.e 0x0a and data (new value which you need to update to variable at slave side ) then at slave side in I2C isr routine collect address and data.
data then you can collect in temp variable then you can update to actual var.
 

Your slave is an MCU (avr as i understood?) and you don't know the address within the slave from your master? Do you write a code for the slave?
If so, it's easy. You don't have to make x with fixed address. But your i2c-slave routine must process incoming address from master and translate it from "external" (what address sees the master) to "internal" (the address of variable inside the slave). For example, let "external" address of x be 0x20 and you want to write there a value 0xA5, so your master code will look like this:
Code:
i2c_start();
// the code on micro master
i2c_write(0xD0); //chip addr
i2c_write(0x20); //x addr
i2c_write(0xA5); //data to be written
i2c_stop();

And on receiving side your code will contain the following:
Code:
// the code on micro slave

int8_t x; // the variable you want to change

i2c_write_from_master()
/// From i2c we will get 0xD0, 0x20 and 0xA5 (chip_id, addr, data). I omitted details of receiving, just assume we get everything right.
if (addr == 0x20)  x = data;

That is very simplified, but I guess the idea is clear. If you write a slave code, so it's up to you to describe the internal structure of your memory available via i2c.
 

hi
yes I am thinking the same if slave device is any mcu don't care the address on which we need to put data , just take care of collecting data in I2C ISr routine .address may be you can use for just data integrity

Code:
//slave side code[B] just example[/B] 
unsigned char var ; // slave side var to be updated
var = I2CRead();

// in your i2C read routine
unsigned char I2CRead()
{
       unsigned char i, Data=0;
       // recive address ommiting recive side code 
  	
	for (i = 0; i < 8; i++) {
		SCL = 1;
		if(SDA)
			Data |=1;
		if(i<7)
			Data<<=1;
		SCL = 0;
	}
	return Data;
}
 

Your slave is an MCU (avr as i understood?) and you don't know the address within the slave from your master? Do you write a code for the slave?
If so, it's easy. You don't have to make x with fixed address. But your i2c-slave routine must process incoming address from master and translate it from "external" (what address sees the master) to "internal" (the address of variable inside the slave). For example, let "external" address of x be 0x20 and you want to write there a value 0xA5, so your master code will look like this:
Code:
i2c_start();
// the code on micro master
i2c_write(0xD0); //chip addr
i2c_write(0x20); //x addr
i2c_write(0xA5); //data to be written
i2c_stop();

And on receiving side your code will contain the following:
Code:
// the code on micro slave

int8_t x; // the variable you want to change

i2c_write_from_master()
/// From i2c we will get 0xD0, 0x20 and 0xA5 (chip_id, addr, data). I omitted details of receiving, just assume we get everything right.
if (addr == 0x20)  x = data;

That is very simplified, but I guess the idea is clear. If you write a slave code, so it's up to you to describe the internal structure of your memory available via i2c.


Thanks,

but I do not want use if to find the address i want write directly
This possible?
 

I'm still confused over this. The question isn't clear.

I *think* what Azadfalah is asking is either:
1. how to dynamically address an I2C slave without knowing it's address,
2. how to change the slave address by setting it from the master device,
3. how a slave can read it's own address.
4. can the master read the address of a slave.

If I'm right, the answers are:
1. you can't. It may be possible to broadcast to all devices at once but each has it's address partially or wholly set by hardware. It is assumed that the software is written for a specific design so the addresses would be known before hand. It may be possible to write to another device that sets the logic state on the I2C devices address pins.

2. If you can't address the device, you can't change it's settings so the same as 1. applies.

3. This may be possible on some devices but most would only be known because it is set by the address bits 'hard wired' in that device type. The software would be written to drive a particular device type and it would not normally change.

4. Only by polling each address and looking for a response. There is an algorithm for shortening the search (similar to device discovery on 1-Wire busses) but there is no I2C mechanism for devices to sequentially provide ID by themselves.

Brian.
 

Thanks,

but I do not want use if to find the address i want write directly
This possible?

In theory it's possible, but I wouldn't recommend just direct writing, it may lead to disasterous results (you can easily write wrong data to wrong place). Create an array of desired variables, check boundaries and use address as index ("array[addr]").
 

I'm still confused over this. The question isn't clear.

I *think* what Azadfalah is asking is either:
1. how to dynamically address an I2C slave without knowing it's address,
2. how to change the slave address by setting it from the master device,
3. how a slave can read it's own address.
4. can the master read the address of a slave.

If I'm right, the answers are:
1. you can't. It may be possible to broadcast to all devices at once but each has it's address partially or wholly set by hardware. It is assumed that the software is written for a specific design so the addresses would be known before hand. It may be possible to write to another device that sets the logic state on the I2C devices address pins.

2. If you can't address the device, you can't change it's settings so the same as 1. applies.

3. This may be possible on some devices but most would only be known because it is set by the address bits 'hard wired' in that device type. The software would be written to drive a particular device type and it would not normally change.

4. Only by polling each address and looking for a response. There is an algorithm for shortening the search (similar to device discovery on 1-Wire busses) but there is no I2C mechanism for devices to sequentially provide ID by themselves.

Brian.

hello ,

i have two microcontroller i want write directly on ram of slave microcontroller using (i2c.uart , ...) I just got the idea from i2c
 

Thanks,

but I do not want use if to find the address i want write directly
This possible?

yes possible .. like said in previous post in I2c read routine of slave device ignore address , take data. update that data to your variable
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top