SPISetup.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*--------------------------------------------------------------------------------------
  2. * @file SPISetup.c
  3. * @author ZhangJing
  4. * @version base on stm32f0x
  5. * @date 2015.09.11
  6. * @brief SPI驱动
  7. ---------------------------------------------------------------------------------------*/
  8. #include "stm32f10x_gpio.h"
  9. #include "stm32f10x_rcc.h"
  10. #include "stm32f10x_gpio.h"
  11. #include "SPISetup.h"
  12. /*************************************************************************************
  13. * Function: SPIInit
  14. * Object: 用I/O口模拟的SPI初始化
  15. * 输入: 无
  16. * 输出: 无
  17. * 备注: 1、使用引脚GPIOD的Pin_4 | Pin_3 | Pin_2 | Pin_9 | Pin_0 | Pin_1 | Pin_8;
  18. * 相对应CS,RST,A0,LCD_SCK,ERD,RWR,MOSI
  19. **************************************************************************************/
  20. #ifdef OLD_LCD
  21. void SPIInit( void )
  22. {
  23. /*定义一个GPIO_InitTypeDef类型的结构体*/
  24. GPIO_InitTypeDef GPIO_InitStructure;
  25. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  26. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //PD14-MOSI PD12-MISO
  27. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  28. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_Init(GPIOD, &GPIO_InitStructure);
  30. /* Enable the GPIO_SPEAKER Clock */
  31. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  32. /*选择要控制的GPIOD引脚*/
  33. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; //CS,RST,RS/DC,MISO,LCD_SCK
  34. /*设置引脚模式为通用推挽输出*/
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  36. /*设置引脚速率为50MHz */
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. /*调用库函数,初始化GPIOD*/
  39. GPIO_Init(GPIOD, &GPIO_InitStructure);
  40. }
  41. #else
  42. void SPIInit( void )
  43. {
  44. /*定义一个GPIO_InitTypeDef类型的结构体*/
  45. GPIO_InitTypeDef GPIO_InitStructure;
  46. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  47. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PD14-MOSI PD12-MISO
  48. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  49. // // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  50. // GPIO_Init(GPIOD, &GPIO_InitStructure);
  51. /* Enable the GPIO_SPEAKER Clock */
  52. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  53. /*选择要控制的GPIOD引脚*/
  54. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_9 | GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8; //CS,RST,A0,LCD_SCK,ERD,RWR,MOSI
  55. /*设置引脚模式为通用推挽输出*/
  56. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  57. /*设置引脚速率为50MHz */
  58. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  59. /*调用库函数,初始化GPIOD*/
  60. GPIO_Init(GPIOD, &GPIO_InitStructure);
  61. }
  62. #endif
  63. /*************************************************************************************
  64. * Function: SPIWriteByte
  65. * Object: SPI写操作
  66. * 输入: uint8_t Byte 写数据
  67. * 输出: 无
  68. * 备注: uint8_t Read;写状态读取
  69. **************************************************************************************/
  70. uint8_t SPIWriteByte( uint8_t Byte )
  71. {
  72. uint8_t i,Read;
  73. for( i = 0; i < 8; i++)
  74. {
  75. SPIClrClk();
  76. SPISetData(Byte);
  77. Byte <<= 1;
  78. SPISetClk();
  79. //Read <<= 1;
  80. //Read |= SPIv_ReadData();
  81. }
  82. //SPIv_ClrClk();
  83. return Read;
  84. }