- void MM_SPI_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- SPI_InitTypeDef SPI_InitStruct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- SPI_StructInit(&SPI_InitStruct);
- SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
- SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStruct.SPI_DataWidth = 8;
- SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
- SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_Init(SPI2, &SPI_InitStruct);
- SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Rx);
- SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Tx);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_5);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_5);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- SPI_Cmd(SPI2, ENABLE);
- }
其他引脚初始化:
- #define TFT_RS_reset GPIO_ResetBits(GPIOC, GPIO_Pin_12)
- #define TFT_RS_set GPIO_SetBits(GPIOC, GPIO_Pin_12)
- #define TFT_RESET_reset GPIO_ResetBits(GPIOB, GPIO_Pin_0)
- #define TFT_RESET_set GPIO_SetBits(GPIOB, GPIO_Pin_0)
- #define TFT_BL_SET GPIO_SetBits(GPIOD, GPIO_Pin_2)
- #define TFT_BL_RESET GPIO_ResetBits(GPIOD, GPIO_Pin_2)
- #define SPI1_CS_OUT0 GPIO_ResetBits(GPIOB, GPIO_Pin_12)
- #define SPI1_CS_OUT1 GPIO_SetBits(GPIOB, GPIO_Pin_12)
- void MM_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStruct);
-
- TFT_init();
- }
效果如下: