S3C2440 IIC总线接口
时间:11-21
来源:互联网
点击:
- #include
- #include"2440addr.h"
- #include"2440lib.h"
- #include"def.h"
- #defineWRDATA(1)
- #definePOLLACK(2)
- #defineRDDATA(3)
- #defineSETRDADDR(4)
- #defineIICBUFSIZE0x20
- staticU8iicData[IICBUFSIZE];
- staticvolatileintiicDataCount;
- staticvolatileintiicStatus;
- staticvolatileintiicMode;
- staticintiicPt;
- voidWr24C02(U32slvAddr,U32addr,U8data);
- voidRd24C02(U32slvAddr,U32addr,U8*data);
- voidIicPoll(void);
- voidRun_IicPoll(void);
- voidMain(void)
- {
- unsignedinti,j;
- staticU8data[256];//用于存储AT24C02读出的数据
- SelectFclk(2);//设置系统时钟400M
- ChangeClockDivider(2,1);//设置分频1:4:8
- CalcBusClk();//计算总线频率
- rGPHCON&=~((3<4)|(3<6));
- rGPHCON|=(2<4)|(2<6);//GPH2--TXD[0];GPH3--RXD[0]
- rGPHUP=0x00;//使能上拉功能
- Uart_Init(0,115200);
- Uart_Select(0);
- Uart_Printf("[IICTest(Polling)usingAT24C020]\n");
- rGPEUP|=0xc000;//关上拉
- rGPECON&=~0xf0000000;
- rGPECON|=0xa0000000;//GPE15:IICSDA,GPE14:IICSCL
- //使能应答,IIC总线时钟IICCLK=PCLK/16,使能中断,发送时钟IICCLK/16
- rIICCON=(1<7)|(0<6)|(1<5)|(0xf);
- rIICADD=0x10;//2440从机地址=[7:1]
- rIICSTAT=0x10;//IIC总线数据输出使能(Rx/Tx)
- Uart_Printf("WritetestdataintoAT24C02\n");
- for(i=0;i<256;i++)
- Wr24C02(0xa0,(U8)i,i);//写入数据到AT24C02
- for(i=0;i<256;i++)//数组数据清零
- data[i]=0;
- Uart_Printf("ReadtestdatafromAT24C02\n");
- for(i=0;i<256;i++)
- Rd24C02(0xa0,(U8)i,&(data[i]));//读取AT24C02的数据放入data数组中
- for(i=0;i<16;i++)
- {
- for(j=0;j<16;j++)
- Uart_Printf("%2x",data[i*16+j]);//打印从AT24C02读出的数据
- Uart_Printf("\n");
- }
- }
- voidWr24C02(U32slvAddr,U32addr,U8data)//slvAddr为从地址
- {//addr为字节地址,data为写入的数据
- iicMode=WRDATA;//写数据模式
- iicPt=0;
- iicData[0]=(U8)addr;
- iicData[1]=data;
- iicDataCount=2;//根据AT24C02字节写的发式,要写从地址和字节地址
- rIICDS=slvAddr;//把0xa0地址写入到数据移位寄存器IICDS
- //MasterTxmode,Start(Write),IIC-busdataoutputenable
- //Busarbitrationsucessful,AddressasslavestatusflagCleared,
- //Addresszerostatusflagcleared,Lastreceivedbitis0
- rIICSTAT=0xf0;//
- //Clearingthependingbitisntneededbecausethependingbithasbeencleared.
- while(iicDataCount!=-1)
- Run_IicPoll();
- iicMode=POLLACK;
- while(1)
- {
- rIICDS=slvAddr;
- iicStatus=0x100;//Tocheckif_iicStatusischanged
- rIICSTAT=0xf0;//MasterTx,Start,OutputEnable,Sucessful,Cleared,Cleared,0
- rIICCON=0xaf;//ResumesIICoperation.
- while(iicStatus==0x100)
- Run_IicPoll();
- if(!(iicStatus&0x1))
- break;//WhenACKisreceived
- }
- rIICSTAT=0xd0;//MasterTxcondition,Stop(Write),OutputEnable
- rIICCON=0xaf;//ResumesIICoperation.
- Delay(1);//Waituntilstopcondtionisineffect.
- //Writeiscompleted.
- }
- //************************[_Rd24C02]********************************
- voidRd24C02(U32slvAddr,U32addr,U8*data)
- {
- iicMode=SETRDADDR;//设置要从从机读取数据的从地址
- iicPt=0;
- iicData[0]=(U8)addr;
- iicDataCount=1;//写从地址
- rIICDS=slvAddr;
- rIICSTAT=0xf0;//MasTx,Start
- //Clearingthependingbitisntneededbecausethependingbithasbeencleared.
- while(iicDataCount!=-1)
- Run_IicPoll();
- iicMode=RDDATA;//读数据模式
- iicPt=0;
- iicDataCount=1;//
- rIICDS=slvAddr;
- rIICSTAT=0xb0;//MasterRx,Start
- rIICCON=0xaf;//ResumesIICoperation.
- while(iicDataCount!=-1)
- Run_IicPoll();
- *data=iicData[1];
- }
- voidRun_IicPoll(void)
- {
- if(rIICCON&0x10)//Tx/Rx中断使能
- IicPoll();
- }
- voidIicPoll(void)
- {
- U32iicSt,i;
- iicSt=rIICSTAT;//ICC状态寄存器
- if(iicSt&0x8){}//总线仲裁失败
- if(iicSt&0x4){}//从地址与ICCADD地址匹配
- if(iicSt&0x2){}//从地址为00000000b
- if(iicSt&0x1){}//未收到ACK
- switch(iicMode)
- {
- casePOLLACK:
- iicStatus=iicSt;
- break;
- caseRDDATA://从从机中读取数据
- if((iicDataCount--)==0)
- {
- iicData[iicPt++]=rIICDS;
- rIICSTAT=0x90;//StopMasRxcondition
- rIICCON=0xaf;//ResumesIICoperation.
- Delay(1);//Waituntilstopcondtionisineffect.
- //Toolongtime...
- //Thependingbitwillnotbesetafterissuingstopcondition.
- break;
- }
- iicData[iicPt++]=rIICDS;
- //Thelastdatahastobereadwithnoack.
- if((iicDataCount)==0)
- rIICCON=0x2f;//ResumesIICoperationwithNOACK.
- else
- rIICCON=0xaf;//ResumesIICoperationwithACK
- break;
- caseWRDATA://写数据到从机
- if((iicDataCount--)==0)
- {
- rIICSTAT=0xd0;//st
S3C2440IIC总线接 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)