Arduino brief notes i2c iic character conversion

Source: Internet
Author: User

About I2C, this is also the previously tangled part. Note.

(For details, refer to: source; Phodal's
Blog)

The I2C (Inter-Integrated Circuit) bus is a two-line serial bus developed by PHILIPS to connect the microcontroller and its peripheral devices. It is a bus standard widely used in the field of microelectronics communication control. > The I2C bus supports any IC production process (nmos cmos, bipolar ). Two-wire-the serial data (SDA) and serial clock (SCL) lines transmit information between devices connected to the bus. Each device has a unique address recognition (whether it is a microcontroller-MCU, LCD driver, memory or keyboard interface ), it can also be used as a transmitter or receiver (determined by the features of the device ). Obviously, the LCD driver is only a receiver, while the memory can receive and send data. Number of devices executed except the transmitter and receiver
Data transmission can also be viewed as a host or slave (see table 1 ). A host is a device that initializes bus data transmission and generates clock signals that can be transmitted. In this case, any addressable device is considered as a slave device.

II2, or I2C, is used in arduino. > Host program example

# Include Wire. h & gt; // declare the I2C library file # define LED 13 byte x = 0; // variable x determines the LED light-off // initialize void setup () {Wire. begin (); // Add the i2c bus as the host pinMode (LED, OUTPUT); // set the number Port 13 to the OUTPUT} // The main program void loop () {Wire. beginTransmission (4); // send data to the slave Wire with device number 4. send ("light is"); // send the string "light is" Wire. send (x); // send a byte Wire in variable x. endTransmission (); // stop sending x ++; // variable x plus 1 if (x = 2) // if the value of variable x is 2, convert x to 0 x = 0; delay (1000); // delay 1 s Wire. requestFr Om (4, 1); // indicates that 1 byte while (Wire. available () & gt; 0) // when the host receives the Slave Data, {byte c = Wire. receive (); // receives a byte value assigned to c // determines that c is 1, then the LED is lit; otherwise, the LED is extinguished. If (c = 1) {digitalWrite (LED, LOW) ;}else {digitalWrite (LED, HIGH) ;}} delay (1000); // latency 1 s}

Brief section of the Arduino slave program

# Include Wire. h & gt; // declare the I2C library file int x; // variable x determines whether the host's LEDs are lit. // initialize void setup () {Wire. begin (4); // Add the i2c bus and set the slave address to #4 Wire. onReceive (receiveEvent); // register the event Wire that receives host characters. onRequest (requestEvent); // register the host to notify the event Serial of data uploaded from the slave. begin (9600); // set the serial port baud rate} // main program void loop () {delay (100); // latency} // when the slave receives host characters, execute this event void receiveEvent (int howevent) {while (Wire. available () & gt; 1) // executes cyclically until the packet contains only the last character {char c = Wire. receive (); // receives the byte Serial as a character. print (c); // print the character to the serial monitor} // receives the last byte x = Wire in the packet sent by the host. receive (); // receives the byte Serial as an integer. println (x); // print the integer to the serial monitor, and press ENTER} // when the host notifies the slave to upload data, execute this event void requestEvent () {// send the last byte of the packet sent by the receiving host to the host Wire. send (x); // sends a byte data to the host in response to the notification from the host}

I2C is mainly dependent on the wire. h library.

Arduino itoa is mainly used for int to char, that is, converting text into characters.

> Itoa is a widely used non-standard C Language extension function. Because it is not a standard C function, it cannot be used in all compilers. However, most compilers (such as those on Windows) usually include this function in header files. The function with the opposite function is atoi. Function: converts an integer to a string.

The usage is as follows:

char *itoa(int value, char *string, int radix);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.