IIC slave mode Implementation Method Based on Huaqing vision generation f051

Source: Internet
Author: User

Instructor Lu,Hua Qing vision embedded college lecturer.

In most cases, MCU is used to control sensors, nodes, and related slave devices. However, in complicated systems, MCU is sometimes used as the slave device.

The following describes how to implement the slave mode of ikef051. The main idea is to accept and process the data sent by the IIC master device from the interruption.

The minimum system of 2017f051:

Configure the IIC from-mode gpio

Check the datasheet of st32f051 Based on the schematic diagram.

Configuration pin:

Void i2c_gpio_configuration (void)
{
Gpio_inittypedefgpio_initstruct;

/* Enable gpioa clock */
Rcc_ahbperiphclockcmd (rcc_ahbperiph_gpiob, enable );
/*! <See_i2c periph clock enable */
Rcc_apb1periphclockcmd (rcc_apb1periph_i2c1, enable );
/*! <Gpio configuration */
/*! <Configure see_i2c pins: SCL */
Gpio_initstruct.gpio_pin = gpio_pin_8;
Gpio_initstruct.gpio_mode = gpio_mode_af; // gpio_mode_in
Gpio_initstruct.gpio_speed = gpio_speed_level_3;
Gpio_initstruct.gpio_otype = gpio_otype_od; // open-Drain
Gpio_init (gpiob, & gpio_initstruct );

/*! <Configure see_i2c pins: SDA */
Gpio_initstruct.gpio_pin = gpio_pin_9;
Gpio_init (gpiob, & gpio_initstruct );

/* Connect pxx to i2c_scl */
Gpio_pinafconfig (gpiob, gpio_pinsource8, gpio_af_1 );
/* Connect pxx to i2c_sda */
Gpio_pinafconfig (gpiob, gpio_pinsource9, gpio_af_1 );
}

When configuring the IIC slave mode, you need to set the address. Here, it is set to 0xa0, and the device clock belongs to the passive mode, which is determined by the IIC master.

Void i2c_configuration (void)
{
I2c_inittypedef i2c_initstruct;

/* I2C configuration */
I2c_initstruct.i2c_mode = i2c_mode_i2c;
I2c_initstruct.i2c_analogfilter = i2c_analogfilter_enable;
I2c_initstruct.i2c_digitalfilter = 0x00;
I2c_initstruct.i2c_ownaddress1 = 0xa0;
I2c_initstruct.i2c_ack = i2c_ack_enable;
I2c_initstruct.i2c_acknowledgedaddress = i2c_acknowledgedaddress_7bit;
I2c_initstruct.i2c_timing = 0xb0420f13; // 100 kbits
/* I2C peripheral enable */
I2c_cmd (i2c1, enable );
/* Apply I2C configuration after enabling it */
I2c_init (i2c1, & i2c_initstruct );
}

The implementation of IIC interrupt functions is as follows:

Void i2c1_irqhandler (void)
{
I2c_clearitpendingbit (i2c1, i2c_isr_addr | i2c_isr_stopf );
If (i2c_getitstatus (i2c1, i2c_it_rxne ));
{
I2cbuf [i2c_it_flag ++] = i2c_receivedata (i2c1 );
}
}

In the interrupt function, there are two types of clear interruptions. One is that the IIC is successfully verified from the address, and the other is that the data is stopped during transmission. For slave devices, receiving data interruptions is what we need most. Here we store the data accepted by IIC into the defined array for future use. Likewise, when reading and receiving data, the hardware has removed the interruption of the accepted data.

Source:Huaqing vision embedded College,Original article address:Http://www.embedu.org/Column/Column912.htm

For more information about embedded systems, seeHua Qing visionInstructor blog>

IIC slave mode Implementation Method Based on Huaqing vision generation f051

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.