Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /**
  2. * Keil project example for I2C - Multiple write and read bytes from SLAVE
  3. *
  4. * Before you start, select your target, on the right of the "Load" button
  5. *
  6. * @author Tilen Majerle
  7. * @email tilen@majerle.eu
  8. * @website http://stm32f4-discovery.com
  9. * @ide Keil uVision 5
  10. * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines"
  11. * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support
  12. * @stdperiph STM32F4xx/STM32F7xx HAL drivers required
  13. */
  14. /* Include core modules */
  15. #include "stm32fxxx_hal.h"
  16. /* Include my libraries here */
  17. #include "defines.h"
  18. #include "tm_stm32_disco.h"
  19. #include "tm_stm32_delay.h"
  20. #include "tm_stm32_i2c.h"
  21. #include <string.h>
  22.  
  23. /* PoStep60 device address */
  24. #define POSTEP60_ADDRESS 0x4d
  25.  
  26. /* Byte value read from external device */
  27. uint8_t data_array[6];
  28.  
  29. int main(void) {
  30.  
  31. int i;
  32. int32_t pos;
  33.  
  34. /* Init system clock for maximum system speed */
  35. TM_RCC_InitSystem();
  36.  
  37. /* Init HAL layer */
  38. HAL_Init();
  39.  
  40. /* Init leds */
  41. TM_DISCO_LedInit();
  42.  
  43. /* Init delay */
  44. TM_DELAY_Init();
  45.  
  46. /* Init I2C, SCL = PB8, SDA = PB9, available on Arduino headers and on all discovery boards */
  47. /* For STM32F4xx and STM32F7xx lines */
  48. TM_I2C_Init(I2C3, TM_I2C_PinsPack_1, 100000);
  49.  
  50. /* Format data array to write to device */
  51. data_array[0] = 0x01; /* Command = 0x01 */
  52. data_array[1] = 0x11; /* Data 1 */
  53. data_array[2] = 0x22; /* Data 2 */
  54. data_array[3] = 0x33; /* Data 3 */
  55. data_array[4] = 0xcc; /* Data 4 */
  56.  
  57. // Test loopback
  58. TM_I2C_WriteMultiNoRegister(I2C3, POSTEP60_ADDRESS, data_array, 5);
  59. HAL_Delay(1);
  60. for (i = 0; i<5; i++) data_array[i]=0;
  61. TM_I2C_ReadMultiNoRegister(I2C3, POSTEP60_ADDRESS, data_array, 5);
  62. HAL_Delay(1);
  63.  
  64.  
  65. // Run
  66. data_array[0] = 0x03; /* Command 0x03 – Set Run/Sleep mode */
  67. data_array[1] = 0xda; /* Run 0xDA */
  68. TM_I2C_WriteMultiNoRegister(I2C3, POSTEP60_ADDRESS, data_array, 2);
  69. HAL_Delay(1);
  70.  
  71. pos = 0;
  72.  
  73.  
  74. // Set zero
  75. data_array[0] = 0x5e; /* Command 0x5E – Set zero */
  76. TM_I2C_WriteMultiNoRegister(I2C3, POSTEP60_ADDRESS, data_array, 1);
  77. HAL_Delay(1);
  78.  
  79.  
  80. while (1) {
  81.  
  82. // Set position
  83. data_array[0] = 0x50; /* Command 0x50 – Set position */
  84. memcpy(&data_array[1], &pos, 4);
  85. TM_I2C_WriteMultiNoRegister(I2C3, POSTEP60_ADDRESS, data_array, 5);
  86. HAL_Delay(2000);
  87.  
  88. if (pos>0) pos = -5000; else pos=5000;
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement