Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #define I2C_ACCELEROMETER_ADDRESS 0xD0 //( 0x68 + one bit)
  2. #define I2C_OWN_ADDRESS 0x72
  3.  
  4. //Init I2C communication to accelerometer
  5. // enable I2C0 clock
  6. rcu_periph_clock_enable(RCU_I2C0);
  7. // connect PB8 to I2C0_SCL
  8. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8);
  9. // connect PB9 to I2C0_SDA
  10. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_9);
  11. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_8);
  12. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  13. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9);
  14. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  15.  
  16. void I2C_Accelerometer_init(void){
  17. // configure I2C0 clock
  18. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  19. // configure I2C0 address
  20. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_OWN_ADDRESS);
  21. // enable I2C0
  22. i2c_enable(I2C0);
  23. // enable acknowledge
  24. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  25. }
  26.  
  27. extern uint8_t Accelerometer_X_High;
  28.  
  29. //periodically called by timers in it.c
  30. void GetAccelerometerData(void){
  31. uint8_t bytesToReadIndex=0;
  32.  
  33. //WAKE UP ACCELEROMETER (6B 00)
  34.  
  35. // wait until I2C bus is idle
  36. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  37. // send a start condition to I2C bus
  38. i2c_start_on_bus(I2C0);
  39.  
  40. // wait until SBSEND bit is set
  41. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  42.  
  43. // send slave address to I2C bus
  44. i2c_master_addressing(I2C0, I2C_ACCELEROMETER_ADDRESS, I2C_TRANSMITTER);
  45.  
  46. // wait until ADDSEND bit is set
  47. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  48.  
  49. // clear ADDSEND bit
  50. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  51.  
  52. // send a data byte
  53. i2c_data_transmit(I2C0,0x6B); //PWR MANAGEMENT
  54. // wait until the transmission data register is empty
  55. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  56. // send a data byte
  57. i2c_data_transmit(I2C0,0x00); //WAKE UP
  58. // wait until the transmission data register is empty
  59. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  60.  
  61. // send a stop condition to I2C bus
  62. i2c_stop_on_bus(I2C0);
  63.  
  64.  
  65. while(I2C_CTL0(I2C0)&0x0200);
  66.  
  67.  
  68. //REQUEST ACELEROMETER DATA (3B )
  69.  
  70. // wait until I2C bus is idle
  71. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  72. // send a start condition to I2C bus
  73. i2c_start_on_bus(I2C0);
  74.  
  75. // wait until SBSEND bit is set
  76. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  77.  
  78. // send slave address to I2C bus
  79. i2c_master_addressing(I2C0, I2C_ACCELEROMETER_ADDRESS, I2C_TRANSMITTER);
  80.  
  81. // wait until ADDSEND bit is set
  82. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  83. // clear ADDSEND bit
  84. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  85.  
  86. // send a data byte
  87. i2c_data_transmit(I2C0,0x3B); //ACCELEROMETER REGISTER
  88. // wait until the transmission data register is empty
  89. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  90.  
  91.  
  92. // send a stop condition to I2C bus
  93. i2c_stop_on_bus(I2C0);
  94.  
  95.  
  96. while(I2C_CTL0(I2C0)&0x0200);
  97.  
  98.  
  99.  
  100.  
  101. //now reopen the bus to receive the reply
  102.  
  103.  
  104.  
  105. // wait until I2C bus is idle
  106. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  107. // send a start condition to I2C bus
  108. i2c_start_on_bus(I2C0);
  109. // wait until SBSEND bit is set
  110. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  111. // send slave address to I2C bus
  112. i2c_master_addressing(I2C0, I2C_ACCELEROMETER_ADDRESS, I2C_RECEIVER);
  113. // wait until ADDSEND bit is set
  114. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  115.  
  116. // if we receive only one byte: reset ACKEN bit
  117. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  118.  
  119. //clear ADDSEND bit
  120. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  121.  
  122. // if we receive only one byte: send stop condition
  123. i2c_stop_on_bus(I2C0);
  124.  
  125.  
  126. /* wait until the RBNE bit is set */
  127. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  128. //read a data from I2C_DATA
  129. Accelerometer_X_High = i2c_data_receive(I2C0);
  130.  
  131. // if we receive more bytes: send a stop condition to I2C bus
  132. //i2c_stop_on_bus(I2C0);
  133. while(I2C_CTL0(I2C0)&0x0200);
  134. // enable acknowledge
  135. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement