spi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. ******************************** STM32F10x *********************************
  3. * @文件名 : spi.c
  4. * @作者 : sun
  5. * @库版本 : V3.5.0
  6. * @文件版本 : V1.0.0
  7. * @日期 : 2016年05月09日
  8. * @摘要 : SPI源文件
  9. ******************************************************************************/
  10. /*----------------------------------------------------------------------------
  11. 更新日志:
  12. 2016-05-09 V1.0.0:初始版本
  13. ----------------------------------------------------------------------------*/
  14. /* 包含的头文件 --------------------------------------------------------------*/
  15. #include "spi.h"
  16. /************************************************
  17. 函数名称 : SPI_GPIO_Configuration
  18. 功 能 : SPI引脚配置
  19. 参 数 : 无
  20. 返 回 值 : 无
  21. 作 者 : sun
  22. *************************************************/
  23. void SPI_GPIO_Configuration(void)
  24. {
  25. GPIO_InitTypeDef GPIO_InitStructure;
  26. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
  27. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  29. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  30. GPIO_Init(GPIOA, &GPIO_InitStructure);
  31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33. GPIO_Init(GPIOA, &GPIO_InitStructure);
  34. GPIO_SetBits(GPIOA, GPIO_Pin_4);
  35. //GPIO_SetBits(GPIOA, GPIO_Pin_4|GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
  36. }
  37. /************************************************
  38. 函数名称 : SPI_Configuration
  39. 功 能 : SPI配置
  40. SPI为主模式,时钟线平时为低,上升沿采集数据
  41. 8位数据格式,软件控制片选,数据高位在前
  42. 参 数 : 无
  43. 返 回 值 : 无
  44. 作 者 : sun
  45. *************************************************/
  46. void SPI_Configuration(void)
  47. {
  48. SPI_InitTypeDef SPI_InitStructure;
  49. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  50. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  51. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  52. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  53. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  54. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  55. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 72/8 MHz
  56. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  57. SPI_InitStructure.SPI_CRCPolynomial = 7;
  58. SPI_Init(SPI1, &SPI_InitStructure);
  59. SPI_Cmd(SPI1, ENABLE);
  60. SPI_SSOutputCmd(SPI1, ENABLE);
  61. }
  62. /************************************************
  63. 函数名称 : SPI_Initializes
  64. 功 能 : SPI初始化
  65. 参 数 : 无
  66. 返 回 值 : 无
  67. 作 者 : sun
  68. *************************************************/
  69. void SPI_Initializes(void)
  70. {
  71. SPI_GPIO_Configuration();
  72. SPI_Configuration();
  73. }
  74. /************************************************
  75. 函数名称 : SPI_WriteReadByte
  76. 功 能 : 对SPI写读字节数据
  77. 参 数 : TxData --- 发送的字节数据
  78. 返 回 值 : 读回来的字节数据
  79. 作 者 : sun
  80. *************************************************/
  81. uint8_t SPI_WriteReadByte(uint8_t TxData)
  82. {
  83. while((SPI1->SR & SPI_I2S_FLAG_TXE) == (uint16_t)RESET);
  84. SPI1->DR = TxData;
  85. while((SPI1->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET);
  86. return SPI1->DR;
  87. }
  88. /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/