#define _ISR_STARTADDRESS 0x33ffff00
#define U32 unsigned int
#define U8 unsigned char
#define pISR_UART0 (*(unsigned *)(_ISR_STARTADDRESS+0x90))
#define pISR_IIC (*(unsigned *)(_ISR_STARTADDRESS+0x8c))
#define rULCON0 (*(volatile unsigned *)0x50000000) //UART 0 Line control
#define rUCON0 (*(volatile unsigned *)0x50000004) //UART 0 Control
#define rUFCON0 (*(volatile unsigned *)0x50000008) //UART 0 FIFO control
#define rUMCON0 (*(volatile unsigned *)0x5000000c) //UART 0 Modem control
#define rUTRSTAT0 (*(volatile unsigned *)0x50000010) //UART 0 Tx/Rx status
#define rUERSTAT0 (*(volatile unsigned *)0x50000014) //UART 0 Rx error status
#define rUFSTAT0 (*(volatile unsigned *)0x50000018) //UART 0 FIFO status
#define rUMSTAT0 (*(volatile unsigned *)0x5000001c) //UART 0 Modem status
#define rUBRDIV0 (*(volatile unsigned *)0x50000028) //UART 0 Baud rate divisor
//little endian
#define rUTXH0 (*(volatile unsigned char *)0x50000020) //UART 0 Transmission Hold
#define rURXH0 (*(volatile unsigned char *)0x50000024) //UART 0 Receive buffer
#define rSRCPND (*(volatile unsigned *)0x4a000000) //Interrupt request status
//#define rINTMOD (*(volatile unsigned *)0x4a000004) //Interrupt mode control
#define rINTMSK (*(volatile unsigned *)0x4a000008) //Interrupt mask control
//#define rPRIORITY (*(volatile unsigned *)0x4a00000c) //IRQ priority control
#define rINTPND (*(volatile unsigned *)0x4a000010) //Interrupt request status
#define rSUBSRCPND (*(volatile unsigned *)0x4a000018) //Sub source pending
#define rINTSUBMSK (*(volatile unsigned *)0x4a00001c) //Interrupt sub mask
//#define rGPFCON (*(volatile unsigned *)0x56000050) //Port F control
#define rGPHCON (*(volatile unsigned *)0x56000070) //Port H control
//#define rGPHDAT (*(volatile unsigned *)0x56000074) //Port H data
#define rGPHUP (*(volatile unsigned *)0x56000078) //Pull-up control H
#define rGPECON (*(volatile unsigned *)0x56000040) //Port E control
#define rGPEDAT (*(volatile unsigned *)0x56000044) //Port E data
#define rGPEUP (*(volatile unsigned *)0x56000048) //Pull-up control E
// IIC
#define rIICCON (*(volatile unsigned *)0x54000000) //IIC control
#define rIICSTAT (*(volatile unsigned *)0x54000004) //IIC status
#define rIICADD (*(volatile unsigned *)0x54000008) //IIC address
#define rIICDS (*(volatile unsigned *)0x5400000c) //IIC data shift
#define rIICLC (*(volatile unsigned *)0x54000010) //IIC multi-master line control
#define rGPBCON (*(volatile unsigned *)0x56000010) //Port B control
#define rGPBDAT (*(volatile unsigned *)0x56000014) //Port B data
#define rGPBUP (*(volatile unsigned *)0x56000018) //Pull-up control B
unsigned char iic_buffer[8];//year1,month,day,hour,minute,second;
unsigned char address,length;
unsigned char flag;
unsigned char comm;
unsigned char devAddr=0xa0;
void delay(int a)
{
int k;
for(k=0;k<a;k++)
;
}
void __irq IicISR(void)
{
rSRCPND |= 0x1<<27;
rINTPND |= 0x1<<27;
flag = 0;
}
void __irq uartISR(void)
{
char ch;
static char command;
static char count;
//static char count1;
rSUBSRCPND |= 0x1;
rSRCPND |= 0x1<<28;
rINTPND |= 0x1<<28;
//if(rUTRSTAT0 & 1) //接收数据处理部分
//{
ch = rURXH0; //接收字节数据
if(command==0)
{
switch(ch)
{
case 0xc0:
command = 0xc0; //接收数据,写eeprom
count=0;
comm = 0;
//rUTXH0=ch;
break;
case 0xc1:
command = 0xc1; //发送数据,读eeprom
count=0;
comm = 0;
//rUTXH0=ch;
break;
default:
command = 0;
count =0;
rUTXH0=ch;
break;
}
}
else
{
if(command == 0xc0)
{
count++;
if (count == 1)
{
address = ch;
}
else if(count == 2)
{
length = ch;
//count1=0;
}
else
{
iic_buffer[count-3] = ch;
//count1++;
if(count==length+2)
{
rUTXH0=0xc0;
count=0;
command=0;
comm=1;
}
}
}
else if(command ==0xc1)
{
count++;
if(count==1)
{
address = ch;
}
else
{
length = ch;
rUTXH0=0xc1;
count=0;
command=0;
comm = 2;
}
}
}
//}
}
void wr24c02a(unsigned char wordAddr,unsigned char iic_buffer[],int sizeofdate )
{
//static int iii;
int i;
//rIICCON &= ~0x10;
flag =1;
rIICDS =0xa1; //devAddr;//(devAddr<<1) | 0x0;
rIICCON &= ~0x10;
rIICSTAT = 0xf0;
//iii++;
while(flag == 1)
delay(100);//rGPBDAT = ~0x1e0;;
//rGPBDAT = 0x1e0;
flag = 1;
//rIICCON &= ~0x10;
rIICDS = wordAddr;
rIICCON &= ~0x10;
while(flag)
{delay(100);
/*rGPBDAT = ~0x1e0;*/}
for(i=0;i<sizeofdate;i++)
{
flag = 1;
//rIICCON &= ~0x10;
rIICDS = iic_buffer[i];
rIICCON &= ~0x10;
while(flag)
delay(100);
}
//rIICCON &= ~0x10;
rIICSTAT = 0xd0; //stop bit
rIICCON = 0xe0; //iic ready
delay(100); //wait a few time
}
void rd24c02a(unsigned char wordAddr,unsigned char iic_buffer[],int sizeofdate )
{
int i;
unsigned char temp;
flag =1;
//rIICCON &= ~0x10;
rIICDS = devAddr;//(devAddr<<1) | 0x0;
rIICSTAT = 0xf0;
rIICCON &= ~0x10;
while(flag)
delay(100);
rGPBDAT = ~0x1e0;
flag = 1;
//rIICCON &= ~0x10;
rIICDS = wordAddr;
rIICCON &= ~0x10;
while(flag)
delay(100);
flag = 1;
//rIICCON &= ~0x10;
rIICDS = devAddr;//(devAddr<<1) | 0x0;
rIICSTAT = 0xb0;
rIICCON &= ~0x10;
while (flag)
delay(100);
flag = 1;
temp = rIICDS;
rIICCON &= ~0x10;
while(flag)
delay(100);
for(i=0;i<sizeofdate;i++)
{
flag = 1;
//rIICCON &= ~0x10;
if(i==sizeofdate-1)
rIICCON &= ~0x80;
iic_buffer[i] = rIICDS;
//rUTXH0 = iic_buffer[i];
rIICCON &= ~0x10;
while(flag)
delay(100);
}
rIICSTAT = 0x90; //stop bit
rIICCON = 0xe0; //iic ready
rGPBDAT = 0x1e0;
delay(100); //wait a few time
}
void Main(void)
{
int i;
//uart0 port
rGPHCON = 0x00faaa;
rGPHUP = 0x7ff;
//iic port
rGPEUP = 0xc000; //Pull-up disable
rGPECON = 0xa0000000; //GPE15:IICSDA , GPE14:IICSCL
//uart0
rULCON0 = 0x3;
rUCON0 = 0x5;
rUFCON0 = 0;
rUMCON0 = 0;
rUBRDIV0 = 26;
rSRCPND = (0x1<<27)|(0x1<<28);
rSUBSRCPND = 0x1;
rINTPND = (0x1<<27)|(0x1<<28);
rINTSUBMSK = ~(0x1);
rINTMSK = ~((0x1<<27)|(0x1<<28));
rGPBCON = 0x015550;
rGPBUP = 0x7ff;
rGPBDAT = 0x1e0;
//init iic
rIICCON = 0xe0;
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)
//rIICADD = 0x10; //2410 slave address = [7:1]
pISR_UART0 = (U32)uartISR;
pISR_IIC = (U32)IicISR;
for (i=0;i<8;i++)
{
iic_buffer[i] = (i+1)*0x11;
}
flag=1;
comm=0;
while(1)
{
switch(comm)
{
case 1:
//rUTXH0 = 0xbb;
wr24c02a(address,iic_buffer,length);
comm=0;
//rGPBDAT = ~0x1e0;
flag=1;
address=0;
length=0;
//rUTXH0 = 0x99;
break;
case 2:
rd24c02a(address,iic_buffer,length);
comm=0;
//rGPBDAT = ~0x1e0;
//rUTXH0 = 0xaa;
flag=1;
address=0;
for(i=0;i<length;i++)
{
delay(500);
rUTXH0 = iic_buffer[i];
}
length=0;
break;
}
}
}