#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
#define BIT8 0x100
#define BIT9 0x200
#define BIT10 0x400
#define BIT11 0x800
#define BIT12 0x1000
#define BIT13 0x2000
#define BIT14 0x4000
#define BIT15 0x8000
#define BIT16 0x10000
#define BIT22 0x400000
#define BIT24 0x1000000
#define BIT25 0x2000000
#define BIT26 0x4000000
#define BIT27 0x8000000
#define BIT30 0x40000000
void Undef_Handler(void) { }
void IRQ_Handler(void) { }
void SWI_Handler(void) { }
void PAbt_Handler(void) { }
void DAbt_Handler(void) { }
void FIQ_Handler(void) { }
unsigned char szTxData[]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char szRxData[]={0x00};
#include
int main(void)
{ unsigned char ucWai
tingForXIRQ0;
unsigned int I2CMSTATUS ;
unsigned char ucRxCount ;
unsigned char ucTxCount ;
POWKEY1 = 0x1;
POWCON0 = 0x78; // 主频 10.24Mhz
POWKEY2 = 0xF4;
GP1CON = BIT0 + BIT4; // Select UART functionality for P1.0/P1.1
COMCON0 = BIT7; // Enable access to COMDIV registers
COMDIV0 = 0x21; // Set baud rate to 9600.
COMDIV1 = 0x00;
COMCON0 = BIT0 + BIT1 + BIT2;
//==========================================
GP0CON0 = BIT4 + BIT12; // Select SPI/I2C alternative function for P0.1 P0.3
GP0KEY1 = 0x7; // Write to GP0KEY1
GP0CON1 = BIT1; // Select I2C functionality for P0.1 P0.3
GP0KEY2 = 0x13; // Write to GP0KEY2
I2CMCON = BIT0 ;
I2CDIV = 0x3e3e; // Select 80kHz clock rate
ucTxCount = 0;
I2CFSTA = BIT9; // Flush Master Tx FIFO
I2CFSTA = ~BIT9;
I2CMTX = 0x00;
I2CADR0 = 0xA0;
ucWaitingForXIRQ0 = 1;
ucRxCount = 0;
while (1)
{ if (ucWaitingForXIRQ0 == 0) // Wait for XIRQ0 to trigger - cleared in XIRQ0 ISR
{ ucWaitingForXIRQ0 = 1;
ucRxCount = 0;
I2CMCNT0 = 9; // Read back 6 bytes
I2CADR0 = 0xA1;}// + I2C_RD_BIT; // Begin Read sequence from Slave
if(ucWaitingForXIRQ0 == 1)
{ I2CMSTATUS = 0;
I2CMSTATUS = I2CMSTA;
if ((I2CMSTATUS BIT2) == BIT2) // If I2C Master Tx IRQ
{ if (ucTxCount < 8) // Have max 6 bytes been sent?
{I2CMTX = szTxData[ucTxCount++]; // Load Tx buffer
}
ucWaitingForXIRQ0 =0;}
if ((I2CMSTATUS BIT3) == BIT3) // If I2C Master Rx IRQ
{if (ucRxCount < 8) // Have max 6 bytes been received?
{szRxData[ucRxCount] = I2CMRX; // Read Rx buffer
while(!((COMSTA0 BIT6)==BIT6)){};
COMTX=szRxData[ucRxCount];
ucRxCount++;}
ucWaitingForXIRQ0 =1;
}
}
}
}