微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > S3C2440 IIC总线接口

S3C2440 IIC总线接口

时间:11-21 来源:互联网 点击:

  1. #include
  2. #include"2440addr.h"
  3. #include"2440lib.h"
  4. #include"def.h"
  5. #defineWRDATA(1)
  6. #definePOLLACK(2)
  7. #defineRDDATA(3)
  8. #defineSETRDADDR(4)
  9. #defineIICBUFSIZE0x20
  10. staticU8iicData[IICBUFSIZE];
  11. staticvolatileintiicDataCount;
  12. staticvolatileintiicStatus;
  13. staticvolatileintiicMode;
  14. staticintiicPt;
  15. voidWr24C02(U32slvAddr,U32addr,U8data);
  16. voidRd24C02(U32slvAddr,U32addr,U8*data);
  17. voidIicPoll(void);
  18. voidRun_IicPoll(void);
  19. voidMain(void)
  20. {
  21. unsignedinti,j;
  22. staticU8data[256];//用于存储AT24C02读出的数据
  23. SelectFclk(2);//设置系统时钟400M
  24. ChangeClockDivider(2,1);//设置分频1:4:8
  25. CalcBusClk();//计算总线频率
  26. rGPHCON&=~((3<4)|(3<6));
  27. rGPHCON|=(2<4)|(2<6);//GPH2--TXD[0];GPH3--RXD[0]
  28. rGPHUP=0x00;//使能上拉功能
  29. Uart_Init(0,115200);
  30. Uart_Select(0);
  31. Uart_Printf("[IICTest(Polling)usingAT24C020]\n");
  32. rGPEUP|=0xc000;//关上拉
  33. rGPECON&=~0xf0000000;
  34. rGPECON|=0xa0000000;//GPE15:IICSDA,GPE14:IICSCL
  35. //使能应答,IIC总线时钟IICCLK=PCLK/16,使能中断,发送时钟IICCLK/16
  36. rIICCON=(1<7)|(0<6)|(1<5)|(0xf);
  37. rIICADD=0x10;//2440从机地址=[7:1]
  38. rIICSTAT=0x10;//IIC总线数据输出使能(Rx/Tx)
  39. Uart_Printf("WritetestdataintoAT24C02\n");
  40. for(i=0;i<256;i++)
  41. Wr24C02(0xa0,(U8)i,i);//写入数据到AT24C02
  42. for(i=0;i<256;i++)//数组数据清零
  43. data[i]=0;
  44. Uart_Printf("ReadtestdatafromAT24C02\n");
  45. for(i=0;i<256;i++)
  46. Rd24C02(0xa0,(U8)i,&(data[i]));//读取AT24C02的数据放入data数组中
  47. for(i=0;i<16;i++)
  48. {
  49. for(j=0;j<16;j++)
  50. Uart_Printf("%2x",data[i*16+j]);//打印从AT24C02读出的数据
  51. Uart_Printf("\n");
  52. }
  53. }
  54. voidWr24C02(U32slvAddr,U32addr,U8data)//slvAddr为从地址
  55. {//addr为字节地址,data为写入的数据
  56. iicMode=WRDATA;//写数据模式
  57. iicPt=0;
  58. iicData[0]=(U8)addr;
  59. iicData[1]=data;
  60. iicDataCount=2;//根据AT24C02字节写的发式,要写从地址和字节地址
  61. rIICDS=slvAddr;//把0xa0地址写入到数据移位寄存器IICDS
  62. //MasterTxmode,Start(Write),IIC-busdataoutputenable
  63. //Busarbitrationsucessful,AddressasslavestatusflagCleared,
  64. //Addresszerostatusflagcleared,Lastreceivedbitis0
  65. rIICSTAT=0xf0;//
  66. //Clearingthependingbitisntneededbecausethependingbithasbeencleared.
  67. while(iicDataCount!=-1)
  68. Run_IicPoll();
  69. iicMode=POLLACK;
  70. while(1)
  71. {
  72. rIICDS=slvAddr;
  73. iicStatus=0x100;//Tocheckif_iicStatusischanged
  74. rIICSTAT=0xf0;//MasterTx,Start,OutputEnable,Sucessful,Cleared,Cleared,0
  75. rIICCON=0xaf;//ResumesIICoperation.
  76. while(iicStatus==0x100)
  77. Run_IicPoll();
  78. if(!(iicStatus&0x1))
  79. break;//WhenACKisreceived
  80. }
  81. rIICSTAT=0xd0;//MasterTxcondition,Stop(Write),OutputEnable
  82. rIICCON=0xaf;//ResumesIICoperation.
  83. Delay(1);//Waituntilstopcondtionisineffect.
  84. //Writeiscompleted.
  85. }
  86. //************************[_Rd24C02]********************************
  87. voidRd24C02(U32slvAddr,U32addr,U8*data)
  88. {
  89. iicMode=SETRDADDR;//设置要从从机读取数据的从地址
  90. iicPt=0;
  91. iicData[0]=(U8)addr;
  92. iicDataCount=1;//写从地址
  93. rIICDS=slvAddr;
  94. rIICSTAT=0xf0;//MasTx,Start
  95. //Clearingthependingbitisntneededbecausethependingbithasbeencleared.
  96. while(iicDataCount!=-1)
  97. Run_IicPoll();
  98. iicMode=RDDATA;//读数据模式
  99. iicPt=0;
  100. iicDataCount=1;//
  101. rIICDS=slvAddr;
  102. rIICSTAT=0xb0;//MasterRx,Start
  103. rIICCON=0xaf;//ResumesIICoperation.
  104. while(iicDataCount!=-1)
  105. Run_IicPoll();
  106. *data=iicData[1];
  107. }
  108. voidRun_IicPoll(void)
  109. {
  110. if(rIICCON&0x10)//Tx/Rx中断使能
  111. IicPoll();
  112. }
  113. voidIicPoll(void)
  114. {
  115. U32iicSt,i;
  116. iicSt=rIICSTAT;//ICC状态寄存器
  117. if(iicSt&0x8){}//总线仲裁失败
  118. if(iicSt&0x4){}//从地址与ICCADD地址匹配
  119. if(iicSt&0x2){}//从地址为00000000b
  120. if(iicSt&0x1){}//未收到ACK
  121. switch(iicMode)
  122. {
  123. casePOLLACK:
  124. iicStatus=iicSt;
  125. break;
  126. caseRDDATA://从从机中读取数据
  127. if((iicDataCount--)==0)
  128. {
  129. iicData[iicPt++]=rIICDS;
  130. rIICSTAT=0x90;//StopMasRxcondition
  131. rIICCON=0xaf;//ResumesIICoperation.
  132. Delay(1);//Waituntilstopcondtionisineffect.
  133. //Toolongtime...
  134. //Thependingbitwillnotbesetafterissuingstopcondition.
  135. break;
  136. }
  137. iicData[iicPt++]=rIICDS;
  138. //Thelastdatahastobereadwithnoack.
  139. if((iicDataCount)==0)
  140. rIICCON=0x2f;//ResumesIICoperationwithNOACK.
  141. else
  142. rIICCON=0xaf;//ResumesIICoperationwithACK
  143. break;
  144. caseWRDATA://写数据到从机
  145. if((iicDataCount--)==0)
  146. {
  147. rIICSTAT=0xd0;//st

上一篇:单片机LED数码管
下一篇:S3C2440 AD转换器

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top