You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "main.h"
  6. #include "usart.h"
  7. #include "tim.h"
  8. #define READ_CMD 0x80
  9. #define SPI_TIMEOUT_VALUE 1000
  10. #define SPI_CS_1() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_SET) /* */
  11. #define SPI_CS_0() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_RESET) /* */
  12. #define SPI_SCK_1() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_SET) /* SCK = 1 */
  13. #define SPI_SCK_0() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_RESET) /* SCK = 0 */
  14. #define SPI_MOSI_1() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_SET) /* MOSI = 1 */
  15. #define SPI_MOSI_0() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_RESET) /* MOSI = 0 */
  16. #define SPI_READ_MISO() HAL_GPIO_ReadPin(MISO_GPIO_Port, MISO_Pin) /* 读MISO口线状态 */
  17. #define Dummy_Byte 0xFF // 读取时MISO发送的数据,可以为任意数据
  18. #define delay_us 5
  19. // 50us
  20. //
  21. // CS 11000
  22. // SCK
  23. // MOSI
  24. //
  25. //
  26. // uint8_t table[3][52] = {
  27. // {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
  28. // {1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  29. // {1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
  30. // };
  31. void sys_delay_us(uint32_t us)
  32. {
  33. uint16_t counter = 0;
  34. __HAL_TIM_SET_COUNTER(&htim7, 0);
  35. HAL_TIM_Base_Start(&htim7);
  36. while (counter < us)
  37. {
  38. counter = __HAL_TIM_GET_COUNTER(&htim7);
  39. }
  40. HAL_TIM_Base_Stop(&htim7);
  41. }
  42. // void dotable()
  43. // {
  44. // for (size_t i = 0; i < 52; i++)
  45. // {
  46. // table[0][i] ? SPI_CS_1() : SPI_CS_0();
  47. // table[1][i] ? SPI_SCK_1() : SPI_SCK_0();
  48. // table[2][i] ? SPI_MOSI_1() : SPI_MOSI_0();
  49. // sys_delay_us(5);
  50. // }
  51. // }
  52. // SPI可以同时读取和写入数据,因此一个函数即可满足要求
  53. uint8_t SPI_ReadWriteByte(uint8_t txData)
  54. {
  55. uint8_t i;
  56. uint8_t rxData = 0;
  57. SPI_CS_0();
  58. sys_delay_us(delay_us); // symbol tsw
  59. SPI_SCK_0();
  60. sys_delay_us(delay_us + 3);
  61. for (i = 0; i < 8; i++)
  62. {
  63. SPI_SCK_1();
  64. sys_delay_us(1);
  65. // 数据发送
  66. if (txData & 0x80)
  67. {
  68. SPI_MOSI_1();
  69. }
  70. else
  71. {
  72. SPI_MOSI_0();
  73. }
  74. txData <<= 1;
  75. sys_delay_us(delay_us);
  76. SPI_SCK_0();
  77. sys_delay_us(delay_us);
  78. // 数据接收
  79. // rxData <<= 1;
  80. // if (SPI_READ_MISO())
  81. // {
  82. // rxData |= 0x01;
  83. // }
  84. sys_delay_us(delay_us);
  85. }
  86. for (i = 0; i < 8; i++)
  87. {
  88. SPI_SCK_1();
  89. sys_delay_us(delay_us);
  90. sys_delay_us(delay_us);
  91. SPI_SCK_0();
  92. sys_delay_us(delay_us);
  93. sys_delay_us(delay_us);
  94. }
  95. SPI_SCK_1();
  96. SPI_MOSI_1(); // 数据线空闲高电平
  97. sys_delay_us(delay_us); // symbol thi
  98. sys_delay_us(delay_us); // symbol tsw
  99. SPI_CS_1();
  100. return rxData;
  101. }
  102. uint8_t SPI_ReadByte(void)
  103. {
  104. return SPI_ReadWriteByte(Dummy_Byte);
  105. }
  106. void SPI_WriteByte(uint8_t txData)
  107. {
  108. (void)SPI_ReadWriteByte(txData);
  109. }
  110. void user_main()
  111. {
  112. uint8_t temp;
  113. SPI_CS_1(); // 片选
  114. SPI_SCK_1(); // 时钟空闲高电平
  115. SPI_MOSI_1(); // 数据线空闲高电平
  116. while (1)
  117. {
  118. // dotable();
  119. SPI_ReadWriteByte(0x86);
  120. // HAL_UART_Transmit(&huart1, &temp, 1, 100);
  121. HAL_Delay(10);
  122. }
  123. }