//Master MCU
# include & ltWire.h & gt
void setup() {
wire . begin(); //Initialize I2C communication
}
Void loop () {
wire . begin transmission(8); //Start sending data to the device with address 8.
wire . write(" 1 "); //Send character' 1'
wire . end transmission(); //End the transmission
Delay (500); //Wait 500 milliseconds
}
-
//From single chip microcomputer
# include & ltWire.h & gt
void setup() {
wire . begin(8); //Join the I2C bus with address 8.
wire . on receive(receive event); //Register to receive data events
PinMode( 13, output); //Set pin 13 to output mode.
}
Void loop () {
Delay (100);
}
//Call this function when data is received.
void receive event(int number){
While (Wire.available()) {// loop until all the data has been read.
char c = wire . read(); //Read a character
If (c = =' 1') {/if character is'1'
DigitalWrite( 13, high); //Set pin 13 to high level.
} Otherwise {
DigitalWrite( 13, low); //Otherwise, set pin 13 to low level.
}
}
}
The above code is for reference only, and the specific code may be different according to your hardware and requirements. When writing code, you need to consider the specific model, communication protocol, port number and other factors of your single chip microcomputer.
..................................................
You can use I2C protocol to communicate and control between two 80C5 1 single-chip computers. The following is a basic example, which shows how to write code on the master and control the port of another slave through I2C protocol.
First, we need to write code on the main microcontroller to send I2C commands. Here is a simple example:
# include & ltreg 5 1 . h & gt;
#include "I2C.h" // I2C communication library
# Defines the slave address 0x50//I2C address of the slave microcontroller
void main() {
Unsigned character command = 0x01; //Command to send
I2C _ initialization (); //Initialize I2C
I2C _ Start(); //Start I2C communication
I2C _ write (slave _ address); //Write the address of the slave microcontroller.
I2C_Write (command); //Write command
I2C _ Stop(); //Stop I2C communication
while( 1); //infinite loop, keep the program running
}
Then, the code written on the slave microcontroller receives the I2C command and controls the port according to the received command. Here is a simple example:
# include & ltreg 5 1 . h & gt;
#include "I2C.h" // I2C communication library
# Defines the slave address 0x50//I2C address of the slave microcontroller
void main() {
Unsigned character command;
I2C _ initialization (); //Initialize I2C
while( 1) {
If (I2C _ read (from address, & command) {//If I2C command is received.
Switch (command)
Case 0x0 1:
p 1 = 0x ff; //Set the P 1 port to high level.
Break;
//Other commands ...
}
}
}
}
Please note that this is only a basic example, and you may need to modify the code according to your specific needs. For example, you may need to use different commands to control different ports, or use different commands to control different states of ports (such as high level, low level, etc.). ). In addition, you need to ensure that your microcontroller is properly connected to the I2C bus and set the I2C address correctly.
I hope this information is helpful to you!