1
0

demo2016_06_05 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. Author : ADI - Apps www.analog.com/MicroConverter
  3. Date : May. 2007
  4. File : I2C_Slave.c
  5. Hardware : Applicable to ADuC702x rev H or I silicon
  6. Currently targetting ADuC7028.
  7. Description : I2C Slave to demonstrate with I2C_Master.
  8. Operates in two modes, read & write (called recieve and
  9. transmit here). At the begining of an I2C transmission, the
  10. Master sends an address. The LSB of this address determines
  11. if the Master is to read (1) or write (0).
  12. ***************************************************************************/
  13. #include<ADuC7028.h>
  14. void My_IRQ_Handler(void);
  15. typedef unsigned char BYTE; //8-bit
  16. typedef unsigned short int HALFWORD; //16-bit
  17. typedef unsigned int WORD; //32-bit
  18. #define TRUE 1
  19. #define FALSE 0
  20. #define HIGH 1
  21. #define LOW 0
  22. short i = 0, dat[256];
  23. int r = 0;
  24. int w = 0;
  25. BYTE i2c_first = TRUE;
  26. int main()
  27. {
  28. dat[0] = 0;
  29. dat[1] = 1;
  30. dat[2] = 2;
  31. dat[3] = 3;
  32. dat[4] = 4;
  33. dat[5] = 5;
  34. dat[6] = 6;
  35. dat[7] = 7;
  36. i=1;
  37. // I2C on P1.0 and P1.1
  38. GP1CON = 0x22;
  39. IRQ=My_IRQ_Handler;
  40. IRQEN = 0x200; // I2C0 Slave Interupt
  41. I2C0CFG = 0x4001;//0x01; // Slave Enable
  42. I2C0ID0 = 0xA0; // Slave ID
  43. I2C0STX = 0x77;
  44. while (1)
  45. {
  46. };
  47. // return 0;
  48. }
  49. /*************************************************/
  50. /*************************************************/
  51. /************ IRQ Service Routine *************/
  52. /*************************************************/
  53. /*************************************************/
  54. unsigned int status;
  55. unsigned int x;
  56. unsigned int addr;
  57. void My_IRQ_Handler()
  58. {
  59. status = I2C0SSTA;
  60. if (((status & 0x02000)==0x02000)) // Slave Transmit IRQ
  61. {
  62. r=0;
  63. w=-1;
  64. i++;
  65. }
  66. // Slave Recieve
  67. if ((status & 0x08)==0x08) // Slave Recieve IRQ
  68. {
  69. if(w==-1){
  70. addr = I2C0SRX;
  71. w++;
  72. }
  73. else{
  74. /* if(w==0){
  75. }
  76. else if(w==1){
  77. }*/
  78. dat[addr+w] = I2C0SRX;
  79. w++;
  80. }
  81. //I2C0STX = dat[addr+r];
  82. //I2C0STX = dat[addr+r];
  83. //i++;
  84. }
  85. // Slave Transmit
  86. if ((status & 0x04)==0x04) // Slave Transmit IRQ
  87. {
  88. I2C0STX = dat[addr+r];
  89. //I2C0STX = dat[addr+r];
  90. r++;
  91. // i++;
  92. }
  93. //i++;
  94. }