[DemoCode下载] N76E003单片机的I2C操作方式

[复制链接]
6142|6
 楼主| huahuagg 发表于 2017-10-23 20:28 | 显示全部楼层 |阅读模式
这个芯片最近成为了风云芯片,大家来学习啊
主机模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/29/2016
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 I2C master mode demo code, the Slave address = 0xA4
  14. //
  15. //   ____________            _____________
  16. //  |            |   SDA    |             |
  17. //  |            |<-------->|             |
  18. //  |            |          |             |
  19. //  |N76E003(M) |          | N76E003(S) |
  20. //  |(I2C_Master)|          | (I2C_Slave) |
  21. //  |            |   SCL    |             |
  22. //  |            |--------->|             |
  23. //  |____________|          |_____________|
  24. //
  25. //  The protocol of I2C is same the "24LC64"
  26. //***********************************************************************************************************


  27. #include <stdio.h>
  28. #include "N76E003.h"
  29. #include "Common.h"
  30. #include "Delay.h"
  31. #include "SFR_Macro.h"
  32. #include "Function_Define.h"

  33. #define I2C_CLOCK               13
  34. #define EEPROM_SLA              0xA4
  35. #define EEPROM_WR               0
  36. #define EEPROM_RD               1
  37. #define ERROR_CODE              0x78
  38. #define PAGE_SIZE               32

  39. //========================================================================================================
  40. void Init_I2C(void)
  41. {
  42. //    /* Set I2C clock rate */
  43.     I2CLK = I2C_CLOCK;

  44.     /* Enable I2C */
  45.     set_I2CEN;                                   
  46. }
  47. //========================================================================================================
  48. void I2C_Error(void)
  49. {
  50. //    P3 = I2STAT;
  51. //    P3 = ERROR_CODE;
  52.     while (1);   
  53. }
  54. //========================================================================================================
  55. void I2C_Process(UINT8 u8DAT)
  56. {
  57.     UINT32 u32Count;

  58. //--------------------------------------------------------------------------------------------
  59. //----  Page Write----------------------------------------------------------------------------
  60. //--------------------------------------------------------------------------------------------
  61.     /* Step1 */
  62.     set_STA;                                    /* Send Start bit to I2C EEPROM */
  63.     clr_SI;
  64.     while (!SI);                                //Check SI set or not
  65.     if (I2STAT != 0x08)                         //Check status value after every step
  66.         I2C_Error();
  67.    
  68.     /* Step2 */
  69.     clr_STA;                                    //STA=0
  70.     I2DAT = (EEPROM_SLA | EEPROM_WR);
  71.     clr_SI;
  72.     while (!SI);                                //Check SI set or not
  73.     if (I2STAT != 0x18)              
  74.         I2C_Error();

  75.     /* Step3 */
  76.     I2DAT = 0x00;                               //address high for I2C EEPROM
  77.     clr_SI;
  78.     while (!SI);                                //Check SI set or not
  79.     if (I2STAT != 0x28)              
  80.         I2C_Error();
  81.                     
  82.     /* Step4 */
  83.     I2DAT = 0x00;                               //address low for I2C EEPROM
  84.     clr_SI;
  85.     while (!SI);                                //Check SI set or not
  86.     if (I2STAT != 0x28)              
  87.         I2C_Error();

  88.     /* Step5 */
  89.     for (u32Count = 0; u32Count < PAGE_SIZE; u32Count++)
  90.     {
  91.         I2DAT = u8DAT;
  92.         clr_SI;
  93.         while (!SI);                            //Check SI set or not
  94.         if (I2STAT != 0x28)              
  95.             I2C_Error();

  96.         u8DAT = ~u8DAT;        
  97.     }

  98. //--------------------------------------------------------------------------------------------
  99. //----  Waitting the ready for I2C write------------------------------------------------------
  100. //--------------------------------------------------------------------------------------------
  101.     /* Step6 */
  102.     do{
  103.         set_STO;
  104.         clr_SI;
  105.         
  106.         set_STA;                                //Check if no ACK is returned by EEPROM, it is under timed-write cycle
  107.         clr_SI;
  108.         while (!SI);                            //Check SI set or not
  109.         if (I2STAT != 0x08)                     //Check status value after every step
  110.             I2C_Error();

  111.         clr_STA;
  112.         I2DAT = (EEPROM_SLA | EEPROM_WR);
  113.         clr_SI;
  114.         while (!SI);                            //Check SI set or not
  115.     }while (I2STAT != 0x18);
  116.    
  117.     /* Step7 */
  118.     set_STO;
  119.     clr_SI;
  120.     while (STO);                                /* Check STOP signal */
  121. //--------------------------------------------------------------------------------------------
  122. //----  Page Read ----------------------------------------------------------------------------
  123. //--------------------------------------------------------------------------------------------
  124.     /* Step8 */
  125.     set_STA;
  126.     clr_SI;         
  127.     while (!SI);                                //Check SI set or not
  128.     if (I2STAT != 0x08)                         //Check status value after every step
  129.         I2C_Error();

  130.     /* Step9 */
  131.     I2DAT = (EEPROM_SLA | EEPROM_WR);
  132.     clr_STA;
  133.     clr_SI;
  134.     while (!SI);                                //Check SI set or not
  135.     if (I2STAT != 0x18)              
  136.         I2C_Error();

  137.     /* Step10 */
  138.     I2DAT = 0x00;                               //address high for I2C EEPROM
  139.     clr_SI;
  140.     while (!SI);                                //Check SI set or not
  141.     if (I2STAT != 0x28)              
  142.         I2C_Error();

  143.     /* Step11 */
  144.     I2DAT = 0x00;                               //address low for I2C EEPROM
  145.     clr_SI;
  146.     while (!SI);                                //Check SI set or not
  147.     if (I2STAT != 0x28)              
  148.         I2C_Error();

  149.     /* Step12 */
  150.     /* Repeated START */
  151.     set_STA;                       
  152.     clr_SI;
  153.     while (!SI);                                //Check SI set or not
  154.     if (I2STAT != 0x10)                         //Check status value after every step
  155.         I2C_Error();
  156.    
  157.     /* Step13 */
  158.     clr_STA;                                    //STA needs to be cleared after START codition is generated
  159.     I2DAT = (EEPROM_SLA | EEPROM_RD);
  160.     clr_SI;
  161.     while (!SI);                                //Check SI set or not
  162.     if (I2STAT != 0x40)              
  163.         I2C_Error();
  164.    
  165.     /* Step14 */
  166.     for (u32Count = 0; u32Count <PAGE_SIZE-1; u32Count++)
  167.     {
  168.         set_AA;
  169.         clr_SI;        
  170.         while (!SI);                            //Check SI set or not

  171.         if (I2STAT != 0x50)              
  172.             I2C_Error();
  173.         
  174.         if (I2DAT != u8DAT)            
  175.             I2C_Error();
  176.         u8DAT = ~u8DAT;
  177.     }
  178.    
  179.     /* Step15 */
  180.     clr_AA;
  181.     clr_SI;
  182.     while (!SI);                                //Check SI set or not
  183.     if (I2STAT != 0x58)              
  184.         I2C_Error();

  185.     /* Step16 */
  186.     set_STO;
  187.     clr_SI;
  188.     while (STO);                                /* Check STOP signal */
  189. }
  190. //========================================================================================================
  191. void main(void)
  192. {
  193.     /* Note
  194.        MCU power on system clock is HIRC (22.1184MHz), so Fsys = 22.1184MHz
  195.     */
  196.    
  197.     Set_All_GPIO_Quasi_Mode;       
  198.     Init_I2C();                                 //initial I2C circuit
  199.     I2C_Process(0x55);                          /* I2C Master will send 0x55,0xAA,.... to slave */
  200.    
  201.     P0 = 0x00;
  202.     P3 = 0x00;
  203.    
  204.     while (1);
  205. /* =================== */
  206. }



 楼主| huahuagg 发表于 2017-10-23 20:28 | 显示全部楼层
