现在用新唐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============================
|