我把以前的工程中的模拟SPI换成cubemx生成的硬件SPI代码,驱动OLED。但是一直没效果。之前没用过CUBEMX,不知道问题在哪。
生成的SPI.c文件如下
- #include "spi.h"
- #include "gpio.h"
- SPI_HandleTypeDef hspi1;
- void MX_SPI1_Init(void)
- {
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
- hspi1.Init.CRCPolynomial = 10;
- HAL_SPI_Init(&hspi1);
- }
- void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(hspi->Instance==SPI1)
- {
- __SPI1_CLK_ENABLE();
-
- /**SPI1 GPIO Configuration
- PA5 ------> SPI1_SCK
- PA7 ------> SPI1_MOSI
- */
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- }
- void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
- {
- if(hspi->Instance==SPI1)
- {
- __SPI1_CLK_DISABLE();
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7);
- }
-
- }
复制代码mian.c中的初始化如下
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_SPI1_Init();
- while (1)
- {
- }
复制代码
下面是SPI写函数。、
- void SPI_WriteCmd(uint8_t cmd)
- {
- DC_L;
- HAL_SPI_Transmit(&hspi1, (uint8_t *)cmd,1,100)
- while(HAL_SPI_GetState(&hspi1)==HAL_SPI_STATE_BUSY_TX);
- }
- void SPI_Write_Data(uint8_t data)
- {
- DC_H;
- HAL_SPI_Transmit(&hspi1, (uint8_t *)data,1,100)
- while(HAL_SPI_GetState(&hspi1)==HAL_SPI_STATE_BUSY_TX);
- }
复制代码那位大神看得出问题在哪里吗
此帖出自
小平头技术问答
一周热门 更多>