i want to create a 40KHz sine wave with a STM32 Controller and an external DAC. I created my SPI connection to the MCP4922 and sending a sine wave table to it. The init function of the SPI interface looks like this:
static 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_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
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();
}
}
At the beginning i had a sin wave table with 50 values. The MCP4922 was generating a nice wave like i expected it with 400 Hz. So i reduced the values.
uint16_t sine_wave[] =
{
0x800,0xcb3,0xf9b,0xf9b,0xcb3,0x800,0x34c,0x64,
0x64,0x34c,0x800,
};
It is still a sine wave but very edgy, the frequency increased to 4,3 KHz. The function i call to send the data to the mcp4922 looks like this.
void MCP4922_SendByte(uint16_t dac_data)
{
data[1] = ((dac_data >> 8) & 0x0F) | 0x30;
data[0] = dac_data;
HAL_GPIO_WritePin(GPIOA, SPI1_CS_Pin, 0);
HAL_SPI_Transmit(&hspi1,data, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA, SPI1_CS_Pin, 1);
}
So my question is, how can i change my code or my hardware setup to generate a 40KHz sine wave. Is it possible to increase the speed of my SPI transmission?