一、仿真原理图:
二、运行效果:
三、STM32CubeMX配置:
1)、时钟配置:
2)、SPI配置:
四、软件部分:
1)、SPI初始化:
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
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_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
2)、时钟配置:
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
3)、MCP4161 SPI读写:
/**************************************************************************************
* Function Name : MCP4161_Write_Data
* Description :
**************************************************************************************/
void MCP4161_Write_Data(uint8_t address,uint8_t command,uint8_t data)
{
uint8_t buffer;
buffer = ((address << 4) + (command << 2)) & 0xfc;
MCP4161_CS(0);
HAL_SPI_Transmit(&MCP4161_SPI_PORT, &buffer, sizeof(buffer), HAL_MAX_DELAY);
HAL_SPI_Transmit(&MCP4161_SPI_PORT, &data, sizeof(data), HAL_MAX_DELAY);
MCP4161_CS(1);
}
/**************************************************************************************
* Function Name : MCP4161_Read_Data
* Description :
**************************************************************************************/
uint16_t MCP4161_Read_Data(uint8_t command)
{
uint16_t data;
uint8_t msb,lsb;
MCP4161_CS(0);
//HAL_SPI_Transmit(&MCP4161_SPI_PORT, &command, sizeof(command), HAL_MAX_DELAY);
HAL_SPI_Receive(&MCP4161_SPI_PORT, &msb, sizeof(msb), HAL_MAX_DELAY);
HAL_SPI_Receive(&MCP4161_SPI_PORT, &lsb, sizeof(lsb), HAL_MAX_DELAY);
MCP4161_CS(1);
data = (((msb << 8) + lsb)) & 0x1fff;
return data;
}