Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

master and slave Microcontrollers

Status
Not open for further replies.

garg29

Advanced Member level 1
Joined
Nov 17, 2004
Messages
451
Helped
25
Reputation
50
Reaction score
9
Trophy points
1,298
Activity points
3,605
writei2c(0xa1

I'm trying to design a project just for hobby.
Project is suppose i have to mesaure some physical
property let's say temperature of 10 different
locations each situated nearby only. i have to use
slave and master method as i have drawn in picture
below. what method should i follow for programming.
I'll be really thankful if someone can please provide
just brief example on this.

Thanks in advance.

Garg
 

mrcube_ns

Advanced Member level 1
Joined
Apr 10, 2002
Messages
452
Helped
48
Reputation
96
Reaction score
34
Trophy points
1,308
Location
Europe
Activity points
3,814
slave writei2c

In this situations I use modbus (or mechanisams of modbus) or something similar (I do this on PLC's).

Master should put message:
ABCD
A - master address e.g. 01h
B - slave address e.g. 04h (third slave mcu)
C - command (give me temperature, or comething else, or put output pin 1 to "1" or ....), e.g. 01h give temp, 02h give time, 03h set output1 ....
D - checksum (there are many types of CS, so choose one...

Slave's should give answer:
ABCDE

A - slave address
B - master address (01h)
C - number of bytes which slave sending
D - data (C number of bytes data)
E - checksum

Regards,

Mr.Cube
 

    garg29

    points: 2
    Helpful Answer Positive Rating

garg29

Advanced Member level 1
Joined
Nov 17, 2004
Messages
451
Helped
25
Reputation
50
Reaction score
9
Trophy points
1,298
Activity points
3,605
Thanks Mr.Cube. Should i use SCL & SDA type two wire link for whole network(as i have drawn). Sorry if it's too basic....i'm new to communications
 

mrcube_ns

Advanced Member level 1
Joined
Apr 10, 2002
Messages
452
Helped
48
Reputation
96
Reaction score
34
Trophy points
1,308
Location
Europe
Activity points
3,814
Generaly,

You always can use i2c, it's made for this kind of communication (master - slaves).

I think you can find lots examples and codes on edaboard.

Best regards.

Mr.Cube
 

    garg29

    points: 2
    Helpful Answer Positive Rating

garg29

Advanced Member level 1
Joined
Nov 17, 2004
Messages
451
Helped
25
Reputation
50
Reaction score
9
Trophy points
1,298
Activity points
3,605
i2c reminds me of ds1307 rtc chip which i tried to commnicate with 89c51 i did but with PIC microcontroller i was unsuccessful. With eeprom 24cxx i have done again with 89c uCs will i need to take same method as in the code attached

Thanks........with best regards.
Garg


Code:
#include<reg52.h>
#include<intrins.h>


#define ACK		1
#define NO_ACK	0


unsigned char i2;


sbit SDA  =  P1^7;	// connect to SDA pin (Data)
sbit SCL  =  P1^6;	// connect to SCL pin (Clock)



//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
    SDA = 1;
	SCL = 1;
	_nop_();_nop_();
	SDA = 0;
	_nop_();_nop_();
	SCL = 0;
	_nop_();_nop_();
}

//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
	SDA = 0;	    	
	_nop_();_nop_();
	SCL = 1;
	_nop_();_nop_();
	SDA = 1;	
}

//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{    

	for (i2=0;i2<8;i2++)
	{
        SDA = (Data & 0x80) ? 1:0;
		SCL=1;SCL=0;
		Data<<=1;
	}

  	SCL = 1; 
	_nop_();_nop_();
	SCL = 0;

}

//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{
    
    unsigned char Data=0;

    SDA = 1;	
	for (i2=0;i2<8;i2++)
	{
		SCL   = 1;		
		Data<<= 1;
		Data  = (Data | SDA);		
		SCL   = 0;
		_nop_();
	}
    
 	if (ACK_Bit == 1)
	SDA = 0; // Send ACK		
	else		
	SDA = 1; // Send NO ACK				

	_nop_();_nop_();
	SCL = 1;		
	_nop_();_nop_();
	SCL = 0;
	
	return Data;
}

//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned int Addr)
{
   	unsigned char Data;
	Start();
	WriteI2C(0xA0);
	//WriteI2C((unsigned char)(Addr>>8)&0xFF);	// ....not valid for small EEPROMs
	WriteI2C((unsigned char)Addr&0xFF);
	Start();
	WriteI2C(0xA1);
	Data = ReadI2C(NO_ACK);
	Stop();
   	return(Data);
}

//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned int Addr,unsigned char Data)
{
	Start();
	WriteI2C(0xA0);
	//WriteI2C((unsigned char)(Addr>>8)&0xFF);	// send address high....not valid for small EEPROMs
	WriteI2C((unsigned char)Addr&0xFF);			// send address low	
	WriteI2C(Data);
	Stop();	
}
 

mrcube_ns

Advanced Member level 1
Joined
Apr 10, 2002
Messages
452
Helped
48
Reputation
96
Reaction score
34
Trophy points
1,308
Location
Europe
Activity points
3,814
Here some i2c code examples.

Help yourself.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top