STM32 is set as I2C slave

Publisher:丹青妙手Latest update time:2018-04-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Hardware platform: STM32F401 

Editor: keil 5.18 

Operating system: win7


1. I2C Protocol 

When transmitting data, the SDA line must remain stable during the high level period of the clock. The high or low level state of SDA can only change when the clock signal of the SCL line is low.


Start and Stop Conditions 

  When the SCL line is high, the SDA line switches from high to low, which indicates the start condition; 

  When the SCL line is high, the SDA line switches from low to high, which indicates a stop condition.


Addressing mode (7/10-bit address mode)


  The first 7 bits of the first byte form the slave address, and the least significant bit (LSB) is the 8th bit, which determines the direction of the transmission of the 7-bit address format of the ordinary and repeated start conditions. The least significant bit of the first byte is "0", indicating that the host will write information to the selected slave; "1" indicates that the host will read information from the slave. When an address is sent, each device in the system compares the first 7 bits with its own address after the start condition. If the same, the device will determine that it is addressed by the master. Whether it is a slave receiver or a slave transmitter is determined by the R/W bit.


2. I2C Slave Mode 

1. Set the i2c slave address


I2C_HandleTypeDef I2cHandle;


I2cHandle.Instance = I2C1;

I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

I2cHandle.Init.ClockSpeed ​​= 400000;

I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;

I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

I2cHandle.Init.OwnAddress1 = I2C_ADDRESS;

I2cHandle.Init.OwnAddress2 = 0xFE;   


As mentioned above, I2C has two address modes. Here, I2C_ADDRESSINGMODE_7BIT sets I2C to slave address mode 7. OwnAddress1 is the I2C address of this device, that is, the slave address we set ourselves. OwnAddress2 does not need to be set.

2. I2C pin and interrupt initialization


void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)

{  

    GPIO_InitTypeDef GPIO_InitStruct;


    /* Enable GPIO TX/RX clock */

    I2Cx_SCL_GPIO_CLK_ENABLE();

    I2Cx_SDA_GPIO_CLK_ENABLE();

    /* Enable I2C1 clock */

    I2Cx_CLK_ENABLE(); 


    /* Configure peripheral GPIO */  

    /* I2C TX GPIO pin configuration */

    GPIO_InitStruct.Pin = I2Cx_SCL_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed ​​= GPIO_SPEED_FAST;

    GPIO_InitStruct.Alternate = I2Cx_SCL_AF;


    HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);


    /* I2C RX GPIO pin configuration */

    GPIO_InitStruct.Pin = I2Cx_SDA_PIN;

    GPIO_InitStruct.Alternate = I2Cx_SDA_AF;


    HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);


    /* Configure the NVIC for I2C */   

    /* NVIC for I2C1 */

    HAL_NVIC_SetPriority(I2Cx_ER_IRQn, 0, 2);

    HAL_NVIC_EnableIRQ(I2Cx_ER_IRQn);

    HAL_NVIC_SetPriority(I2Cx_EV_IRQn, 0, 1);

    HAL_NVIC_EnableIRQ(I2Cx_EV_IRQn);

}


3. From sending


uint8_t aTxBuffer[] = "....hello world....";


while (1)

    if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE+1)!= HAL_OK)

    {

        /* Transfer error in transmission process */

        Error_Handler();    

    }

    HAL_Delay(100);


    while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY)

    {

        Error_Handler();    

    }

}


That’s it.


Keywords:STM32 Reference address:STM32 is set as I2C slave

Previous article:STM32's own IIC configuration
Next article:I2C communication between two STM32 chips

Recommended ReadingLatest update time:2024-11-16 14:28

stm32 GPIO port configuration operation
         The most basic idea in stm32 is that before using the corresponding registers of the peripherals, the clock that controls the corresponding registers must be turned on. Readers can check the corresponding registers controlled by the corresponding clock in the technical manual.            Here we first turn on
[Microcontroller]
Solution to the problem that STM32 can no longer burn after setting the idle pins to analog state
Use STM32's isp download. Set the boot to ISP download mode, and then use the FLYMCU tool to perform ISP download. The new program cannot set the pins to analog state. Otherwise, the pins cannot be recognized when downloading using other methods.
[Microcontroller]
STM32+W5500+MQTT+Android realizes remote data acquisition and control
0 Introduction I've been learning MQTT recently and found that MQTT is quite useful, so I spent some time making a simple application example, hoping to give some reference to those who need to do this. Related background knowledge: http://www.embed-net.com/thread-224-1-1.html The specific functions are: 1. STM32F
[Microcontroller]
STM32+W5500+MQTT+Android realizes remote data acquisition and control
STM32 interrupt source location
In ...\CMSIS\Device\ST\STM32F10x, find the code snippet corresponding to the microcontroller model in the structure IRQn_Type. For example, STM32F103C8T6 belongs to STM32F10X_MD, and the corresponding interrupt source is: For example, STM32F103ZET6 corresponds to STM32F10X_HD
[Microcontroller]
STM32 interrupt source location
STM32 Getting Started: GPIO
       I have been using STM32 for a while. I felt it was difficult to get started and I didn't know where to start. Now I have learned a little bit and I would like to share it with you.        First of all, what is GPIO? The answer to this question is that I don’t know either! At least I don’t need to know it for no
[Microcontroller]
Detailed explanation of the meaning of the five states of FLASH_Status in STM32
Get status: FLASH_Status FLASH_GetStatus(void); The return value is defined by the enumeration type.  typedef enum {     FLASH_BUSY = 1, //Busy    FLASH_ERROR_PG, //Programming error    FLASH_ERROR_WRP, //Write protection error    FLASH_COMPLETE, //Operation completed    FLASH_TIMEOUT //Operation timeout  } FLASH_S
[Microcontroller]
Problems encountered during stm32 transplantation
1.Warning: L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.)    In the Linker---Misc controls of Options for Target, add the entry address: --entry Reset_Handler  2. Error: L6915E: Library reports error: __use_no_semihosting was requested, but __user_initial_stackheap
[Microcontroller]
Problems encountered during stm32 transplantation
EMS LCD touch screen design based on STM32
0 Introduction Electric vehicles have always been a focus of attention for their cleanliness and environmental protection. With the intensification of the energy crisis and the continuous rise in oil prices, electric vehicles are becoming more and more popular among users. Electric vehicles are generally powere
[Microcontroller]
EMS LCD touch screen design based on STM32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号