/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technology Corp.
// E-Mail: MicroC-8bit@nuvoton.com
// Date: 27/Jan/2014
//***********************************************************************************************************
/*===========================================================================================================
// N79E352 I2C sample code for I2C Master device, the slave address = 0xA4
//
// ____________ _____________
// | | SDA | |
// | |<-------->| |
// | | | |
// | N79E352(M) | | N79E352(S) |
// |(I2C_Master)| | (I2C_Slave) |
// | | SCL | |
// | |--------->| |
// |____________| |_____________|
//
// The transaction protocol:
// S => SLA+W(A) => DATA_AMOUNT(A) => Data[0](A) => Data[1](A) => ... => Data[6](A) => Data[7](/A) => P
// S => SLA+W(A) => DATA_ADDR(A) => rS => SLA+R(A) => Read_Back_Data(A) => P
//
// Suggest observing SDA and SCL waveform with an oscilloscope.
*=========================================================================================================*/
/*
//-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
//
// <o0> I2C clock rate under Fsys 22.1184MHz
// <13=> 400kbps
// <54=> 100kbps
// <o5> Data which is wanted to be read back
// <0=> Byte 0, 0x00
// <1=> Byte 1, 0x55
// <2=> Byte 2, 0xAA
// <3=> Byte 3, 0xFF
// <4=> Byte 4, 0x54
// <5=> Byte 5, 0xA9
// <6=> Byte 6, 0xFE
// <7=> Byte 7, 0x53
//
//-------- <<< end of configuration section >>> ------------------------------
*/
#include "N79E352.h"
#include "Typedef.h"
#define I2C_CLOCK 13
#define SLAVE_ADDRESS 0xA4 //for I2C Slave MCU address
#define WR 0 //WR and RD determine SLA+W or SLA+R
#define RD 1
#define DATA_AMOUNT 8 //8 byte data length for demo
#define DATA_INDEX 0
#define ERROR_CODE 0x78
#define PASS_CODE 0x5d
const UINT8 Data[DATA_AMOUNT] = {0x00, 0x55, 0xAA, 0xFF, 0x54, 0xA9, 0xFE, 0x53};
volatile UINT8 u8Data_Num = 0, u8Read_Back_Data;
volatile BIT bWR_RD = WR;
/************************************************************************************************************
* I2C interrupt subroutine
************************************************************************************************************/
void I2C_Error (void)
{
P0 = ERROR_CODE;
while(1);
}
/************************************************************************************************************
* I2C interrupt subroutine
************************************************************************************************************/
void I2C_ISR (void) interrupt 6 //interrupt address is 0x0033
{
switch (I2STATUS)
{
case 0x00: //bus error handle
STO = 1;
SI = 0;
while(STO);
ENSI = 0;
ENSI = 1;
break;
case 0x08:
STA = 0;
I2DAT = SLAVE_ADDRESS + WR;
break;
case 0x10:
STA = 0;
I2DAT = SLAVE_ADDRESS + RD;
break;
case 0x18:
I2DAT = (!bWR_RD) ? DATA_AMOUNT : DATA_INDEX;
break;
case 0x28:
if (!bWR_RD)
{
I2DAT = Data[u8Data_Num];
u8Data_Num++;
}
else
STA = 1;
break;
case 0x30:
STO = 1;
u8Data_Num = 0;
bWR_RD = RD;
break;
case 0x40:
AA = 1;
break;
case 0x50:
u8Read_Back_Data = I2DAT;
STO = 1;
bWR_RD = WR;
break;
case 0x20:
case 0x38:
case 0x48:
case 0x58:
case 0x68:
case 0x78:
case 0xB0:
P2 = I2STATUS;
I2C_Error();
break;
}
SI = 0;
while(STO);
}
/************************************************************************************************************
* Main function
************************************************************************************************************/
void main (void)
{
UINT8 u8Delay = 0xFF;
P12 = P13 = 1; //before use I2C bus, function shared pins P1.2 and P1.3 should be high
I2CLK = I2C_CLOCK; //determine I2C clock rate
ENSI = 1; //enable I2C
EI2 = 1;
EA = 1;
while (u8Delay--); //short delay to waiting I2C slave device ready
STA = 1; //generate START condition to start writing transaction
while(!bWR_RD); //waiting for writing transaction complete
STA = 1; //generate START condition to start reading transaction
while(bWR_RD); //waiting for reading transaction complete
/*check read back data value*/
if (u8Read_Back_Data != Data[DATA_INDEX])
I2C_Error();
else
P0 = PASS_CODE;
while(1);
}