/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Website: http://www.nuvoton.com
// E-Mail : MicroC-8bit@nuvoton.com
//***********************************************************************************************************
#include "ML51.h"
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] I2C0 master demo.
* @param None
* [url=home.php?mod=space&uid=266161]@return[/url] None
* [url=home.php?mod=space&uid=1543424]@Details[/url] please confirm enable LXT and check work stable if use LXT as WKT clock source.
*/
unsigned char u8DeviceAddr;
bit I2CEndFlag=0,I2CErrorFlag=0;
unsigned char u8DataLen;
unsigned char xdata u8TxData[10],u8MstRxData[10];
void I2C0_ISR(void) interrupt 6 // Vector [url=home.php?mod=space&uid=72445]@[/url] 0x33
{
switch(I2C0STAT)
{
/* START has been transmitted, send read or write status to the bus */
case 0x08:
clr_I2C0CON_STA;
I2C0DAT = (u8DeviceAddr<<1)|I2C_R; /* Write SLA+R to Register I2C0DAT */
break;
/* For master RX status */
/* SLA+R has been transmitted and ACK has been received */
case 0x40:
u8MstRxData[u8DataLen] = I2C0DAT;
set_I2C0CON_AA;
break;
/* SLA+R has been transmitted and NACK has been received */
case 0x48:
set_I2C0CON_STO; /* This status should not be found, send stop to close this transmit.*/
I2CEndFlag = 1;
I2CErrorFlag = 1;
break;
/* Data has been recived and ACK has been received */
case 0x50:
u8MstRxData[u8DataLen++] = I2C0DAT;
set_I2C0CON_AA; /* send ACK to slave */
case 0x58:
u8MstRxData[u8DataLen++] = I2C0DAT;
clr_I2C0CON_AA;
set_I2C0CON_STO;
I2CEndFlag = 1;
I2CErrorFlag = 0;
break;
/* Bus error */
case 0x00:
set_I2C0CON_STO;
I2CEndFlag = 1;
I2CErrorFlag = 1;
break;
}
clr_I2C0CON_SI;
}
/*---------------------------------------------------------------------------------------------------------*/
/* I2C Tx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_INIT(void)
{
I2C_Open(I2C0, 24000000, 100000);
I2C_EnableTimeout(I2C0);
I2C_EnableInt(I2C0);
}
void main (void)
{
MFP_P13_GPIO;
P13_PUSHPULL_MODE;
MFP_P24_I2C0_SDA;
P24_OPENDRAIN_MODE;
MFP_P25_I2C0_SCL;
P25_OPENDRAIN_MODE;
I2C_Open(I2C0, 24000000, 100000);
I2C_EnableTimeout(I2C0);
I2C_EnableInt(I2C0);
while(I2CEndFlag==0);
if(I2CErrorFlag|I2C0STAT==0xF8)
{
I2CEndFlag = 0;
I2CErrorFlag = 0;
while(I2CEndFlag==0);
}
while(1);
}
|