[技术问答] I2C这个switch case的变量I2C0STAT是寄存器吗?

[复制链接]
950|4
 楼主| 捉虫天师 发表于 2024-1-17 15:18 | 显示全部楼层 |阅读模式
I2C这个switch case的变量I2C0STAT是寄存器吗?
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* SPDX-License-Identifier: Apache-2.0                                                                     */
  4. /* Copyright(c) 2020 Nuvoton Technology Corp. All rights reserved.                                         */
  5. /*                                                                                                         */
  6. /*---------------------------------------------------------------------------------------------------------*/

  7. #include "ml51_iar.h"
  8. #include "Table_Data.h"
  9. //***********************************************************************************************************
  10. //  File Function: MUG51 I2C master mode demo code, the Slave address = 0xA4
  11. //
  12. //   ____________            _____________
  13. //  |            |   SDA    |             |
  14. //  |            |<-------->|             |
  15. //  |            |          |             |
  16. //  |ML51(M)     |          | ML51(S)     |
  17. //  |(I2C_Master)|          | (I2C_Slave) |
  18. //  |            |   SCL    |             |
  19. //  |            |--------->|             |
  20. //  |____________|          |_____________|
  21. //
  22. //  The protocol of I2C is master: start -> write 10 byte(ACK) ->stop -> start ->read 10 byte(ACK) -> stop
  23. //***********************************************************************************************************

  24. #define EEPROM_ADDRESS          0xA0
  25. #define I2C_WR                     0
  26. #define I2C_RD                     1

  27. #define LOOP_SIZE                 10

  28. unsigned int Tx_Addr = 0;
  29. unsigned char Tx_Dat = 0;
  30. unsigned int Rx_Addr = 0;
  31. unsigned char Rx_Dat[30];
  32. BIT Write_End_Flag, Read_End_Flag,i2c_Error_Flag;;


  33. void (*I2C_Func)(void);
  34. #pragma vector=0x33
  35. __interrupt void I2C0_ISR(void){
  36.     I2C_Func();
  37. }

  38. /*=====  I2C master tranfer to eeprom process  ===========================*/
  39. void I2C0_Master_Tx_Isr(void)
  40. {
  41.     static uint8_t addr_flag = 0;
  42.     static uint8_t u8Count = 0;

  43. PUSH_SFRS;
  44.     SFRS = 0;

  45.     switch (I2C0STAT)
  46.     {
  47.        /* Bus error */
  48.        case 0x00: set_I2C0CON_STO; break;
  49.         
  50.       /* I2C start */
  51.        case 0x08:
  52.             clr_I2C0CON_STA;
  53.             I2C0DAT = (EEPROM_ADDRESS | I2C_WR);
  54.        break;

  55.        /* Master Transmit Address ACK (to transmit the eeprom address high byte) */
  56.        case 0x18:
  57.             I2C0DAT = HIBYTE(Tx_Addr);
  58.             addr_flag = 1;
  59.        break;

  60.        /* Master Transmit Data ACK (to transmit the eeprom address low byte) */
  61.        case 0x28:
  62.             if(addr_flag)
  63.             {
  64.                 I2C0DAT = LOBYTE(Tx_Addr);
  65.                 addr_flag = 0;
  66.                 u8Count = 0;
  67.             }
  68.             else
  69.             {
  70.                 if(u8Count != 30)
  71.                 {
  72.                     I2C0DAT = Table_Data[u8Count];
  73.                     u8Count++;
  74.                 }
  75.                 else
  76.                 {
  77.                     Write_End_Flag = 1;
  78.                     set_I2C0CON_STO;
  79.                 }
  80.             }
  81.         break;
  82.     }

  83.     I2C0_SI_Check();

  84. POP_SFRS;
  85. }

  86. /*======== I2C master read from eeprom process======================================*/
  87. void I2C0_Master_Rx_Isr(void)
  88. {
  89.     static uint8_t addr_flag = 0;
  90.     static uint8_t u8Count = 0;
  91. PUSH_SFRS;

  92.     SFRS = 0;

  93.     switch (I2C0STAT)
  94.     {
  95.        /* Bus error */
  96.        case 0x00: set_I2C0CON_STO; break;

  97.       /* I2C start */
  98.        case 0x08:
  99.             clr_I2C0CON_STA;
  100.             I2C0DAT = (EEPROM_ADDRESS | I2C_WR);
  101.        break;

  102.        /* Master Transmit Address ACK (to transmit the eeprom address high byte) */
  103.        case 0x18:
  104.             I2C0DAT = HIBYTE(Rx_Addr);
  105.             addr_flag = 1;
  106.        break;

  107.        /* Master Transmit Data ACK  (to transmit the eeprom address low byte) */
  108.        case 0x28:
  109.             if(addr_flag)
  110.             {
  111.                 I2C0DAT = LOBYTE(Rx_Addr);
  112.                 addr_flag = 0;
  113.                 u8Count = 0;
  114.             }
  115.             else
  116.             {
  117.                 set_I2C0CON_STA;
  118.             }
  119.        break;  

  120.        /* Master Repeat Start  */
  121.        case 0x10:
  122.            clr_I2C0CON_STA;
  123.            I2C0DAT = (EEPROM_ADDRESS | I2C_RD);
  124.        break;

  125.       /* Master Receive Address ACK  */
  126.        case 0x40:  set_I2C0CON_AA; break;
  127.       
  128.       /* Master Receive Data ACK  */  /*I2C master read from eeprom   */
  129.        case 0x50:
  130.                  if(u8Count != 30)
  131.                 {
  132.                     Rx_Dat[u8Count] = I2C0DAT;
  133.                     u8Count++;
  134.                 }
  135.                 else
  136.                 {
  137.                     Read_End_Flag = 1;
  138.                     set_I2C0CON_STO;
  139.                 }
  140.        break;
  141.     }

  142.     I2C0_SI_Check();

  143. POP_SFRS;
  144. }


  145. //========================================================================================================
  146. unsigned char I2C0_Write(unsigned int u16I2Caddr)
  147. {
  148.     unsigned long count = 0;

  149.     Write_End_Flag = 0;
  150.     I2C_Func = I2C0_Master_Tx_Isr;
  151.     Tx_Addr = u16I2Caddr;

  152.     set_I2C0CON_STA;             /* Start transmit */
  153.     while(1)
  154.     {
  155.         count++;
  156.         if(Write_End_Flag == 1)
  157.         {
  158.             return 1;
  159.         }
  160.         
  161.         if(count > 100000)
  162.         {
  163.             return 0;
  164.         }
  165.     }
  166. }

  167. /*========================================================================================================*/
  168. unsigned char I2C0_Read(unsigned int u8I2Caddr)
  169. {
  170.     uint32_t count = 0;
  171.     Read_End_Flag = 0;
  172.     I2C_Func = I2C0_Master_Rx_Isr;
  173.     Rx_Addr = u8I2Caddr;
  174.   
  175.     set_I2C0CON_STA;
  176.    
  177.     while(1)
  178.     {
  179.         count++;
  180.         if(Read_End_Flag == 1)
  181.         {
  182. //            *u8I2Cdat = Rx_Dat;
  183.             return 1;
  184.         }
  185.         
  186.         if(count > 100000)
  187.         {
  188.             return 0;
  189.         }
  190.     }
  191. }
  192. /*========================================================================================================*/
  193. void Init_I2C(void)
  194. {
  195.     /* Set I2C GPIO */
  196.     MFP_P41_I2C0_SCL;
  197.     MFP_P40_I2C0_SDA;
  198.     GPIO_SetMode(Port4, SET_BIT0|SET_BIT1, GPIO_MODE_OPENDRAIN);      /* External pull high resister in circuit */
  199.     GPIO_SchmittTrigger(Port4, SET_BIT0|SET_BIT1, ENABLE);            /* Setting Schmitt Trigger type input */

  200.     /* Set I2C clock rate and enable*/
  201.      I2C_Master_Open(I2C0,24000000,100000);
  202.     /* Set I2C Interrupt enable*/
  203.      I2C_Interrupt(I2C0, ENABLE);
  204.      Global_Interrupt(ENABLE);
  205. }

  206. /*========================================================================================================*/
  207. void main(void)
  208. {
  209.     unsigned char u8Count=0;

  210.   
  211.     Enable_UART0_VCOM_printf();
  212.     printf("\n\r  I2C EEPROM intial...");
  213.     Init_I2C();  
  214.    

  215.         if(I2C0_Write(0x3000) == 1)
  216.         {
  217.             Timer0_Delay(24000000,50,1000);
  218.             while (I2C0_Read(0x3000) ==0);
  219.         }
  220.         for (u8Count=0;u8Count<32;u8Count++)
  221.         {
  222.           if(Rx_Dat[u8Count] != Table_Data[u8Count])
  223.           {
  224.                 SFRS=0; printf("\n\r  Read Byte FAIL! %x", u8Count);
  225.                 i2c_Error_Flag =1;
  226.           }
  227.           else
  228.           {
  229.                  SFRS=0; printf("\n\r  Read Byte PASS! %x", u8Count);
  230.           }
  231.           Timer0_Delay(24000000,10,1000);
  232.         }

  233.         I2C_Close(I2C0);
  234.         if (i2c_Error_Flag)
  235.         {
  236.            printf("\n\r  ");
  237.            printf("\n\r  I2C EEPROM W/R Fail !");
  238.            i2c_Error_Flag=0;
  239.         }

  240.         while(1);

  241. }



huahuagg 发表于 2024-1-17 15:22 | 显示全部楼层
7755465a780470420e.png
是的呢,这个寄存器在不同状态的数值不同。
huahuagg 发表于 2024-1-17 15:26 | 显示全部楼层
yiy 发表于 2024-1-18 23:15 | 显示全部楼层
是的,一个状态寄存器,读取状态判断。
埃娃 发表于 2024-1-29 10:55 来自手机 | 显示全部楼层
寄存器值
您需要登录后才可以回帖 登录 | 注册

本版积分规则

213

主题

3276

帖子

7

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