打印
[技术问答]

两个N79E715开发板 I2C通信,主机发送了从机地址后,没有返回ack

[复制链接]
1182|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yeruo|  楼主 | 2017-7-19 11:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
现在用新唐715的两个mcu进行I2C通信实验,但是主机发送了从机地址后,没有返回ack,主机就一直卡在这儿的。请问有朋友知道可能是什么原因吗?
======================master==============================

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

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

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

#include <stdio.h>
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Version.h"

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

#define EEPROM_WR               0
#define EEPROM_RD               1
#define PAGE_SIZE               32
#define ERROR_CODE              0x78

UINT32 u32Count;

//-----------------------------------------------------------------------------------------------------------
void Init_I2C(void)
{
    I2CLK = I2C_CLOCK;                          // Set I2C clock rate
    I2CEN = 1;                                  // Enable I2C bus
//        SDA = 1;
        //SCL = 1;
}
//-----------------------------------------------------------------------------------------------------------
void I2C_Error(void)
{
    P0 = ERROR_CODE;
    while (1);
}
//-----------------------------------------------------------------------------------------------------------
void I2C_Start(void)
{
    STA = 1;
        //SI = 0;
//        printf("i2c start before...\n");
        //printf("70:status=%x\n", I2STA);
    while (!(I2CON & SET_BIT3));                // Check SI set or not
        //printf("i2c start after...\n");
    STA = 0;
}
//-----------------------------------------------------------------------------------------------------------
void I2C_Repeat_Start(void)
{
    STA = 1;
    STO = 1;
    SI = 0;
    while (!(I2CON & SET_BIT3));                // Check SI set or not
    STA = 0;
}
//-----------------------------------------------------------------------------------------------------------
void I2C_Stop(void)
{
    STO = 1;
    SI = 0;
    while ((I2CON & 0x10) == 0x10);
}
//-----------------------------------------------------------------------------------------------------------
void I2C_Send_Address(uint8_t u8Addr_WR)        // Send Slave address
{
    I2DAT = u8Addr_WR;
    SI = 0;
        //printf("send bit3 before...\n");
        //printf("97status =%x\n", I2STA);
        while (!(I2CON & SET_BIT3));                // Check SI set or not
        //printf("send bit3 after...\n");
}
//-----------------------------------------------------------------------------------------------------------
void I2C_EEPROM_Memory_Address(uint8_t u8HighByte,uint8_t LowByte)
{
    I2DAT = u8HighByte;                        // Address high for I2C EEPROM
    SI = 0;
    while (!(I2CON & SET_BIT3));                // Check SI set or not

    I2DAT = LowByte;                           // Address high for I2C EEPROM
    SI = 0;
    while (!(I2CON & SET_BIT3));                // Check SI set or not
}
//-----------------------------------------------------------------------------------------------------------
void Write_Data_To_Slave(uint8_t u8DataNum,uint8_t u8Data)
{
    for (u32Count = 0; u32Count <u8DataNum; u32Count++)
    {
        I2DAT = u8Data;
        SI = 0;
        while (!(I2CON & SET_BIT3));            // Check SI set or not
        u8Data = ~u8Data;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Read_Data_from_Slave(uint8_t u8DataNum,uint8_t u8Data)
{
    for (u32Count = 0; u32Count < u8DataNum; u32Count++)
    {
        AA = 1;
        SI = 0;
        while (!(I2CON & SET_BIT3));            // Check SI set or not

        if (I2DAT != u8Data)
            I2C_Error();
        u8Data = ~u8Data;
    }
    AA = 0;
    SI = 0;
    while (!(I2CON & SET_BIT3));                // Check SI set or not
}
//-----------------------------------------------------------------------------------------------------------
void Master_Write_Data(uint8_t u8DAT)
{
    I2C_Start();
//        printf("i2c send address before...\n");

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

   // I2C_EEPROM_Memory_Address(0,0);

    Write_Data_To_Slave(PAGE_SIZE,u8DAT);

    I2C_Stop();
}
//-----------------------------------------------------------------------------------------------------------
void Master_Read_Data(uint8_t u8DAT)
{
    I2C_Start();

    //I2C_Send_Address(EEPROM_SLA | EEPROM_WR);

    //I2C_EEPROM_Memory_Address(0,0);

    //I2C_Repeat_Start();

    I2C_Send_Address(EEPROM_SLA | EEPROM_RD);

    Read_Data_from_Slave(PAGE_SIZE,u8DAT);

    I2C_Stop();
}
//-----------------------------------------------------------------------------------------------------------
main(void)
{
    AUXR1 |= Uart_Port_Sel;                     // Select P10/P11 as UART pin(default)
//   InitialUART0_Timer1(9600);                  // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  /*  Show_Version_Number_To_PC();
    printf ("\n*==========================================================================");
    printf ("\n* N79E715 Series I2C Interrupt Sample Code. (N79E715_M <--> N79E715_S)");
    printf ("\n*==========================================================================\n");
    printf ("\nI2C Demo Start...\n");
*/
    Init_I2C();                                  // I2C function Initialize
while(1)
{
    Master_Write_Data(0x55);

    Master_Read_Data(0x55);
        Delay1ms(1000);
}
//    printf("\nI2C Test OK!");

    while (1)                                   // Test OK
    {
        P14 = 1;
        P21 = 1;
        Delay1ms(500);
        P14 = 0;
        P21 = 0;
        Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------


======================slave============================
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

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

#include <stdio.h>
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Version.h"

#define Slave_Address   0xA4

uint8_t u8Data_Received[34], u8Data_Num = 0;

//-----------------------------------------------------------------------------------------------------------
void i2c_isr(void) interrupt 6
{
    switch (I2STA)
    {
    case 0x00:
        STO = 1;
        break;

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

        break;

    case 0x80:
        P0 = 0x80;
        u8Data_Received[u8Data_Num] = I2DAT;
        u8Data_Num++;

        if (u8Data_Num == 34)
            AA = 0;
        else
            AA = 1;
        break;

    case 0x88:
        P0 = 0x88;
        u8Data_Received[u8Data_Num] = I2DAT;
        u8Data_Num = 0;
        AA = 1;
        break;

    case 0xA0:
        P0 = 0xA0;
        AA = 1;
        break;

    case 0xA8:
        P0 = 0xA0;
        I2DAT = u8Data_Received[u8Data_Num];
        u8Data_Num++;
        AA = 1;
        break;

    case 0xB8:
        P0 = 0xB8;
        I2DAT = u8Data_Received[u8Data_Num];
        u8Data_Num++;
        AA = 1;
        break;

    case 0xC0:
        P0 = 0xC0;
        AA = 1;
        break;

    case 0xC8:
        P0 = 0xC8;
        AA = 1;
        break;
    }

    SI = 0;
    while(STO);
}
//-----------------------------------------------------------------------------------------------------------
void Init_I2C(void)
{
    SDA = 1;                                    // Set SDA and SCL pins high
    SCL = 1;
    P3M1 |= SET_BIT5;                           // Enable P1 Schmitt trigger input

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

    I2ADDR = Slave_Address;                              // Define own slave address
    I2CEN = 1;                                  // Enable I2C circuit
    AA = 1;
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{
        int i  = 0;
  //  InitialUART0_Timer1(9600);                  // 9600 Baud Rate @ 11.0592MHz
  /*  Show_Version_Number_To_PC();
    printf ("\n*==========================================================================");
    printf ("\n*  N79E715 Series I2C Slave Interrupt Sample Code. (N79E715_M <--> N79E715_S)");
    printf ("\n*==========================================================================\n");
*/
    Init_I2C();                                 // Initialize I2C function
  //  printf ("\nI2C Slave Start Receive...\n");
        while(1);
   /* while(1)
        {
                for(i = 0; i < 34; ++i)
                {
                        printf("reve[%d] = %x\n", i, u8Data_Received[i]);
                }
                //printf("i = %x\n", i);
                printf("-----------------------------------------------------\n");
                Delay1ms(2000);
        }*/
}
//-----------------------------------------------------------------------------------------------------------
沙发
dongnanxibei| | 2017-7-19 19:45 | 只看该作者
官方不是提供的有一对收发的例子吗

使用特权

评论回复
板凳
mintspring| | 2017-7-19 21:52 | 只看该作者
或许你发送的就错了呢。

使用特权

评论回复
地板
mintspring| | 2017-7-19 21:52 | 只看该作者

使用特权

评论回复
5
598330983| | 2017-7-19 22:16 | 只看该作者
新唐的例子好像有这个的吧,怎么会有问题、

使用特权

评论回复
6
yeruo|  楼主 | 2017-7-20 09:09 | 只看该作者
官方提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。

使用特权

评论回复
7
yeruo|  楼主 | 2017-7-20 09:10 | 只看该作者
dongnanxibei 发表于 2017-7-19 19:45
官方不是提供的有一对收发的例子吗


官方提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。

使用特权

评论回复
8
yeruo|  楼主 | 2017-7-20 09:11 | 只看该作者
mintspring 发表于 2017-7-19 21:52
https://bbs.21ic.com/icview-812298-1-1.html
参考这个的回答。

恩恩。这个贴我看过的。但是还是没有找到问题的。谢谢哦

使用特权

评论回复
9
yeruo|  楼主 | 2017-7-20 09:12 | 只看该作者
598330983 发表于 2017-7-19 22:16
新唐的例子好像有这个的吧,怎么会有问题、


新唐的demo提供的主机程序是读写EEPROM的。从机程序就是我贴的那个代码呢。

使用特权

评论回复
10
zhuotuzi| | 2017-7-20 15:27 | 只看该作者
研究出来没,我看人家好多STM32也是这个地方容易卡住,我没这个板子,没法研究研究

使用特权

评论回复
11
yeruo|  楼主 | 2017-7-21 15:59 | 只看该作者
zhuotuzi 发表于 2017-7-20 15:27
研究出来没,我看人家好多STM32也是这个地方容易卡住,我没这个板子,没法研究研究 ...

还没有的。就是呀。卡在这儿了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

6

帖子

0

粉丝