从机模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : May/1/2015
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 I2C Slave demo code
  14. //***********************************************************************************************************

  15. #include <stdio.h>
  16. #include "N76E003.h"
  17. #include "Common.h"
  18. #include "Delay.h"
  19. #include "SFR_Macro.h"
  20. #include "Function_Define.h"

  21. //***********************************************************************************************************
  22. //  N76E885-series I2C slave mode demo code, the Slave address = 0xA4
  23. //
  24. //   ____________            _____________
  25. //  |            |   SDA    |             |
  26. //  |            |<-------->|             |
  27. //  |            |          |             |
  28. //  |N76E003(M) |          | N76E003(S) |
  29. //  |(I2C_Master)|          | (I2C_Slave) |
  30. //  |            |   SCL    |             |
  31. //  |            |--------->|             |
  32. //  |____________|          |_____________|
  33. //
  34. //  The protocol of I2C is same the "24LC64"
  35. //***********************************************************************************************************


  36. #define I2C_CLOCK               13
  37. #define EEPROM_SLA              0xA4


  38. UINT8 data_received[34], data_num = 0;

  39. //========================================================================================================
  40. void I2C_ISR(void) interrupt 6
  41. {
  42.     switch (I2STAT)
  43.     {
  44.         case 0x00:
  45.             STO = 1;
  46.             break;

  47.         case 0x60:
  48.             AA = 1;
  49.             //P3 = 0x60;
  50.             break;
  51.                                
  52.         case 0x68:
  53.                                                 P02 = 0;
  54.             while(1);
  55.             break;

  56.         case 0x80:
  57.             //P3 = 0x80;
  58.             data_received[data_num] = I2DAT;
  59.             data_num++;

  60.             if (data_num == 34)
  61.                 AA = 0;
  62.             else
  63.                 AA = 1;
  64.             break;

  65.         case 0x88:
  66.             //P3 = 0x88;
  67.             data_received[data_num] = I2DAT;
  68.             data_num = 0;
  69.             AA = 1;
  70.             break;

  71.         case 0xA0:
  72.             //P3 = 0xA0;
  73.             AA = 1;
  74.             break;

  75.         case 0xA8:
  76.             //P3 = 0xA0;
  77.             I2DAT = data_received[data_num];
  78.             data_num++;
  79.             AA = 1;
  80.             break;
  81.         
  82.         case 0xB8:
  83.             //P3 = 0xB8;
  84.             I2DAT = data_received[data_num];
  85.             data_num++;
  86.             AA = 1;
  87.             break;

  88.         case 0xC0:
  89.             AA = 1;
  90.             break;

  91.         case 0xC8:
  92.             //P3 = 0xC8;
  93.             AA = 1;
  94.             break;        
  95.     }

  96.     SI = 0;
  97.     while(STO);
  98. }

  99. //========================================================================================================
  100. void Init_I2C(void)
  101. {
  102.     P13_Quasi_Mode;                         //set SCL (P13) is Quasi mode
  103.     P14_Quasi_Mode;                         //set SDA (P14) is Quasi mode
  104.    
  105.     SDA = 1;                                //set SDA and SCL pins high
  106.     SCL = 1;
  107.    
  108.     set_P0SR_6;                             //set SCL (P06) is  Schmitt triggered input select.
  109.    
  110.     set_EI2C;                               //enable      I2C interrupt by setting IE1 bit 0
  111.     set_EA;

  112.     I2ADDR = EEPROM_SLA;                    //define own slave address
  113.     set_I2CEN;                              //enable I2C circuit
  114.     set_AA;
  115. }

  116. //========================================================================================================
  117. void main(void)
  118. {

  119.     Set_All_GPIO_Quasi_Mode;
  120.    
  121.     /* Initial I2C function */
  122.     Init_I2C();                                 //initial I2C circuit

  123.     while (1);
  124. /* =================== */
  125. }

 楼主| huahuagg 发表于 2017-10-23 20:30 | 显示全部楼层
24LC64.pdf (588.3 KB, 下载次数: 15)
NXP_I2C.pdf (280.85 KB, 下载次数: 20)




21mengnan 发表于 2017-10-23 20:48 | 显示全部楼层
这是用IO模拟的吗
捉虫天师 发表于 2017-10-23 21:05 | 显示全部楼层
看到这个图我彻底懂了,主机就是发出时钟的一个。。
huangcunxiake 发表于 2017-10-23 21:23 | 显示全部楼层
应该有一些宏是在common里面定义的吧,在这个里面没有看到具体值。
zippozippo168 发表于 2017-10-25 20:47 | 显示全部楼层
什么是 风云芯片?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

160

主题

1437

帖子

2

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