//==============================================================================
// Example of IIC communication between 9S12XEP100 and EEPROM FM24C256
// BUSCLK = 8MHz
// SCL = 91kHz
// I2C1 is used for commnication
//==============================================================================
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
//----------------------------
#define MaxLen 128 //
#define slaveread 0xA1
#define slavewrite 0xA0
unsigned char RD_data;
//--------------------------
byte WrData[8];
byte RdData[8];
byte RdBuf;
word length;
unsigned char x;
//---
typedef union
{
word EE_Address;
struct
{
byte Address_H;
byte Address_L;
}Bytes;
}tAddr;
tAddr ADDR;
#define Address ADDR.EE_Address
#define Addr_L ADDR.Bytes.Address_L
#define Addr_H ADDR.Bytes.Address_H
//****** Init routines *******************
//--
void IIC0_init(void)
{
IIC0_IBFD = 0x4C;
IIC0_IBAD = 0x00;
IIC0_IBCR = 0x80;
//IIC0_IBSR_IBAL = 1;
}
//--
//---------------------------------------------------------
void IIC_write_nbytes(word addr, byte len)
{
byte i;
Address = addr; // load address;
IIC0_IBCR_TXAK = 0; ;
IIC0_IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0;
// And generate START condition;
//-------start of transmit first byte to IIC bus-----
IIC0_IBDR = slavewrite; // Address the slave and set up for master transmit;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBDR = Addr_H; // Send high byte of the word address;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBDR = Addr_L; // Send low byte of the word address;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
for(i=0;i<len;i++)
{
IIC0_IBDR = WrData[i];//data;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
}
//-----Slave ACK occurred------------
IIC0_IBCR_MS_SL = 0; // generate STOP condition;
}
//-------------------------------------------------------------------------
void IIC_read_nbytes(word addr,byte len)
{
byte dummy;
byte i;
Address = addr;
length=len;
if(length>=MaxLen)
{
length=MaxLen;
}
IIC0_IBCR_TXAK = 0; // TXAK = 0;
IIC0_IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0;
// And generate START condition;
IIC0_IBDR = slavewrite; // Address the slave and set up for master transmit;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBDR = Addr_H; // Send high byte of word address;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBDR = Addr_L; // Send low byte of word address;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while(IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBCR_RSTA = 1; // set up repeated start;
IIC0_IBDR = slaveread; // (slave_address) | (RW = 1);
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
while (IIC0_IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC0_IBCR_TX_RX = 0; // set up to receive;
dummy=IIC0_IBDR;//RD_data = IIC0_IBDR; // dummy read;
for(i=0;i<length;i++)
{
if(i==7){
while(!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF = 1;
RdData[7]=IIC0_IBDR;
}
else{
while(!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;
while (IIC0_IBSR_RXAK);
RdData[i]=IIC0_IBDR;
}
}
IIC0_IBCR_TXAK = 1; // acknowledge disable;
while (!IIC0_IBSR_IBIF); // wait until IBIF;
IIC0_IBSR_IBIF=1; // clear the interrupt event flag;
IIC0_IBCR_MS_SL = 0; // generate stop signal;
RD_data = IIC0_IBDR; // read right data;
}
//****** main routines *******************
void main(void)
{
unsigned int i;
unsigned int j,k;
//---- IIC -----
IIC0_init();
WrData[0]=1;
WrData[1]=0;
WrData[2]=0;
WrData[3]=8;
WrData[4]=6;
WrData[5]=1;
WrData[6]=1;
WrData[7]=255;
//---- main cycle ---
//LedPad22Dir=1;
//LedCpu=1;
do{
IIC_write_nbytes(0x0064,8);
for(j=0;j<0x1000;j++);
IIC_read_nbytes(0x0064,8);
//--
for(j=0;j<50000;j++);
//LedCpu=~LedCpu;
}
while(1);
i+=8;
/**/
}
//==============================================================================
这是我用的程序,二楼写问题 |