iic.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "iic.h"
  2. #include "yc11xx.h"
  3. //总线引脚定义
  4. #define SDA 6
  5. #define SCL 25
  6. #define IIC_HIGH (0x3f)
  7. #define IIC_LOW (0x3e)
  8. #define IIC_RCV 0x40
  9. void Delay() //@11.0592MHz
  10. {
  11. while(0);
  12. }
  13. //总线启动条件
  14. void IIC_Start(void)
  15. {
  16. HWRITE(0x8080+SCL,IIC_HIGH);
  17. HWRITE(0x8080+SDA,IIC_HIGH);
  18. Delay();
  19. HWRITE(0x8080+SDA,IIC_LOW);
  20. Delay();
  21. HWRITE(0x8080+SCL,IIC_LOW);
  22. }
  23. //总线停止条件
  24. void IIC_Stop(void)
  25. {
  26. HWRITE(0x8080+SCL,IIC_LOW);
  27. HWRITE(0x8080+SDA,IIC_LOW);
  28. Delay();
  29. HWRITE(0x8080+SCL,IIC_HIGH);
  30. Delay();
  31. HWRITE(0x8080+SDA,IIC_HIGH);
  32. }
  33. //发送应答
  34. void IIC_SendAck(unsigned char ackbit)
  35. {
  36. HWRITE(0x8080+SCL,IIC_LOW);
  37. HWRITE(0x8080+SDA,(ackbit));
  38. Delay();
  39. HWRITE(0x8080+SCL,IIC_HIGH);
  40. Delay();
  41. HWRITE(0x8080+SCL,IIC_LOW);
  42. //HWRITE(0x8080+SDA,IIC_HIGH);
  43. //Delay();
  44. }
  45. //等待应答
  46. unsigned char IIC_WaitAck(void)
  47. {
  48. HWRITE(0x8080+SDA,IIC_RCV|0x40);
  49. HWRITE(0x8080+SCL,IIC_LOW);
  50. Delay();
  51. HWRITE(0x8080+SCL,IIC_HIGH);
  52. int cnt=0;
  53. while(HREAD(0x831c)&0x10){
  54. cnt++;
  55. Delay();
  56. if(cnt>256)return 0;
  57. };
  58. HWRITE(0x8080+SCL,IIC_LOW);
  59. Delay();
  60. return 1;
  61. }
  62. //通过I2C总线发送数据
  63. void IIC_SendByte(unsigned char byt)
  64. {
  65. unsigned char i;
  66. for (i = 0; i < 8; i++) {
  67. HWRITE(0x8080+SCL,IIC_LOW);
  68. Delay();
  69. if (byt & 0x80)
  70. HWRITE(0x8080+SDA,IIC_HIGH);
  71. else
  72. HWRITE(0x8080+SDA,IIC_LOW);
  73. Delay();
  74. HWRITE(0x8080+SCL,IIC_HIGH);
  75. byt <<= 1;
  76. Delay();
  77. }
  78. HWRITE(0x8080+SCL,IIC_LOW);
  79. //HWRITE(0x8080+SDA,IIC_HIGH);
  80. }
  81. //从I2C总线上接收数据
  82. unsigned char IIC_RecByte(void)
  83. {
  84. unsigned char i, da;
  85. da=0;
  86. HWRITE(0x8080+SDA,IIC_RCV);
  87. for (i = 0; i < 8; i++) {
  88. HWRITE(0x8080+SCL,IIC_LOW);
  89. Delay();
  90. HWRITE(0x8080+SCL,IIC_HIGH);
  91. da <<= 1;
  92. if (HREAD(CORE_GPIO_IN)&(1<<SDA))
  93. da |= 1;
  94. Delay();
  95. }
  96. //HWRITE(0x8080+SDA,IIC_HIGH);
  97. return da;
  98. }
  99. void IIC_ReceiveData(unsigned char *Src, unsigned short Srclen, unsigned char *Dest, unsigned short Destlen){
  100. IIC_Start();
  101. for(int i=0;i<2;i++){
  102. IIC_SendByte(Src[i]);
  103. IIC_WaitAck();
  104. //if(i==1)IIC_Start();
  105. }
  106. IIC_Start();
  107. for(int i=2;i<Srclen;i++){
  108. IIC_SendByte(Src[i]);
  109. IIC_WaitAck();
  110. }
  111. for(int i=0;i<Destlen-1;i++){
  112. Dest[i]=IIC_RecByte();
  113. IIC_SendAck(0x3e);
  114. }
  115. Dest[Destlen-1]=IIC_RecByte();
  116. IIC_SendAck(0x3f);
  117. IIC_Stop();
  118. }
  119. void IIC_SendData(unsigned char *Src, unsigned short len){
  120. IIC_Start();
  121. for(int i=0;i<len;i++){
  122. IIC_SendByte(Src[i]);
  123. IIC_WaitAck();
  124. }
  125. IIC_Stop();
  126. }