[技术问答] 两个N79E715开发板 I2C通信,主机发送了从机地址后,没有返回ack

[复制链接]
1499|10
 楼主| yeruo 发表于 2017-7-19 11:49 | 显示全部楼层 |阅读模式
现在用新唐715的两个mcu进行I2C通信实验,但是主机发送了从机地址后,没有返回ack,主机就一直卡在这儿的。请问有朋友知道可能是什么原因吗?
======================master==============================

  1. //***********************************************************************************************************
  2. //  Nuvoton Technology Corp.
  3. //  E-mail: MicroC-8bit@nuvoton.com
  4. //***********************************************************************************************************
  5. //  Application: I2C Function(Master)
  6. //  SCL => P1.2 ; SDA => P1.3
  7. //
  8. //  Output : P1.4 toggle when I2C function pass
  9. //***********************************************************************************************************

  10. //======================================== How to set I2C register ==========================================
  11. //
  12. //  1.I2C Frequency == Fsys/(I2CLK+1)
  13. //    Use external OSC , Fsys = 11.0592MHz
  14. //  2.I2CEN = 1 ; enable I2C function
  15. //===========================================================================================================

  16. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  17. //
  18. //<o0.0..7> I2CLK<2-255>
  19. //<o1.0..7> I2C Slave Address<160-175>
  20. //     <o2.6> UART pin Select
  21. //          <0=> Select P1.0, P1.1 as UART pin(default)
  22. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  23. //-------------------------------- <<< end of configuration section >>> -------------------------------------

  24. #include <stdio.h>
  25. #include "N79E715.h"
  26. #include "Typedef.h"
  27. #include "Define.h"
  28. #include "Common.h"
  29. #include "Delay.h"
  30. #include "Version.h"

  31. #define I2C_CLOCK               29        // FSYS/(4*(I2CLK +1)) = 12M/(4*(29+1)) = 100K
  32. #define EEPROM_SLA              0xA4
  33. #define Uart_Port_Sel           0x00

  34. #define EEPROM_WR               0
  35. #define EEPROM_RD               1
  36. #define PAGE_SIZE               32
  37. #define ERROR_CODE              0x78

  38. UINT32 u32Count;

  39. //-----------------------------------------------------------------------------------------------------------
  40. void Init_I2C(void)
  41. {
  42.     I2CLK = I2C_CLOCK;                          // Set I2C clock rate
  43.     I2CEN = 1;                                  // Enable I2C bus
  44. //        SDA = 1;
  45.         //SCL = 1;
  46. }
  47. //-----------------------------------------------------------------------------------------------------------
  48. void I2C_Error(void)
  49. {
  50.     P0 = ERROR_CODE;
  51.     while (1);
  52. }
  53. //-----------------------------------------------------------------------------------------------------------
  54. void I2C_Start(void)
  55. {
  56.     STA = 1;
  57.         //SI = 0;
  58. //        printf("i2c start before...\n");
  59.         //printf("70:status=%x\n", I2STA);
  60.     while (!(I2CON & SET_BIT3));                // Check SI set or not
  61.         //printf("i2c start after...\n");
  62.     STA = 0;
  63. }
  64. //-----------------------------------------------------------------------------------------------------------
  65. void I2C_Repeat_Start(void)
  66. {
  67.     STA = 1;
  68.     STO = 1;
  69.     SI = 0;
  70.     while (!(I2CON & SET_BIT3));                // Check SI set or not
  71.     STA = 0;
  72. }
  73. //-----------------------------------------------------------------------------------------------------------
  74. void I2C_Stop(void)
  75. {
  76.     STO = 1;
  77.     SI = 0;
  78.     while ((I2CON & 0x10) == 0x10);
  79. }
  80. //-----------------------------------------------------------------------------------------------------------
  81. void I2C_Send_Address(uint8_t u8Addr_WR)        // Send Slave address
  82. {
  83.     I2DAT = u8Addr_WR;
  84.     SI = 0;
  85.         //printf("send bit3 before...\n");
  86.         //printf("97status =%x\n", I2STA);
  87.         while (!(I2CON & SET_BIT3));                // Check SI set or not
  88.         //printf("send bit3 after...\n");
  89. }
  90. //-----------------------------------------------------------------------------------------------------------
  91. void I2C_EEPROM_Memory_Address(uint8_t u8HighByte,uint8_t LowByte)
  92. {
  93.     I2DAT = u8HighByte;                        // Address high for I2C EEPROM
  94.     SI = 0;
  95.     while (!(I2CON & SET_BIT3));                // Check SI set or not

  96.     I2DAT = LowByte;                           // Address high for I2C EEPROM
  97.     SI = 0;
  98.     while (!(I2CON & SET_BIT3));                // Check SI set or not
  99. }
  100. //-----------------------------------------------------------------------------------------------------------
  101. void Write_Data_To_Slave(uint8_t u8DataNum,uint8_t u8Data)
  102. {
  103.     for (u32Count = 0; u32Count <u8DataNum; u32Count++)
  104.     {
  105.         I2DAT = u8Data;
  106.         SI = 0;
  107.         while (!(I2CON & SET_BIT3));            // Check SI set or not
  108.         u8Data = ~u8Data;
  109.     }
  110. }
  111. //-----------------------------------------------------------------------------------------------------------
  112. void Read_Data_from_Slave(uint8_t u8DataNum,uint8_t u8Data)
  113. {
  114.     for (u32Count = 0; u32Count < u8DataNum; u32Count++)
  115.     {
  116.         AA = 1;
  117.         SI = 0;
  118.         while (!(I2CON & SET_BIT3));            // Check SI set or not

  119.         if (I2DAT != u8Data)
  120.             I2C_Error();
  121.         u8Data = ~u8Data;
  122.     }
  123.     AA = 0;
  124.     SI = 0;
  125.     while (!(I2CON & SET_BIT3));                // Check SI set or not
  126. }
  127. //-----------------------------------------------------------------------------------------------------------
  128. void Master_Write_Data(uint8_t u8DAT)
  129. {
  130.     I2C_Start();
  131. //        printf("i2c send address before...\n");

  132.     I2C_Send_Address(EEPROM_SLA | EEPROM_WR);
  133. //                printf("i2c send addr after..\n");

  134.    // I2C_EEPROM_Memory_Address(0,0);

  135.     Write_Data_To_Slave(PAGE_SIZE,u8DAT);

  136.     I2C_Stop();
  137. }
  138. //-----------------------------------------------------------------------------------------------------------
  139. void Master_Read_Data(uint8_t u8DAT)
  140. {
  141.     I2C_Start();

  142.     //I2C_Send_Address(EEPROM_SLA | EEPROM_WR);

  143.     //I2C_EEPROM_Memory_Address(0,0);

  144.     //I2C_Repeat_Start();

  145.     I2C_Send_Address(EEPROM_SLA | EEPROM_RD);

  146.     Read_Data_from_Slave(PAGE_SIZE,u8DAT);

  147.     I2C_Stop();
  148. }
  149. //-----------------------------------------------------------------------------------------------------------
  150. main(void)
  151. {
  152.     AUXR1 |= Uart_Port_Sel;                     // Select P10/P11 as UART pin(default)
  153. //   InitialUART0_Timer1(9600);                  // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  154.   /*  Show_Version_Number_To_PC();
  155.     printf ("\n*==========================================================================");
  156.     printf ("\n* N79E715 Series I2C Interrupt Sample Code. (N79E715_M <--> N79E715_S)");
  157.     printf ("\n*==========================================================================\n");
  158.     printf ("\nI2C Demo Start...\n");
  159. */
  160.     Init_I2C();                                  // I2C function Initialize
  161. while(1)
  162. {
  163.     Master_Write_Data(0x55);

  164.     Master_Read_Data(0x55);
  165.         Delay1ms(1000);
  166. }
  167. //    printf("\nI2C Test OK!");

  168.     while (1)                                   // Test OK
  169.     {
  170.         P14 = 1;
  171.         P21 = 1;
  172.         Delay1ms(500);
  173.         P14 = 0;
  174.         P21 = 0;
  175.         Delay1ms(500);
  176.     }
  177. }
  178. //-----------------------------------------------------------------------------------------------------------


