This time I tried to connect two microcontrollers with I2C communication.
I2C is a serial protocol and synchronous bus. In standard applications and the hardware and the time is same.
Configuring the I2C communication
Example of simulation using the Proteus
code:
'----------------------------------------------------------------
' microcontroller 1
'----------------------------------------------------------------
$regfile = "m32def.dat"
$crystal = 11059200
$baud = 9600
Config Sda = Portd.2
Config Scl = Portd.4
Config Portc = Output
I2cinit
Dim X As Integer
Dim Y As Integer
Const Slave = &H40
X = 0
Do
I2cstart
I2csend Slave , X
I2cstop
Waitus 100
Incr X
If X > 9 Then X = 0
I2cstart
I2creceive Slave , Y
I2cstop
Waitus 100
Portc = Lookup(y , Dta)
Loop
Dta:
Data &H40 , &H79 , &H24 , &H30 , &H19 , &H12 , &H02 , &H78 , &H00 , &H10
'----------------------------------------------------------------
' microcontroller 2
'----------------------------------------------------------------
$regfile = "m32def.dat"
$crystal = 11059200
$baud = 9600
Config Sda = Portd.2
Config Scl = Portd.4
Config Portc = Output
I2cinit
Dim X As Integer
Dim Y As Integer
Y = 9
Const Slave = &H40
Do
I2cstart
I2creceive Slave , X
I2cstop
Waitus 100
Portc = Lookup(x , Dta)
I2cstart
I2csend Slave , Y
I2cstop
Waitus 100
Decr Y
If Y < 0 Then Y = 9
Loop
Dta:
Data &H40 , &H79 , &H24 , &H30 , &H19 , &H12 , &H02 , &H78 , &H00 , &H10
The workings of this program which the microcontroller 1 will send the numbers 0 to 9 continue to return to 0 and so on. This data will be received by the microcontroller 2 and will be displayed with seven segment display. Conversely microcontroller2 will send the number 9 down to 0 and back to 9 and so on. This data will be received by the microcontroller 1 and will be displayed with seven segment display.
Note:
The thing to remember is the timing of the microcontroller should be the same or will result in the received data is not proper.
I2C is a serial protocol and synchronous bus. In standard applications and the hardware and the time is same.
Configuring the I2C communication
Example of simulation using the Proteus
code:
'----------------------------------------------------------------
' microcontroller 1
'----------------------------------------------------------------
$regfile = "m32def.dat"
$crystal = 11059200
$baud = 9600
Config Sda = Portd.2
Config Scl = Portd.4
Config Portc = Output
I2cinit
Dim X As Integer
Dim Y As Integer
Const Slave = &H40
X = 0
Do
I2cstart
I2csend Slave , X
I2cstop
Waitus 100
Incr X
If X > 9 Then X = 0
I2cstart
I2creceive Slave , Y
I2cstop
Waitus 100
Portc = Lookup(y , Dta)
Loop
Dta:
Data &H40 , &H79 , &H24 , &H30 , &H19 , &H12 , &H02 , &H78 , &H00 , &H10
'----------------------------------------------------------------
' microcontroller 2
'----------------------------------------------------------------
$regfile = "m32def.dat"
$crystal = 11059200
$baud = 9600
Config Sda = Portd.2
Config Scl = Portd.4
Config Portc = Output
I2cinit
Dim X As Integer
Dim Y As Integer
Y = 9
Const Slave = &H40
Do
I2cstart
I2creceive Slave , X
I2cstop
Waitus 100
Portc = Lookup(x , Dta)
I2cstart
I2csend Slave , Y
I2cstop
Waitus 100
Decr Y
If Y < 0 Then Y = 9
Loop
Dta:
Data &H40 , &H79 , &H24 , &H30 , &H19 , &H12 , &H02 , &H78 , &H00 , &H10
The workings of this program which the microcontroller 1 will send the numbers 0 to 9 continue to return to 0 and so on. This data will be received by the microcontroller 2 and will be displayed with seven segment display. Conversely microcontroller2 will send the number 9 down to 0 and back to 9 and so on. This data will be received by the microcontroller 1 and will be displayed with seven segment display.
Note:
The thing to remember is the timing of the microcontroller should be the same or will result in the received data is not proper.
nice share bro..
ReplyDelete