[DemoCode下载] NUC123单片机的I2C从机模式

[复制链接]
2193|6
 楼主| antusheng 发表于 2017-10-23 20:25 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 18 $
  5. * $Date: 16/06/21 7:43p $
  6. * @brief
  7. *           Demonstrate how to set I2C in slave mode to receive the data from a Master.
  8. *           This sample code is EEPROM like, only support 256 bytes.
  9. *           Needs to work with I2C_Master sample code.
  10. * @note
  11. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  12. *
  13. ******************************************************************************/
  14. #include <stdio.h>
  15. #include "NUC123.h"

  16. #define PLLCON_SETTING      CLK_PLLCON_72MHz_HXT
  17. #define PLL_CLOCK           72000000

  18. uint32_t slave_buff_addr;
  19. uint8_t g_au8SlvData[256];
  20. uint8_t g_au8SlvRxData[3];
  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /* Global variables                                                                                        */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. volatile uint8_t g_u8DeviceAddr;
  25. volatile uint8_t g_u8SlvDataLen;

  26. typedef void (*I2C_FUNC)(uint32_t u32Status);

  27. static I2C_FUNC s_I2C0HandlerFn = NULL;

  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /*  I2C0 IRQ Handler                                                                                       */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. void I2C0_IRQHandler(void)
  32. {
  33.     uint32_t u32Status;

  34.     u32Status = I2C_GET_STATUS(I2C0);

  35.     if(I2C_GET_TIMEOUT_FLAG(I2C0))
  36.     {
  37.         /* Clear I2C0 Timeout Flag */
  38.         I2C_ClearTimeoutFlag(I2C0);
  39.     }
  40.     else
  41.     {
  42.         if(s_I2C0HandlerFn != NULL)
  43.             s_I2C0HandlerFn(u32Status);
  44.     }
  45. }

  46. /*---------------------------------------------------------------------------------------------------------*/
  47. /*  I2C TRx Callback Function                                                                              */
  48. /*---------------------------------------------------------------------------------------------------------*/
  49. void I2C_SlaveTRx(uint32_t u32Status)
  50. {
  51.     uint8_t u8data;
  52.    
  53.     if(u32Status == 0x60)                       /* Own SLA+W has been receive; ACK has been return */
  54.     {
  55.         g_u8SlvDataLen = 0;
  56.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  57.     }
  58.     else if(u32Status == 0x80)                 /* Previously address with own SLA address
  59.                                                    Data has been received; ACK has been returned*/
  60.     {
  61.         u8data = (unsigned char) I2C_GET_DATA(I2C0);   
  62.         if(g_u8SlvDataLen < 2)        
  63.         {
  64.             g_au8SlvRxData[g_u8SlvDataLen++] = u8data;        
  65.             slave_buff_addr = (g_au8SlvRxData[0] << 8) + g_au8SlvRxData[1];            
  66.         }
  67.         else
  68.         {
  69.             g_au8SlvData[slave_buff_addr++] = u8data;
  70.             if(slave_buff_addr==256)
  71.             {
  72.                 slave_buff_addr = 0;
  73.             }            
  74.         }            

  75.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  76.     }
  77.     else if(u32Status == 0xA8)                  /* Own SLA+R has been receive; ACK has been return */
  78.     {
  79.         I2C_SET_DATA(I2C0, g_au8SlvData[slave_buff_addr]);
  80.         slave_buff_addr++;
  81.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  82.     }
  83.     else if(u32Status == 0xB8)                  /* Data byte in I2CDAT has been transmitted ACK has been received */
  84.     {
  85.         I2C_SET_DATA(I2C0, g_au8SlvData[slave_buff_addr++]);
  86.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  87.     }   
  88.     else if(u32Status == 0xC0)                 /* Data byte or last data in I2CDAT has been transmitted
  89.                                                    Not ACK has been received */
  90.     {
  91.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  92.     }
  93.     else if(u32Status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
  94.                                                    been returned */
  95.     {
  96.         g_u8SlvDataLen = 0;
  97.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  98.     }
  99.     else if(u32Status == 0xA0)                 /* A STOP or repeated START has been received while still
  100.                                                    addressed as Slave/Receiver*/
  101.     {
  102.         g_u8SlvDataLen = 0;
  103.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  104.     }
  105.     else
  106.     {
  107.         /* TO DO */
  108.         printf("Status 0x%x is NOT processed\n", u32Status);
  109.     }
  110. }

  111. void SYS_Init(void)
  112. {
  113.     /*---------------------------------------------------------------------------------------------------------*/
  114.     /* Init System Clock                                                                                       */
  115.     /*---------------------------------------------------------------------------------------------------------*/

  116.     /* Enable XT1_OUT (PF0) and XT1_IN (PF1) */
  117.     SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;

  118.     /* Enable Internal RC 22.1184MHz clock */
  119.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  120.     /* Waiting for Internal RC clock ready */
  121.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  122.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  123.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  124.     /* Enable external XTAL 12MHz clock */
  125.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  126.     /* Waiting for external XTAL clock ready */
  127.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  128.     /* Set core clock as PLL_CLOCK from PLL */
  129.     CLK_SetCoreClock(PLL_CLOCK);

  130.     /* Enable UART module clock */
  131.     CLK_EnableModuleClock(UART0_MODULE);

  132.     /* Enable I2C0 module clock */
  133.     CLK_EnableModuleClock(I2C0_MODULE);

  134.     /* Select UART module clock source */
  135.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  136.     /*---------------------------------------------------------------------------------------------------------*/
  137.     /* Init I/O Multi-function                                                                                 */
  138.     /*---------------------------------------------------------------------------------------------------------*/

  139.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  140.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  141.     /* Set GPF multi-function pins for I2C0 SDA and SCL */
  142.     SYS->GPF_MFP |= (SYS_GPF_MFP_PF2_I2C0_SDA | SYS_GPF_MFP_PF3_I2C0_SCL);
  143.     SYS->ALT_MFP1 &= ~(SYS_ALT_MFP1_PF2_Msk | SYS_ALT_MFP1_PF3_Msk);
  144.     SYS->ALT_MFP1 |= (SYS_ALT_MFP1_PF2_I2C0_SDA | SYS_ALT_MFP1_PF3_I2C0_SCL);
  145. }

  146. void UART0_Init()
  147. {
  148.     /*---------------------------------------------------------------------------------------------------------*/
  149.     /* Init UART                                                                                               */
  150.     /*---------------------------------------------------------------------------------------------------------*/
  151.     /* Reset IP */
  152.     SYS_ResetModule(UART0_RST);

  153.     /* Configure UART0 and set UART0 Baudrate */
  154.     UART_Open(UART0, 115200);
  155. }


  156. void I2C0_Init(void)
  157. {
  158.     /* Open I2C module and set bus clock */
  159.     I2C_Open(I2C0, 100000);

  160.     /* Get I2C0 Bus Clock */
  161.     printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));

  162.     /* Set I2C 4 Slave Addresses */
  163.     I2C_SetSlaveAddr(I2C0, 0, 0x15, 0);   /* Slave Address : 0x15 */
  164.     I2C_SetSlaveAddr(I2C0, 1, 0x35, 0);   /* Slave Address : 0x35 */
  165.     I2C_SetSlaveAddr(I2C0, 2, 0x55, 0);   /* Slave Address : 0x55 */
  166.     I2C_SetSlaveAddr(I2C0, 3, 0x75, 0);   /* Slave Address : 0x75 */

  167.     /* Set I2C 4 Slave Addresses Mask */
  168.     I2C_SetSlaveAddrMask(I2C0, 0, 0x01);
  169.     I2C_SetSlaveAddrMask(I2C0, 1, 0x04);
  170.     I2C_SetSlaveAddrMask(I2C0, 2, 0x01);
  171.     I2C_SetSlaveAddrMask(I2C0, 3, 0x04);

  172.     /* Enable I2C interrupt */
  173.     I2C_EnableInt(I2C0);
  174.     NVIC_EnableIRQ(I2C0_IRQn);
  175. }

  176. void I2C0_Close(void)
  177. {
  178.     /* Disable I2C0 interrupt and clear corresponding NVIC bit */
  179.     I2C_DisableInt(I2C0);
  180.     NVIC_DisableIRQ(I2C0_IRQn);

  181.     /* Disable I2C0 and close I2C0 clock */
  182.     I2C_Close(I2C0);
  183.     CLK_DisableModuleClock(I2C0_MODULE);

  184. }

  185. /*---------------------------------------------------------------------------------------------------------*/
  186. /*  Main Function                                                                                          */
  187. /*---------------------------------------------------------------------------------------------------------*/
  188. int32_t main(void)
  189. {
  190.     uint32_t i;

  191.     /* Unlock protected registers */
  192.     SYS_UnlockReg();

  193.     /* Init System, IP clock and multi-function I/O */
  194.     SYS_Init();

  195.     /* Init UART0 for printf */
  196.     UART0_Init();

  197.     /* Lock protected registers */
  198.     SYS_LockReg();

  199.     /*
  200.         This sample code sets I2C bus clock to 100kHz. Then, Master accesses Slave with Byte Write
  201.         and Byte Read operations, and check if the read data is equal to the programmed data.
  202.     */

  203.     printf("+----------------------------------------------------+\n");
  204.     printf("|  I2C Driver Sample Code(Slave) for access Slave    |\n");
  205.     printf("|  Needs to work with I2C_Master sample code.        |\n");
  206.     printf("|  I2C Master (I2C0) <---> I2C Slave(I2C0)           |\n");
  207.     printf("| !! This sample code requires two borads to test !! |\n");
  208.     printf("+----------------------------------------------------+\n");

  209.     printf("Configure I2C0 as a slave.\n");
  210.     printf("The I/O connection for I2C0:\n");
  211.     printf("I2C0_SDA(PF.2), I2C0_SCL(PF.3)\n");

  212.     /* Init I2C0 */
  213.     I2C0_Init();

  214.     /* I2C enter no address SLV mode */
  215.     I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);

  216.     for(i = 0; i < 0x100; i++)
  217.     {
  218.         g_au8SlvData[i] = 0;
  219.     }

  220.     /* I2C function to Slave receive/transmit data */
  221.     s_I2C0HandlerFn = I2C_SlaveTRx;

  222.     printf("\n");
  223.     printf("I2C Slave Mode is Running.\n");

  224.     while(1);
  225. }





 楼主| antusheng 发表于 2017-10-23 20:25 | 显示全部楼层