======================slave============================
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************
  10. //  Application: I2C Function(Slave)
  11. //  SCL => P1.2 ; SDA => P1.3
  12. //
  13. //  Output : I2C status => P0
  14. // **********************************************************************************************************

  15. #include <stdio.h>
  16. #include "N79E715.h"
  17. #include "Typedef.h"
  18. #include "Define.h"
  19. #include "Common.h"
  20. #include "Delay.h"
  21. #include "Version.h"

  22. #define Slave_Address   0xA4

  23. uint8_t u8Data_Received[34], u8Data_Num = 0;

  24. //-----------------------------------------------------------------------------------------------------------
  25. void i2c_isr(void) interrupt 6
  26. {
  27.     switch (I2STA)
  28.     {
  29.     case 0x00:
  30.         STO = 1;
  31.         break;

  32.     case 0x60: /*60H, own SLA+W received, ACK returned*/
  33.         AA = 1;
  34.         //P0 = 0x60;

  35.         break;

  36.     case 0x80:
  37.         P0 = 0x80;
  38.         u8Data_Received[u8Data_Num] = I2DAT;
  39.         u8Data_Num++;

  40.         if (u8Data_Num == 34)
  41.             AA = 0;
  42.         else
  43.             AA = 1;
  44.         break;

  45.     case 0x88:
  46.         P0 = 0x88;
  47.         u8Data_Received[u8Data_Num] = I2DAT;
  48.         u8Data_Num = 0;
  49.         AA = 1;
  50.         break;

  51.     case 0xA0:
  52.         P0 = 0xA0;
  53.         AA = 1;
  54.         break;

  55.     case 0xA8:
  56.         P0 = 0xA0;
  57.         I2DAT = u8Data_Received[u8Data_Num];
  58.         u8Data_Num++;
  59.         AA = 1;
  60.         break;

  61.     case 0xB8:
  62.         P0 = 0xB8;
  63.         I2DAT = u8Data_Received[u8Data_Num];
  64.         u8Data_Num++;
  65.         AA = 1;
  66.         break;

  67.     case 0xC0:
  68.         P0 = 0xC0;
  69.         AA = 1;
  70.         break;

  71.     case 0xC8:
  72.         P0 = 0xC8;
  73.         AA = 1;
  74.         break;
  75.     }

  76.     SI = 0;
  77.     while(STO);
  78. }
  79. //-----------------------------------------------------------------------------------------------------------
  80. void Init_I2C(void)
  81. {
  82.     SDA = 1;                                    // Set SDA and SCL pins high
  83.     SCL = 1;
  84.     P3M1 |= SET_BIT5;                           // Enable P1 Schmitt trigger input

  85.     EI2C = 1;                                   // Enable I2C interrupt by setting EIE bit 0
  86.     EA = 1;

  87.     I2ADDR = Slave_Address;                              // Define own slave address
  88.     I2CEN = 1;                                  // Enable I2C circuit
  89.     AA = 1;
  90. }
  91. //-----------------------------------------------------------------------------------------------------------
  92. void main(void)
  93. {
  94.         int i  = 0;
  95.   //  InitialUART0_Timer1(9600);                  // 9600 Baud Rate @ 11.0592MHz
  96.   /*  Show_Version_Number_To_PC();
  97.     printf ("\n*==========================================================================");
  98.     printf ("\n*  N79E715 Series I2C Slave Interrupt Sample Code. (N79E715_M <--> N79E715_S)");
  99.     printf ("\n*==========================================================================\n");
  100. */
  101.     Init_I2C();                                 // Initialize I2C function
  102.   //  printf ("\nI2C Slave Start Receive...\n");
  103.         while(1);
  104.    /* while(1)
  105.         {
  106.                 for(i = 0; i < 34; ++i)
  107.                 {
  108.                         printf("reve[%d] = %x\n", i, u8Data_Received[i]);
  109.                 }
  110.                 //printf("i = %x\n", i);
  111.                 printf("-----------------------------------------------------\n");
  112.                 Delay1ms(2000);
  113.         }*/
  114. }
  115. //-----------------------------------------------------------------------------------------------------------
