|
rt,代码如下
#include "option.h"
#include "def.h"
#include "2440addr.h"
#include "lcd_tft.h"
#include "mmu.h"
#define pISR_IIC (*(unsigned *)(_ISR_STARTADDRESS+0x8c))
#define pISR_UART0 (*(unsigned *)(_ISR_STARTADDRESS+0x90))
//IIC部分的变量
U8 iic_wrbuffer[8];
U8 iic_rdbuffer[8];
U8 iic_flag;
U8 address;
int length;
U8 comm;
U8 devAddr=0xa0;
static char command;
static char count;
int i;
void delay(U32 ms)
{
for(;ms>0;ms--);
}
void __irq uartisp(void)
{
//读写AT24C02A的串口
U8 ch;
rSUBSRCPND |=0x1;
rSRCPND |=0x1<<28;
rINTPND |=0x1<<28;
ch=rURXH0;
if(command==0)
{
switch(ch){
case 0xc0:
command=0xc0;
count=0;
comm=0;
break;
case 0xc1:
command=0xc1;
count=0;
comm=0;
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;
}
else
{
iic_wrbuffer[count-3]=ch;
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;
count=0;
command=0;
comm=2;
rUTXH0=0xc1;
}
}
}
}
void uart_init(void)
{
rGPHCON = 0x00ffaa;
rGPHUP = 0x7ff;
rULCON0 = 0x3;
rUCON0 = 0x5;
rUFCON0 = 0x0;
rUMCON0 = 0x0;
rUBRDIV0 = 26;
rSRCPND = (0x1<<28);
// rSUBSRCPND = 0x3;
rSUBSRCPND = 0x1;
rINTPND = (0x1<<28);
// rINTSUBMSK &=~0x3;
rINTSUBMSK &=~0x1;
rINTMSK &=~(0x1<<28);
}
void interrupt_init(void)
{
rSRCPND |=0x3;
rINTMSK &=~0x3;
rINTPND |=0x3;
rGPFCON &=~0xf;
rGPFCON |=0xa;
}
//IIC中断
void __irq iicISR(void)
{
rSRCPND |=0x1<<27;
rINTPND |=0x1<<27;
iic_flag=0;
rGPBDAT = ~0x80;
}
//IIC的写程序
void wr_at24c02(U8 wordAddr,U8 *buffer,int sizeofdata)
{
// U8 i;
iic_flag=1;
rIICDS=devAddr;
rIICCON &=~0x10; //清中断标志
delay(5);
rIICSTAT =0xf0; //主设备发送模式
while(iic_flag)
{
delay(200);
}
rGPBDAT = ~0x80;
iic_flag=1;
rIICDS=wordAddr;
rIICCON &=~0x10;
while(iic_flag)
delay(100);
for(i=0;i<sizeofdata;i++)
{
iic_flag=1;
rIICDS=*(buffer+i);
rIICCON &=~0x10;
while(iic_flag)
delay(100);
}
rIICSTAT = 0xd0; //发出stop命令
rIICCON =0xe0;
delay(100);
if(comm==1)
rGPBDAT = ~0x20;
}
//IIC读程序
void rd_at24c02a(U8 wordAddr,U8 *buffer,int sizeofdata)
{
// int i;
U8 temp;
iic_flag=1;
rIICDS=devAddr;
rIICCON &=~0x10;
rIICSTAT =0xf0;
rGPBDAT = ~0x80;
while(iic_flag)
delay(100);
iic_flag=1;
rIICDS=wordAddr;
rIICCON &=~0x10;
while(iic_flag)
delay(100);
iic_flag=1;
rIICDS=devAddr;
rIICCON &=~0x10;
rIICSTAT=0xb0;
while(iic_flag)
delay(100);
iic_flag=1;
temp=rIICDS;
rIICCON &=~0x10;
while(iic_flag)
delay(100);
for(i=0;i<sizeofdata;i++)
{
iic_flag=1;
if(i==sizeofdata-1)
rIICCON &=~0x80;
*(buffer+i)=rIICDS;
rIICCON &=~0x10;
while(iic_flag==1)
delay(100);
}
rIICSTAT =0x90;
rIICCON=0xe0;
delay(100);
if(comm==1)
rGPBDAT = ~0x40;
}
//IIC初始化
void iic_init(void)
{
rGPEUP |=0xc000;
rGPECON &=~0xf0000000;
rGPECON |=0xa0000000;
rSRCPND |=0x1<<27;
rINTMSK &=~(0x1<<27);
rINTPND |=0x1<<27;
rIICCON =0xe0;
rIICSTAT=0x10;
// iic_flag=1;
comm=0;
}
void Main(void)
{
rGPBCON=0x015400;
rGPBUP=0x7ff;
// light=0x10;
command=0;
count=0;
MMU_Init();
uart_init();
iic_init();
pISR_UART0 = (U32)uartisp;
pISR_IIC = (U32)iicISR;
while(1)
{
switch(comm)
{
case 1:
wr_at24c02(address,iic_wrbuffer,length);
comm=0;
iic_flag=1;
address=0;
length=0;
break;
case 2:
rd_at24c02a(address,iic_rdbuffer,length);
comm=0;
iic_flag=1;
address=0;
for(i=0;i<length;i++)
{
delay(500);
rUTXH0=iic_rdbuffer[i];
}
length=0;
break;
}
}
} |
|