/*-------------------------------------------------------------------------------------- * @file SPISetup.c * @author ZhangJing * @version base on stm32f0x * @date 2015.09.11 * @brief SPI驱动 ---------------------------------------------------------------------------------------*/ #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "SPISetup.h" /************************************************************************************* * Function: SPIInit * Object: 用I/O口模拟的SPI初始化 * 输入: 无 * 输出: 无 * 备注: 1、使用引脚GPIOD的Pin_4 | Pin_3 | Pin_2 | Pin_9 | Pin_0 | Pin_1 | Pin_8; * 相对应CS,RST,A0,LCD_SCK,ERD,RWR,MOSI **************************************************************************************/ #ifdef OLD_LCD void SPIInit( void ) { /*定义一个GPIO_InitTypeDef类型的结构体*/ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //PD14-MOSI PD12-MISO GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Enable the GPIO_SPEAKER Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); /*选择要控制的GPIOD引脚*/ 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 /*设置引脚模式为通用推挽输出*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*设置引脚速率为50MHz */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*调用库函数,初始化GPIOD*/ GPIO_Init(GPIOD, &GPIO_InitStructure); } #else void SPIInit( void ) { /*定义一个GPIO_InitTypeDef类型的结构体*/ GPIO_InitTypeDef GPIO_InitStructure; // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PD14-MOSI PD12-MISO // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOD, &GPIO_InitStructure); /* Enable the GPIO_SPEAKER Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); /*选择要控制的GPIOD引脚*/ 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 /*设置引脚模式为通用推挽输出*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*设置引脚速率为50MHz */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*调用库函数,初始化GPIOD*/ GPIO_Init(GPIOD, &GPIO_InitStructure); } #endif /************************************************************************************* * Function: SPIWriteByte * Object: SPI写操作 * 输入: uint8_t Byte 写数据 * 输出: 无 * 备注: uint8_t Read;写状态读取 **************************************************************************************/ uint8_t SPIWriteByte( uint8_t Byte ) { uint8_t i,Read; for( i = 0; i < 8; i++) { SPIClrClk(); SPISetData(Byte); Byte <<= 1; SPISetClk(); //Read <<= 1; //Read |= SPIv_ReadData(); } //SPIv_ClrClk(); return Read; }