/*---------------------------------------------------------------------------------------------------------*/ /* */ /* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved. */ /* */ /*---------------------------------------------------------------------------------------------------------*/ //*********************************************************************************************************** // Nuvoton Technoledge Corp. // Website: http://www.nuvoton.com // E-Mail : MicroC-8bit@nuvoton.com // Date : Apr/29/2016 //*********************************************************************************************************** //*********************************************************************************************************** // File Function: N76E003 I2C master mode demo code, the Slave address = 0xA4 // // ____________ _____________ // | | SDA | | // | |<-------->| | // | | | | // |N76E003(M) | | N76E003(S) | // |(I2C_Master)| | (I2C_Slave) | // | | SCL | | // | |--------->| | // |____________| |_____________| // // The protocol of I2C is same the "24LC64" //*********************************************************************************************************** #include <stdio.h> #include "N76E003.h" #include "Common.h" #include "Delay.h" #include "SFR_Macro.h" #include "Function_Define.h" #define I2C_CLOCK 13 #define EEPROM_SLA 0xA4 #define EEPROM_WR 0 #define EEPROM_RD 1 #define ERROR_CODE 0x78 #define PAGE_SIZE 32 //======================================================================================================== void Init_I2C(void) { // /* Set I2C clock rate */ I2CLK = I2C_CLOCK; /* Enable I2C */ set_I2CEN; } //======================================================================================================== void I2C_Error(void) { // P3 = I2STAT; // P3 = ERROR_CODE; while (1); } //======================================================================================================== void I2C_Process(UINT8 u8DAT) { UINT32 u32Count; //-------------------------------------------------------------------------------------------- //---- Page Write---------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- /* Step1 */ set_STA; /* Send Start bit to I2C EEPROM */ clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x08) //Check status value after every step I2C_Error(); /* Step2 */ clr_STA; //STA=0 I2DAT = (EEPROM_SLA | EEPROM_WR); clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x18) I2C_Error(); /* Step3 */ I2DAT = 0x00; //address high for I2C EEPROM clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x28) I2C_Error(); /* Step4 */ I2DAT = 0x00; //address low for I2C EEPROM clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x28) I2C_Error(); /* Step5 */ for (u32Count = 0; u32Count < PAGE_SIZE; u32Count++) { I2DAT = u8DAT; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x28) I2C_Error(); u8DAT = ~u8DAT; } //-------------------------------------------------------------------------------------------- //---- Waitting the ready for I2C write------------------------------------------------------ //-------------------------------------------------------------------------------------------- /* Step6 */ do{ set_STO; clr_SI; set_STA; //Check if no ACK is returned by EEPROM, it is under timed-write cycle clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x08) //Check status value after every step I2C_Error(); clr_STA; I2DAT = (EEPROM_SLA | EEPROM_WR); clr_SI; while (!SI); //Check SI set or not }while (I2STAT != 0x18); /* Step7 */ set_STO; clr_SI; while (STO); /* Check STOP signal */ //-------------------------------------------------------------------------------------------- //---- Page Read ---------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- /* Step8 */ set_STA; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x08) //Check status value after every step I2C_Error(); /* Step9 */ I2DAT = (EEPROM_SLA | EEPROM_WR); clr_STA; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x18) I2C_Error(); /* Step10 */ I2DAT = 0x00; //address high for I2C EEPROM clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x28) I2C_Error(); /* Step11 */ I2DAT = 0x00; //address low for I2C EEPROM clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x28) I2C_Error(); /* Step12 */ /* Repeated START */ set_STA; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x10) //Check status value after every step I2C_Error(); /* Step13 */ clr_STA; //STA needs to be cleared after START codition is generated I2DAT = (EEPROM_SLA | EEPROM_RD); clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x40) I2C_Error(); /* Step14 */ for (u32Count = 0; u32Count <PAGE_SIZE-1; u32Count++) { set_AA; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x50) I2C_Error(); if (I2DAT != u8DAT) I2C_Error(); u8DAT = ~u8DAT; } /* Step15 */ clr_AA; clr_SI; while (!SI); //Check SI set or not if (I2STAT != 0x58) I2C_Error(); /* Step16 */ set_STO; clr_SI; while (STO); /* Check STOP signal */ } //======================================================================================================== void main(void) { /* Note MCU power on system clock is HIRC (22.1184MHz), so Fsys = 22.1184MHz */ Set_All_GPIO_Quasi_Mode; Init_I2C(); //initial I2C circuit I2C_Process(0x55); /* I2C Master will send 0x55,0xAA,.... to slave */ P0 = 0x00; P3 = 0x00; while (1); /* =================== */ }
收藏0 举报
/*---------------------------------------------------------------------------------------------------------*/ /* */ /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */ /* */ /*---------------------------------------------------------------------------------------------------------*/ //*********************************************************************************************************** // Nuvoton Technoledge Corp. // Website: http://www.nuvoton.com // E-Mail : MicroC-8bit@nuvoton.com // Date : May/1/2015 //*********************************************************************************************************** //*********************************************************************************************************** // File Function: N76E003 I2C Slave demo code //*********************************************************************************************************** #include <stdio.h> #include "N76E003.h" #include "Common.h" #include "Delay.h" #include "SFR_Macro.h" #include "Function_Define.h" //*********************************************************************************************************** // N76E885-series I2C slave mode demo code, the Slave address = 0xA4 // // ____________ _____________ // | | SDA | | // | |<-------->| | // | | | | // |N76E003(M) | | N76E003(S) | // |(I2C_Master)| | (I2C_Slave) | // | | SCL | | // | |--------->| | // |____________| |_____________| // // The protocol of I2C is same the "24LC64" //*********************************************************************************************************** #define I2C_CLOCK 13 #define EEPROM_SLA 0xA4 UINT8 data_received[34], data_num = 0; //======================================================================================================== void I2C_ISR(void) interrupt 6 { switch (I2STAT) { case 0x00: STO = 1; break; case 0x60: AA = 1; //P3 = 0x60; break; case 0x68: P02 = 0; while(1); break; case 0x80: //P3 = 0x80; data_received[data_num] = I2DAT; data_num++; if (data_num == 34) AA = 0; else AA = 1; break; case 0x88: //P3 = 0x88; data_received[data_num] = I2DAT; data_num = 0; AA = 1; break; case 0xA0: //P3 = 0xA0; AA = 1; break; case 0xA8: //P3 = 0xA0; I2DAT = data_received[data_num]; data_num++; AA = 1; break; case 0xB8: //P3 = 0xB8; I2DAT = data_received[data_num]; data_num++; AA = 1; break; case 0xC0: AA = 1; break; case 0xC8: //P3 = 0xC8; AA = 1; break; } SI = 0; while(STO); } //======================================================================================================== void Init_I2C(void) { P13_Quasi_Mode; //set SCL (P13) is Quasi mode P14_Quasi_Mode; //set SDA (P14) is Quasi mode SDA = 1; //set SDA and SCL pins high SCL = 1; set_P0SR_6; //set SCL (P06) is Schmitt triggered input select. set_EI2C; //enable I2C interrupt by setting IE1 bit 0 set_EA; I2ADDR = EEPROM_SLA; //define own slave address set_I2CEN; //enable I2C circuit set_AA; } //======================================================================================================== void main(void) { Set_All_GPIO_Quasi_Mode; /* Initial I2C function */ Init_I2C(); //initial I2C circuit while (1); /* =================== */ }
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
160
1437
2
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号