dongnanxibei 发表于 2017-7-19 19:45 | 显示全部楼层
官方不是提供的有一对收发的例子吗
mintspring 发表于 2017-7-19 21:52 | 显示全部楼层
或许你发送的就错了呢。
mintspring 发表于 2017-7-19 21:52 | 显示全部楼层
598330983 发表于 2017-7-19 22:16 | 显示全部楼层
新唐的例子好像有这个的吧,怎么会有问题、
 楼主| yeruo 发表于 2017-7-20 09:09 | 显示全部楼层
官方提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。
 楼主| yeruo 发表于 2017-7-20 09:10 | 显示全部楼层
dongnanxibei 发表于 2017-7-19 19:45
官方不是提供的有一对收发的例子吗


官方提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。
 楼主| yeruo 发表于 2017-7-20 09:11 | 显示全部楼层
mintspring 发表于 2017-7-19 21:52
https://bbs.21ic.com/icview-812298-1-1.html
参考这个的回答。

恩恩。这个贴我看过的。但是还是没有找到问题的。谢谢哦
 楼主| yeruo 发表于 2017-7-20 09:12 | 显示全部楼层
598330983 发表于 2017-7-19 22:16
新唐的例子好像有这个的吧,怎么会有问题、


新唐的demo提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。
zhuotuzi 发表于 2017-7-20 15:27 | 显示全部楼层
研究出来没,我看人家好多STM32也是这个地方容易卡住,我没这个板子,没法研究研究
 楼主| yeruo 发表于 2017-7-21 15:59 | 显示全部楼层
zhuotuzi 发表于 2017-7-20 15:27
研究出来没,我看人家好多STM32也是这个地方容易卡住,我没这个板子,没法研究研究 ...

还没有的。就是呀。卡在这儿了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

6

帖子

0

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