Thông báo

Collapse
No announcement yet.

Cờ báo bận đường truyền I2C giữa 2 STM8

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Cờ báo bận đường truyền I2C giữa 2 STM8

    Xin chào mọi người, mình thực hiện giao tiếp I2C giữa 2 vdk stm8, sau khi thực hiện giao tiếp thành công master nhận, slave truyền, đã gửi lệnh stop condition , muốn giao tiếp lại với slave thì đường truyền đều bán đang bận
    while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY)); anh chị em nào có thể giải thích giúp lí do được không được không .
    Rất chân thành cám ơn

  • #2
    Đã send ack chưa ???????

    Comment


    • #3
      Em đã thêm send ack ạ, kết quả vẫn thế ạ, Anh có thể chỉ giúp em lỗi được ko ạ, sau mỗi lần nhận được vẫn báo bận đường truyền.
      Chân thành cám ơn ạ.
      Attached Files

      Comment


      • #4
        Code đây ạ.
        #include "stm8s.h"
        #include "main.h"
        #include "stm8s_i2c.h"

        #define LED_GPIO_PORT (GPIOD)
        #define LED_GPIO_PINS (GPIO_PIN_2)
        /* Define PB*/
        #define LED_GPIO_PORTB (GPIOB)

        /* Define PB4 PB5 */
        #define LED_GPIO_PINSB4 (GPIO_PIN_4)
        #define LED_GPIO_PINSB5 (GPIO_PIN_5)

        /* Private macro -------------------------------------------------------------*/
        extern uint8_t HEADER_ADDRESS_Read = (((SLAVE_ADDRESS & 0xFF00) >> 7) | 0xF1);
        extern uint8_t HEADER_ADDRESS_Write;
        /* Private variables ---------------------------------------------------------*/
        __IO uint8_t Rx_Idx = 0, Tx_Idx = 0,flag,B4,B5;
        uint8_t i = 0;
        __IO uint8_t NumByteToRead = BUFFERSIZE; // So luong byte can phai doc
        __IO uint8_t NumOfBytes = BUFFERSIZE;
        __IO uint8_t RxBuffer[BUFFERSIZE];

        extern __IO uint8_t TxBuffer[BUFFERSIZE];
        /* Private function prototypes -----------------------------------------------*/
        void Delay(__IO uint32_t nCount);

        void main()
        {
        GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS|(GPIO_Pin_TypeDef) LED_GPIO_PINSB4|(GPIO_Pin_TypeDef)LED_GPIO_PINSB5, GPIO_MODE_OUT_PP_LOW_FAST);

        #ifdef FAST_I2C_MODE
        /* system_clock / 1 */
        CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
        #else
        /* system_clock / 2 */
        CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
        #endif


        /* I2C Initialize */
        I2C_Init(I2C_SPEED, 0xA0, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);


        /* Enable Buffer and Event Interrupt*/
        // I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);

        // enableInterrupts();


        while(1)
        {
        Delay(0xFFFF);
        flag = !I2C_GetFlagStatus(I2C_FLAG_BUSBUSY);
        while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

        /* Send START condition */
        I2C_GenerateSTART(ENABLE);
        // while (NumOfBytes);
        //while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

        /* Add a delay to be sure that communication is finished */
        Delay(0xFFFF);

        /***** reception phase ***/
        /* Wait while the bus is busy */
        // while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
        B4 = LED_GPIO_PINSB4;
        B5 = LED_GPIO_PINSB4;
        /* Send START condition */
        I2C_GenerateSTART(ENABLE);

        /* Test on EV5 and clear it */
        while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

        /* Send slave Address for write */
        I2C_Send7bitAddress(SLAVE_ADDRESS, I2C_DIRECTION_RX);

        /* Test on EV6 and clear it */
        while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SE LECTED));
        /* #endif TEN_BITS_ADDRESS */

        /* While there is data to be read */
        while (NumByteToRead)
        {
        if (NumByteToRead == 1)
        {
        if (I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED) )
        {
        RxBuffer[Rx_Idx] = I2C_ReceiveData();
        I2C_AcknowledgeConfig(I2C_ACK_CURR);
        if(RxBuffer[Rx_Idx]==Rx_Idx+1)
        {
        GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
        }
        Rx_Idx++; /* Point to the next location where the byte read will be saved */
        NumByteToRead--; /* Decrement the read bytes counter */
        I2C_AcknowledgeConfig(I2C_ACK_NONE);/* Disable Acknowledgement */
        I2C_GenerateSTOP(ENABLE); /* Send STOP Condition */
        }
        }
        else
        {
        if (I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED) )
        {
        RxBuffer[Rx_Idx] = I2C_ReceiveData();/* Read a byte from the SLVAVE */
        I2C_AcknowledgeConfig(I2C_ACK_CURR);
        if(RxBuffer[Rx_Idx]==Rx_Idx+1)
        {
        GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
        }
        Rx_Idx++; /* Point to the next location where the byte read will be saved */
        NumByteToRead--; /* Decrement the read bytes counter */
        }
        }

        }

        //while(1);
        }

        /* check if sent and received data are not corrupted */

        }

        /**
        * @brief Compares two buffers.
        * @param pBuffer1, pBuffer2: buffers to be compared.
        * @param BufferLength: buffer's length
        * @retval PASSED: pBuffer1 identical to pBuffer2
        * FAILED: pBuffer1 differs from pBuffer2
        */

        /**
        * @brief Inserts a delay time.
        * @param nCount: specifies the delay time length.
        * @retval None
        */
        void Delay(__IO uint32_t nCount)
        {
        for (; nCount != 0; nCount--);
        }

        #ifdef USE_FULL_ASSERT
        /**
        * @brief Reports the name of the source file and the source line number
        * where the assert_param error has occurred.
        * @param file: pointer to the source file name
        * @param line: assert_param error line source number
        * @retval None
        */
        void assert_failed(uint8_t* file, uint32_t line)
        {
        /* User can add his own implementation to report the file name and line number,
        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

        /* Infinite loop */
        while (1)
        {}
        }
        #endif

        /**
        * @}
        */

        /**
        * @}
        */


        /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

        Comment

        Về tác giả

        Collapse

        thuanbkhn Tìm hiểu thêm về thuanbkhn

        Bài viết mới nhất

        Collapse

        Đang tải...
        X