[DemoCode下载] MINI51的IIC通信

[复制链接]
1056|5
 楼主| 598330983 发表于 2019-4-30 20:17 | 显示全部楼层 |阅读模式
[Mini51][I2C]Master_Slave_communication.zip (468.99 KB, 下载次数: 7)
 楼主| 598330983 发表于 2019-4-30 20:18 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V0.10
  4. * $Revision: 4 $
  5. * $Date: 13/10/07 3:56p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Mini51 Series I2C Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Mini51Series.h"

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global variables                                                                                        */
  15. /*---------------------------------------------------------------------------------------------------------*/

  16. volatile uint8_t g_u8DeviceAddr;
  17. volatile uint8_t g_u8DataLen;
  18. volatile uint8_t rawlenth;
  19. volatile uint8_t g_au8Reg;
  20. volatile uint8_t g_u8EndFlag = 0;
  21. uint8_t *g_au8Buffer;

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

  23. I2C_FUNC __IO s_I2CHandlerFn = NULL;

  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /*  I2C IRQ Handler                                                                                       */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. void I2C_IRQHandler(void)
  28. {
  29.     uint32_t u32Status;

  30.     u32Status = I2C_GET_STATUS(I2C);

  31.     if (I2C_GET_TIMEOUT_FLAG(I2C))
  32.     {
  33.         /* Clear I2C Timeout Flag */
  34.         I2C_ClearTimeoutFlag(I2C);                  
  35.     }   
  36.     else
  37.     {
  38.         if (s_I2CHandlerFn != NULL)
  39.             s_I2CHandlerFn(u32Status);
  40.     }
  41. }

  42. /*---------------------------------------------------------------------------------------------------------*/
  43. /*  I2C Rx multi Callback Function                                                                               */
  44. /*---------------------------------------------------------------------------------------------------------*/
  45. void I2C_MasterRx_multi(uint32_t u32Status)
  46. {
  47.     if(u32Status == 0x08)                       /* START has been transmitted and prepare SLA+W */
  48.     {
  49.         I2C_SET_DATA(I2C, (g_u8DeviceAddr << 1));    /* Write SLA+W to Register I2CDAT */
  50.         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  51.     }
  52.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  53.     {
  54.         I2C_SET_DATA(I2C, g_au8Reg);
  55.         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  56.     }
  57.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  58.     {
  59.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STA | I2C_STO);
  60.     }
  61.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  62.     {
  63.         if (rawlenth > 0)
  64.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STA);//repeat start
  65.                                 else
  66.                                 {
  67.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STO);
  68.                                         g_u8EndFlag = 1;
  69.                                 }
  70.     }
  71.     else if(u32Status == 0x10)                  /* Repeat START has been transmitted and prepare SLA+R */
  72.     {
  73.         I2C_SET_DATA(I2C, ((g_u8DeviceAddr << 1) | 0x01));   /* Write SLA+R to Register I2CDAT */
  74.         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  75.     }
  76.     else if(u32Status == 0x40)                  /* SLA+R has been transmitted and ACK has been received */
  77.     {
  78.                                 if (rawlenth > 1)
  79.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  80.                                 else
  81.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  82.     }
  83.                 else if(u32Status == 0x50)                  /* DATA has been received and ACK has been returned */
  84.     {
  85.         g_au8Buffer[g_u8DataLen++] = (unsigned char) I2C_GetData(I2C);
  86.         if (g_u8DataLen < (rawlenth-1))
  87.                                 {
  88.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  89.                                 }
  90.                                 else
  91.                                 {
  92.                                         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  93.                                 }
  94.     }
  95.     else if(u32Status == 0x58)                  /* DATA has been received and NACK has been returned */
  96.     {
  97.         g_au8Buffer[g_u8DataLen++] = (unsigned char) I2C_GetData(I2C);
  98.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STO);
  99.         g_u8EndFlag = 1;
  100.     }
  101.     else
  102.     {
  103.         /* TO DO */
  104.         printf("Status 0x%x is NOT processed\n", u32Status);
  105.     }
  106. }
  107. /*---------------------------------------------------------------------------------------------------------*/
  108. /*  I2C Tx multi Callback Function                                                                               */
  109. /*---------------------------------------------------------------------------------------------------------*/
  110. void I2C_MasterTx_multi(uint32_t u32Status)
  111. {
  112.     if(u32Status == 0x08)                       /* START has been transmitted */
  113.     {
  114.         I2C_SET_DATA(I2C, g_u8DeviceAddr << 1);    /* Write SLA+W to Register I2CDAT */
  115.         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  116.     }
  117.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  118.     {
  119.         I2C_SET_DATA(I2C, g_au8Reg);
  120.         I2C_SET_CONTROL_REG(I2C, I2C_SI);
  121.     }
  122.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  123.     {
  124.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STA | I2C_STO);
  125.     }
  126.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  127.     {
  128.         if(g_u8DataLen < rawlenth)
  129.         {
  130.             I2C_SET_DATA(I2C, g_au8Buffer[g_u8DataLen++]);
  131.             I2C_SET_CONTROL_REG(I2C, I2C_SI);
  132.         }
  133.         else
  134.         {
  135.             I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_STO);
  136.             g_u8EndFlag = 1;
  137.         }
  138.     }
  139.     else
  140.     {
  141.         /* TO DO */
  142.         printf("Status 0x%x is NOT processed\n", u32Status);
  143.     }
  144. }

  145. void SYS_Init(void)
  146. {
  147.     /* Unlock protected registers */
  148.     SYS_UnlockReg();

  149. /*---------------------------------------------------------------------------------------------------------*/
  150. /* Init System Clock                                                                                       */
  151. /*---------------------------------------------------------------------------------------------------------*/

  152.     /* Unlock protected registers */
  153.     SYS_UnlockReg();

  154.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  155.     CLK->PWRCON |= CLK_PWRCON_XTL12M | CLK_PWRCON_IRC22M_EN_Msk;

  156.     /* Waiting for clock ready */
  157.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL_STB_Msk | CLK_CLKSTATUS_IRC22M_STB_Msk);

  158.     /* Switch HCLK clock source to XTL, STCLK to XTL */
  159.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_XTAL | CLK_CLKSEL0_HCLK_S_XTAL;

  160.     /* Enable IP clock */        
  161.     CLK->APBCLK = CLK_APBCLK_UART_EN_Msk;
  162.    
  163.     /* Select IP clock source */
  164.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_XTAL;

  165. /*---------------------------------------------------------------------------------------------------------*/
  166. /* Init I/O Multi-function                                                                                 */
  167. /*---------------------------------------------------------------------------------------------------------*/
  168.     /* Set P0 multi-function pins for UART RXD and TXD */
  169.     SYS->P0_MFP = SYS_MFP_P01_RXD | SYS_MFP_P00_TXD;
  170.    
  171.     /* Set P3.4 and P3.5 for I2C SDA and SCL */
  172.     SYS->P3_MFP = SYS_MFP_P34_SDA | SYS_MFP_P35_SCL;
  173.    
  174.     /* Lock protected registers */
  175.     SYS_LockReg();
  176.    
  177.     /* Update System Core Clock */
  178.     SystemCoreClockUpdate();
  179. }

  180. void UART_Init(void)
  181. {
  182. /*---------------------------------------------------------------------------------------------------------*/
  183. /* Init UART                                                                                               */
  184. /*---------------------------------------------------------------------------------------------------------*/
  185.      /* Reset IP */
  186.     SYS->IPRSTC2 |=  SYS_IPRSTC2_UART_RST_Msk;
  187.     SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART_RST_Msk;
  188.    
  189.     UART->BAUD = UART_BAUD_DIV_X_EN_Msk | UART_BAUD_DIV_X_ONE_Msk | (((__XTAL + (115200/2)) / 115200)-2);   
  190.     UART->LCR = 0x3 | (0x0 << UART_LCR_PBE_Pos) | (0x0 << UART_LCR_NSB_Pos) ;
  191. }

  192. void I2C_Init(void)
  193. {   
  194.     /* Open I2C module and set bus clock */
  195.     I2C_Open(I2C, 100000);
  196.    
  197.      /* Set I2C 4 Slave Addresses */            
  198.     I2C_SetSlaveAddr(I2C, 0, 0x15, 0);   /* Slave Address : 0x15 */
  199.     I2C_SetSlaveAddr(I2C, 1, 0x35, 0);   /* Slave Address : 0x35 */
  200.     I2C_SetSlaveAddr(I2C, 2, 0x55, 0);   /* Slave Address : 0x55 */
  201.     I2C_SetSlaveAddr(I2C, 3, 0x75, 0);   /* Slave Address : 0x75 */

  202.     /* Enable I2C interrupt */
  203.     I2C_EnableInt(I2C);
  204.     NVIC_EnableIRQ(I2C_IRQn);
  205. }

  206. void I2C0_Write_SLAVE_multi(uint8_t slvaddr,uint16_t slvraw,uint8_t slvreg,uint8_t *data)
  207. {
  208.                
  209.                                 g_u8DeviceAddr = slvaddr;
  210.                                 rawlenth=slvraw;
  211.                                 g_au8Reg = slvreg;
  212.                                 g_au8Buffer=data;

  213.         g_u8DataLen = 0;
  214.         g_u8EndFlag = 0;

  215.         /* I2C function to write data to slave */
  216.         s_I2CHandlerFn = (I2C_FUNC)I2C_MasterTx_multi;

  217.         /* I2C as master sends START signal */
  218.         I2C_SET_CONTROL_REG(I2C, I2C_STA);

  219.         /* Wait I2C Tx Finish */
  220.         while(g_u8EndFlag == 0);
  221.         g_u8EndFlag = 0;
  222. }

  223. void I2C0_Read_SLAVE_multi(uint8_t slvaddr,uint16_t slvraw,uint8_t slvreg,uint8_t *data)
  224. {
  225.         g_u8DeviceAddr = slvaddr;
  226.                                 rawlenth=slvraw;
  227.         g_au8Reg = slvreg ;
  228.                                 g_au8Buffer=data;

  229.         g_u8EndFlag = 0;
  230.         g_u8DataLen = 0;
  231.        
  232.         /* I2C function to read data from slave */
  233.         s_I2CHandlerFn = (I2C_FUNC)I2C_MasterRx_multi;
  234.         I2C_SET_CONTROL_REG(I2C, I2C_STA);

  235.         /* Wait I2C Rx Finish */
  236.         while(g_u8EndFlag == 0);
  237.        
  238. }

  239. /*---------------------------------------------------------------------------------------------------------*/
  240. /*  Main Function                                                                                          */
  241. /*---------------------------------------------------------------------------------------------------------*/
  242. int32_t main (void)
  243. {
  244.     uint8_t u8Data[10];
  245.                 uint8_t DID,slvreg;
  246.                 uint32_t i;
  247.                 uint8_t *Buffer;
  248.                 uint16_t raw;
  249.                

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

  252.     /* Init UART for printf */
  253.     UART_Init();

  254.     /*
  255.         This sample code sets I2C bus clock to 100kHz. Then, accesses EEPROM 24LC64 with Byte Write
  256.         and Byte Read operations, and check if the read data is equal to the programmed data.
  257.     */
  258.    
  259.     printf("+-------------------------------------------------------+\n");
  260.     printf("|    Mini51 I2C Driver Sample Code with EEPROM 24LC64    |\n");
  261.     printf("+-------------------------------------------------------+\n");
  262.    
  263.     /* Init I2C to access EEPROM */
  264.     I2C_Init();
  265.         
  266.     printf("\n");
  267.     printf("Check I2C Slave(I2C0) is running first!\n");
  268.     printf("Press any key to continue.\n");
  269.     getchar();

  270.     /* Access Slave with no address */
  271.     printf("\n");
  272.                                
  273.                 DID=0x15;
  274.                 slvreg=0x66;
  275.                 raw=10;
  276.                 Buffer=u8Data;               
  277.                 for (i=0;i<raw;i++)
  278.                 {
  279.                         Buffer[i]=1<<(i%8);
  280.                 }
  281.                
  282.     I2C0_Write_SLAVE_multi(DID,raw,slvreg,Buffer);
  283.                 I2C0_Read_SLAVE_multi(DID,raw,slvreg,Buffer);
  284.                
  285.                 /* Compare data */
  286.     for (i=0;i<raw;i++)
  287.                 {
  288.                         if(Buffer[i] != 1<<(i%8))
  289.                         {
  290.                                         printf("I2C Byte Write/Read Failed, Data 0x%x\n", Buffer[i]);
  291. //                                         while(1);
  292.                         }
  293.                         printf("Master Access Slave Register:0x%X  Data:0x%X Test OK\n", (slvreg+i), Buffer[i]);       
  294.                 }
  295.                                
  296.     printf("SLAVE Address test OK.\n");
  297.                
  298.                 s_I2CHandlerFn = NULL;
  299.                
  300.     /* Close I2C */
  301.     I2C_Close(I2C);

  302.     return 0;
  303. }



 楼主| 598330983 发表于 2019-4-30 20:18 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     main.c
  3. * @version  V0.10
  4. * $Revision: 4 $
  5. * $Date: 13/10/07 3:56p $
  6. * @brief    Mini51 Series I2C Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Mini51Series.h"

  13. uint32_t slave_buff_addr;
  14. uint8_t g_u8SlvData[256];
  15. uint8_t g_au8RxData;
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Global variables                                                                                        */
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. uint8_t g_u8DeviceAddr;
  20. uint8_t g_u8DataLen;

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

  22. I2C_FUNC __IO s_I2CHandlerFn = NULL;

  23. /*---------------------------------------------------------------------------------------------------------*/
  24. /*  I2C IRQ Handler                                                                                       */
  25. /*---------------------------------------------------------------------------------------------------------*/
  26. void I2C_IRQHandler(void)
  27. {
  28.     uint32_t u32Status;

  29.     u32Status = I2C_GET_STATUS(I2C);

  30.     if (I2C_GET_TIMEOUT_FLAG(I2C))
  31.     {
  32.         /* Clear I2C Timeout Flag */
  33.         I2C_ClearTimeoutFlag(I2C);                  
  34.     }   
  35.     else
  36.     {
  37.         if (s_I2CHandlerFn != NULL)
  38.             s_I2CHandlerFn(u32Status);
  39.     }
  40. }

  41. /*---------------------------------------------------------------------------------------------------------*/
  42. /*  I2C TRx Callback Function                                                                               */
  43. /*---------------------------------------------------------------------------------------------------------*/
  44. void I2C_SlaveTRx(uint32_t u32Status)
  45. {
  46.     if(u32Status == 0x60)                       /* Own SLA+W has been receive; ACK has been return */
  47.     {
  48.         g_u8DataLen = 0;
  49.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  50.     }
  51.     else if(u32Status == 0x80)                 /* Previously address with own SLA address
  52.                                                    Data has been received; ACK has been returned*/
  53.     {
  54.         g_au8RxData = (unsigned char) I2C_GET_DATA(I2C);
  55.         g_u8DataLen++;

  56.         if(g_u8DataLen == 1)
  57.         {
  58.             slave_buff_addr = g_au8RxData;
  59.         }
  60.         else
  61.         {
  62.             g_u8SlvData[slave_buff_addr+(g_u8DataLen-2)] = g_au8RxData;
  63.         }
  64.                                
  65.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  66.     }
  67.     else if(u32Status == 0xA8)                  /* Own SLA+R has been receive; ACK has been return */
  68.     {
  69.                      I2C_SET_DATA(I2C, g_u8SlvData[slave_buff_addr]);
  70.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  71.     }
  72.                 else if(u32Status == 0xB8)                 /* Data byte in I2CDAT has been transmitted
  73.                                                    ACK has been received */
  74.     {
  75.         slave_buff_addr=slave_buff_addr+1;
  76.                                 I2C_SET_DATA(I2C, g_u8SlvData[slave_buff_addr]);
  77.                                 I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  78.     }
  79.     else if(u32Status == 0xC0)                 /* Data byte or last data in I2CDAT has been transmitted
  80.                                                    Not ACK has been received */
  81.     {
  82.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  83.     }
  84.     else if(u32Status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
  85.                                                    been returned */
  86.     {
  87.         g_u8DataLen = 0;
  88.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  89.     }
  90.     else if(u32Status == 0xA0)                 /* A STOP or repeated START has been received while still
  91.                                                    addressed as Slave/Receiver*/
  92.     {
  93.         g_u8DataLen = 0;
  94.         I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  95.     }
  96.     else
  97.     {
  98.         /* TO DO */
  99.         printf("Status 0x%x is NOT processed\n", u32Status);
  100.     }
  101. }

  102. void SYS_Init(void)
  103. {
  104.     /* Unlock protected registers */
  105.     SYS_UnlockReg();

  106. /*---------------------------------------------------------------------------------------------------------*/
  107. /* Init System Clock                                                                                       */
  108. /*---------------------------------------------------------------------------------------------------------*/

  109.     /* Unlock protected registers */
  110.     SYS_UnlockReg();

  111.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  112.     CLK->PWRCON |= CLK_PWRCON_XTL12M | CLK_PWRCON_IRC22M_EN_Msk;

  113.     /* Waiting for clock ready */
  114.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL_STB_Msk | CLK_CLKSTATUS_IRC22M_STB_Msk);

  115.     /* Switch HCLK clock source to XTL, STCLK to XTL */
  116.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_XTAL | CLK_CLKSEL0_HCLK_S_XTAL;

  117.     /* Enable IP clock */        
  118.     CLK->APBCLK = CLK_APBCLK_UART_EN_Msk;
  119.    
  120.     /* Select IP clock source */
  121.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_XTAL;

  122. /*---------------------------------------------------------------------------------------------------------*/
  123. /* Init I/O Multi-function                                                                                 */
  124. /*---------------------------------------------------------------------------------------------------------*/
  125.     /* Set P0 multi-function pins for UART RXD and TXD */
  126.     SYS->P0_MFP = SYS_MFP_P01_RXD | SYS_MFP_P00_TXD;
  127.    
  128.     /* Set P3.4 and P3.5 for I2C SDA and SCL */
  129.     SYS->P3_MFP = SYS_MFP_P34_SDA | SYS_MFP_P35_SCL;
  130.    
  131.     /* Lock protected registers */
  132.     SYS_LockReg();
  133.    
  134.     /* Update System Core Clock */
  135.     SystemCoreClockUpdate();
  136. }

  137. void UART_Init(void)
  138. {
  139. /*---------------------------------------------------------------------------------------------------------*/
  140. /* Init UART                                                                                               */
  141. /*---------------------------------------------------------------------------------------------------------*/
  142.      /* Reset IP */
  143.     SYS->IPRSTC2 |=  SYS_IPRSTC2_UART_RST_Msk;
  144.     SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART_RST_Msk;
  145.    
  146.     UART->BAUD = UART_BAUD_DIV_X_EN_Msk | UART_BAUD_DIV_X_ONE_Msk | (((__XTAL + (115200/2)) / 115200)-2);   
  147.     UART->LCR = 0x3 | (0x0 << UART_LCR_PBE_Pos) | (0x0 << UART_LCR_NSB_Pos) ;
  148. }

  149. void I2C_Init(void)
  150. {   
  151.     /* Open I2C module and set bus clock */
  152.     I2C_Open(I2C, 100000);
  153.    
  154.      /* Set I2C 4 Slave Addresses */            
  155.     I2C_SetSlaveAddr(I2C, 0, 0x15, 0);   /* Slave Address : 0x15 */
  156.     I2C_SetSlaveAddr(I2C, 1, 0x35, 0);   /* Slave Address : 0x35 */
  157.     I2C_SetSlaveAddr(I2C, 2, 0x55, 0);   /* Slave Address : 0x55 */
  158.     I2C_SetSlaveAddr(I2C, 3, 0x75, 0);   /* Slave Address : 0x75 */

  159.     /* Enable I2C interrupt */
  160.     I2C_EnableInt(I2C);
  161.     NVIC_EnableIRQ(I2C_IRQn);
  162. }

  163. /*---------------------------------------------------------------------------------------------------------*/
  164. /*  Main Function                                                                                          */
  165. /*---------------------------------------------------------------------------------------------------------*/
  166. int32_t main (void)
  167. {
  168.     uint32_t i;

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

  171.     /* Init UART for printf */
  172.     UART_Init();

  173.     /*
  174.         This sample code sets I2C bus clock to 100kHz. Then, accesses EEPROM 24LC64 with Byte Write
  175.         and Byte Read operations, and check if the read data is equal to the programmed data.
  176.     */
  177.    
  178.     printf("+-------------------------------------------------------+\n");
  179.     printf("|    Mini51 I2C Driver Sample Code with EEPROM 24LC64    |\n");
  180.     printf("+-------------------------------------------------------+\n");
  181.    
  182.     /* Init I2C to access EEPROM */
  183.     I2C_Init();
  184.       
  185.                 /* I2C enter no address SLV mode */
  186.     I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
  187.                
  188.                 for(i = 0; i < 0x100; i++)
  189.     {
  190.         g_u8SlvData[i] = 0;
  191.     }
  192.                
  193.                 /* I2C function to write data to slave */
  194.                 s_I2CHandlerFn = I2C_SlaveTRx;

  195.                 printf("\n");
  196.     printf("I2C Slave Mode is Running.\n");

  197.     while(1);
  198. }



heisexingqisi 发表于 2019-4-30 20:49 | 显示全部楼层
IIC学的不好,参考参考。
xuanhuanzi 发表于 2019-4-30 21:20 | 显示全部楼层
以前玩51都是IO模拟
 楼主| 598330983 发表于 2019-5-2 20:40 | 显示全部楼层
函数指针的高级用法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

5575

帖子

22

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