好多人I2C总是弄的不对,建议多看看官方的例子的设置顺序。
 楼主| antusheng 发表于 2017-10-23 20:26 | 显示全部楼层
这里也提供下主机模式的使用方法
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 16 $
  5. * $Date: 16/06/21 7:43p $
  6. * @brief    Demonstrate I2C Master how to access Slave.
  7. *           Needs to work with I2C_Slave sample code.
  8. * @note
  9. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "NUC123.h"

  14. #define PLLCON_SETTING      CLK_PLLCON_72MHz_HXT
  15. #define PLL_CLOCK           72000000


  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Global variables                                                                                        */
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. volatile uint8_t g_u8DeviceAddr;
  20. volatile uint8_t g_au8MstTxData[3];
  21. volatile uint8_t g_u8MstRxData;
  22. volatile uint8_t g_u8MstDataLen;
  23. volatile uint8_t g_u8MstEndFlag = 0;

  24. typedef void (*I2C_FUNC)(uint32_t u32Status);

  25. static volatile I2C_FUNC s_I2C0HandlerFn = NULL;

  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /*  I2C0 IRQ Handler                                                                                       */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. void I2C0_IRQHandler(void)
  30. {
  31.     uint32_t u32Status;

  32.     u32Status = I2C_GET_STATUS(I2C0);

  33.     if(I2C_GET_TIMEOUT_FLAG(I2C0))
  34.     {
  35.         /* Clear I2C0 Timeout Flag */
  36.         I2C_ClearTimeoutFlag(I2C0);
  37.     }
  38.     else
  39.     {
  40.         if(s_I2C0HandlerFn != NULL)
  41.             s_I2C0HandlerFn(u32Status);
  42.     }
  43. }

  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /*  I2C Rx Callback Function                                                                               */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. void I2C_MasterRx(uint32_t u32Status)
  48. {
  49.     if(u32Status == 0x08)                       /* START has been transmitted and prepare SLA+W */
  50.     {
  51.         I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1));    /* Write SLA+W to Register I2CDAT */
  52.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  53.     }
  54.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  55.     {
  56.         I2C_SET_DATA(I2C0, g_au8MstTxData[g_u8MstDataLen++]);
  57.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  58.     }
  59.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  60.     {
  61.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
  62.     }
  63.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  64.     {
  65.         if(g_u8MstDataLen != 2)
  66.         {
  67.             I2C_SET_DATA(I2C0, g_au8MstTxData[g_u8MstDataLen++]);
  68.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  69.         }
  70.         else
  71.         {
  72.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_SI);
  73.         }
  74.     }
  75.     else if(u32Status == 0x10)                  /* Repeat START has been transmitted and prepare SLA+R */
  76.     {
  77.         I2C_SET_DATA(I2C0, ((g_u8DeviceAddr << 1) | 0x01));   /* Write SLA+R to Register I2CDAT */
  78.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  79.     }
  80.     else if(u32Status == 0x40)                  /* SLA+R has been transmitted and ACK has been received */
  81.     {
  82.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  83.     }
  84.     else if(u32Status == 0x58)                  /* DATA has been received and NACK has been returned */
  85.     {
  86.         g_u8MstRxData = (unsigned char) I2C_GET_DATA(I2C0);
  87.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  88.         g_u8MstEndFlag = 1;
  89.     }
  90.     else
  91.     {
  92.         /* TO DO */
  93.         printf("Status 0x%x is NOT processed\n", u32Status);
  94.     }
  95. }
  96. /*---------------------------------------------------------------------------------------------------------*/
  97. /*  I2C Tx Callback Function                                                                               */
  98. /*---------------------------------------------------------------------------------------------------------*/
  99. void I2C_MasterTx(uint32_t u32Status)
  100. {
  101.     if(u32Status == 0x08)                       /* START has been transmitted */
  102.     {
  103.         I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);    /* Write SLA+W to Register I2CDAT */
  104.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  105.     }
  106.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  107.     {
  108.         I2C_SET_DATA(I2C0, g_au8MstTxData[g_u8MstDataLen++]);
  109.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  110.     }
  111.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  112.     {
  113.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
  114.     }
  115.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  116.     {
  117.         if(g_u8MstDataLen != 3)
  118.         {
  119.             I2C_SET_DATA(I2C0, g_au8MstTxData[g_u8MstDataLen++]);
  120.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  121.         }
  122.         else
  123.         {
  124.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  125.             g_u8MstEndFlag = 1;
  126.         }
  127.     }
  128.     else
  129.     {
  130.         /* TO DO */
  131.         printf("Status 0x%x is NOT processed\n", u32Status);
  132.     }
  133. }

  134. void SYS_Init(void)
  135. {
  136.     /*---------------------------------------------------------------------------------------------------------*/
  137.     /* Init System Clock                                                                                       */
  138.     /*---------------------------------------------------------------------------------------------------------*/

  139.     /* Enable XT1_OUT (PF0) and XT1_IN (PF1) */
  140.     SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;

  141.     /* Enable Internal RC 22.1184MHz clock */
  142.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  143.     /* Waiting for Internal RC clock ready */
  144.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  145.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  146.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  147.     /* Enable external XTAL 12MHz clock */
  148.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  149.     /* Waiting for external XTAL clock ready */
  150.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  151.     /* Set core clock as PLL_CLOCK from PLL */
  152.     CLK_SetCoreClock(PLL_CLOCK);

  153.     /* Enable UART module clock */
  154.     CLK_EnableModuleClock(UART0_MODULE);

  155.     /* Enable I2C0 module clock */
  156.     CLK_EnableModuleClock(I2C0_MODULE);

  157.     /* Select UART module clock source */
  158.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  159.     /*---------------------------------------------------------------------------------------------------------*/
  160.     /* Init I/O Multi-function                                                                                 */
  161.     /*---------------------------------------------------------------------------------------------------------*/

  162.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  163.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  164.     /* Set GPF multi-function pins for I2C0 SDA and SCL */
  165.     SYS->GPF_MFP |= (SYS_GPF_MFP_PF2_I2C0_SDA | SYS_GPF_MFP_PF3_I2C0_SCL);
  166.     SYS->ALT_MFP1 &= ~(SYS_ALT_MFP1_PF2_Msk | SYS_ALT_MFP1_PF3_Msk);
  167.     SYS->ALT_MFP1 |= (SYS_ALT_MFP1_PF2_I2C0_SDA | SYS_ALT_MFP1_PF3_I2C0_SCL);
  168. }

  169. void UART0_Init()
  170. {
  171.     /*---------------------------------------------------------------------------------------------------------*/
  172.     /* Init UART                                                                                               */
  173.     /*---------------------------------------------------------------------------------------------------------*/
  174.     /* Reset IP */
  175.     SYS_ResetModule(UART0_RST);

  176.     /* Configure UART0 and set UART0 Baudrate */
  177.     UART_Open(UART0, 115200);
  178. }

  179. void I2C0_Init(void)
  180. {
  181.     /* Open I2C module and set bus clock */
  182.     I2C_Open(I2C0, 100000);

  183.     /* Get I2C0 Bus Clock */
  184.     printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));

  185.     /* Set I2C 4 Slave Addresses */
  186.     I2C_SetSlaveAddr(I2C0, 0, 0x15, 0);   /* Slave Address : 0x15 */
  187.     I2C_SetSlaveAddr(I2C0, 1, 0x35, 0);   /* Slave Address : 0x35 */
  188.     I2C_SetSlaveAddr(I2C0, 2, 0x55, 0);   /* Slave Address : 0x55 */
  189.     I2C_SetSlaveAddr(I2C0, 3, 0x75, 0);   /* Slave Address : 0x75 */

  190.     /* Enable I2C interrupt */
  191.     I2C_EnableInt(I2C0);
  192.     NVIC_EnableIRQ(I2C0_IRQn);
  193. }

  194. void I2C0_Close(void)
  195. {
  196.     /* Disable I2C0 interrupt and clear corresponding NVIC bit */
  197.     I2C_DisableInt(I2C0);
  198.     NVIC_DisableIRQ(I2C0_IRQn);

  199.     /* Disable I2C0 and close I2C0 clock */
  200.     I2C_Close(I2C0);
  201.     CLK_DisableModuleClock(I2C0_MODULE);

  202. }

  203. int32_t I2C0_Read_Write_SLAVE(uint8_t slvaddr)
  204. {
  205.     uint32_t i;

  206.     g_u8DeviceAddr = slvaddr;

  207.     for(i = 0; i < 0x100; i++)
  208.     {
  209.         g_au8MstTxData[0] = (uint8_t)((i & 0xFF00) >> 8);
  210.         g_au8MstTxData[1] = (uint8_t)(i & 0x00FF);
  211.         g_au8MstTxData[2] = (uint8_t)(g_au8MstTxData[1] + 3);

  212.         g_u8MstDataLen = 0;
  213.         g_u8MstEndFlag = 0;

  214.         /* I2C function to write data to slave */
  215.         s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterTx;

  216.         /* I2C as master sends START signal */
  217.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA);

  218.         /* Wait I2C Tx Finish */
  219.         while(g_u8MstEndFlag == 0);
  220.         g_u8MstEndFlag = 0;

  221.         /* I2C function to read data from slave */
  222.         s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterRx;

  223.         g_u8MstDataLen = 0;
  224.         g_u8DeviceAddr = slvaddr;

  225.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA);

  226.         /* Wait I2C Rx Finish */
  227.         while(g_u8MstEndFlag == 0);

  228.         /* Compare data */
  229.         if(g_u8MstRxData != g_au8MstTxData[2])
  230.         {
  231.             printf("I2C Byte Write/Read Failed, Data 0x%x\n", g_u8MstRxData);
  232.             return -1;
  233.         }
  234.     }
  235.     printf("Master Access Slave (0x%X) Test OK\n", slvaddr);
  236.     return 0;
  237. }
  238. /*---------------------------------------------------------------------------------------------------------*/
  239. /*  Main Function                                                                                          */
  240. /*---------------------------------------------------------------------------------------------------------*/
  241. int32_t main(void)
  242. {
  243.     /* Unlock protected registers */
  244.     SYS_UnlockReg();

  245.     /* Init System, IP clock and multi-function I/O */
  246.     SYS_Init();

  247.     /* Init UART0 for printf */
  248.     UART0_Init();

  249.     /* Lock protected registers */
  250.     SYS_LockReg();

  251.     /*
  252.         This sample code sets I2C bus clock to 100kHz. Then, Master accesses Slave with Byte Write
  253.         and Byte Read operations, and check if the read data is equal to the programmed data.
  254.     */
  255.     printf("\n");
  256.     printf("+----------------------------------------------------+\n");
  257.     printf("| I2C Driver Sample Code(Master) for access Slave    |\n");
  258.     printf("| Needs to work with I2C_Slave sample code           |\n");
  259.     printf("|     I2C Master (I2C0) <---> I2C Slave(I2C0)        |\n");
  260.     printf("| !! This sample code requires two borads to test !! |\n");
  261.     printf("+----------------------------------------------------+\n");

  262.     printf("Configure I2C0 as a master.\n");
  263.     printf("The I/O connection for I2C0:\n");
  264.     printf("I2C0_SDA(PF.2), I2C0_SCL(PF.3)\n");

  265.     /* Init I2C0 */
  266.     I2C0_Init();

  267.     printf("\n");
  268.     printf("Check I2C Slave(I2C0) is running first!\n");
  269.     printf("Press any key to continue.\n");
  270.     getchar();

  271.     /* Access Slave with no address */
  272.     printf("\n");
  273.     printf(" == No Mask Address ==\n");
  274.     I2C0_Read_Write_SLAVE(0x15);
  275.     I2C0_Read_Write_SLAVE(0x35);
  276.     I2C0_Read_Write_SLAVE(0x55);
  277.     I2C0_Read_Write_SLAVE(0x75);
  278.     printf("SLAVE Address test OK.\n");

  279.     /* Access Slave with address mask */
  280.     printf("\n");
  281.     printf(" == Mask Address ==\n");
  282.     I2C0_Read_Write_SLAVE(0x15 & ~0x01);
  283.     I2C0_Read_Write_SLAVE(0x35 & ~0x04);
  284.     I2C0_Read_Write_SLAVE(0x55 & ~0x01);
  285.     I2C0_Read_Write_SLAVE(0x75 & ~0x04);
  286.     printf("SLAVE Address Mask test OK.\n");

  287.     s_I2C0HandlerFn = NULL;

  288.     /* Close I2C0 */
  289.     I2C0_Close();

  290.     while(1);
  291. }
21mengnan 发表于 2017-10-23 20:49 | 显示全部楼层
这个看得懂,是用的硬件收发器,库函数实现果然很方便。
捉虫天师 发表于 2017-10-23 21:02 | 显示全部楼层
为何设置个这都这么难,难道我比较笨。
huangcunxiake 发表于 2017-10-23 21:24 | 显示全部楼层
u32Status == 0x88
这些如果都用宏就好懂了,要不这个数字真没法理解。
zhuotuzi 发表于 2017-10-24 18:54 | 显示全部楼层

    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Enable I2C0 module clock */
    CLK_EnableModuleClock(I2C0_MODULE);
是不是开启了这个时钟相当于激活了这个模块,就开始耗电了,如果不启动这个时钟,是不是就是没有供电。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

86

主题

1723

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部