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.

114 lines
2.5 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
  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. void sys_delay_us(uint32_t us)
  19. {
  20. uint16_t counter = 0;
  21. __HAL_TIM_SET_COUNTER(&htim7, 0);
  22. HAL_TIM_Base_Start(&htim7);
  23. while (counter < us)
  24. {
  25. counter = __HAL_TIM_GET_COUNTER(&htim7);
  26. }
  27. HAL_TIM_Base_Stop(&htim7);
  28. }
  29. // SPI可以同时读取和写入数据,因此一个函数即可满足要求
  30. uint8_t SPI_ReadWriteByte(uint8_t txData)
  31. {
  32. uint8_t i;
  33. uint8_t rxData = 0;
  34. SPI_CS_0();
  35. sys_delay_us(1); // symbol tsw
  36. SPI_SCK_0();
  37. sys_delay_us(1);
  38. for (i = 0; i < 8; i++)
  39. {
  40. SPI_SCK_1();
  41. sys_delay_us(1);
  42. // 数据发送
  43. if (txData & 0x80)
  44. {
  45. SPI_MOSI_1();
  46. }
  47. else
  48. {
  49. SPI_MOSI_0();
  50. }
  51. txData <<= 1;
  52. sys_delay_us(1);
  53. SPI_SCK_0();
  54. sys_delay_us(1);
  55. // 数据接收
  56. // rxData <<= 1;
  57. // if (SPI_READ_MISO())
  58. // {
  59. // rxData |= 0x01;
  60. // }
  61. sys_delay_us(1);
  62. }
  63. for (i = 0; i < 8; i++)
  64. {
  65. SPI_SCK_1();
  66. sys_delay_us(1);
  67. sys_delay_us(1);
  68. SPI_SCK_0();
  69. sys_delay_us(1);
  70. sys_delay_us(1);
  71. }
  72. SPI_SCK_1();
  73. SPI_MOSI_1(); // 数据线空闲高电平
  74. sys_delay_us(1); // symbol thi
  75. sys_delay_us(1); // symbol tsw
  76. SPI_CS_1();
  77. return rxData;
  78. }
  79. uint8_t SPI_ReadByte(void)
  80. {
  81. return SPI_ReadWriteByte(Dummy_Byte);
  82. }
  83. void SPI_WriteByte(uint8_t txData)
  84. {
  85. (void)SPI_ReadWriteByte(txData);
  86. }
  87. void user_main()
  88. {
  89. uint8_t temp;
  90. SPI_CS_1(); // 片选
  91. SPI_SCK_1(); // 时钟空闲高电平
  92. SPI_MOSI_1(); // 数据线空闲高电平
  93. while (1)
  94. {
  95. temp = SPI_ReadWriteByte(0x86);
  96. HAL_UART_Transmit(&huart1, &temp, 1, 100);
  97. HAL_Delay(100);
  98. }
  99. }