uranyumx
Advanced Member level 4
Hello,
I want to generate a sine wave with a timer interrupt control. But I couldn't manage to get correct sine wave. I am using SMT32F767 as microcontroller and external DAC (DAC122S085CIMM/NOPB) . I am glad to hear your feedback about solution of the problem. I have also attached the screenshot of the oscilloscope when I probe at the DAC output.
I want to generate a sine wave with a timer interrupt control. But I couldn't manage to get correct sine wave. I am using SMT32F767 as microcontroller and external DAC (DAC122S085CIMM/NOPB) . I am glad to hear your feedback about solution of the problem. I have also attached the screenshot of the oscilloscope when I probe at the DAC output.
Code:
uint8_t DAC_A_Write = 0x1; // DAC's A output is selected 0 0 0 1
uint8_t DAC_B_Write = 0x5; // DAC's B output is selected 0 1 0 1
uint16_t DACA_Buf; // Output A of the DAC is designated
uint16_t DACB_Buf; // Output B of the DAC is designated
uint32_t Wave_LUT[] = {
2048, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355,
3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076,
4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730,
3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500,
2400, 2300, 2199, 2098, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 1305, 1212, 1120, 1031,
944, 860, 779, 701, 627, 556, 488, 424, 365, 309, 258, 211, 168, 130, 97,
69, 45, 26, 13, 4, 0, 1, 8, 19, 35, 56, 82, 113, 149, 189,
234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258,
1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047
};
uint32_t z;
HAL_TIM_Base_Start_IT(&htim6);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Check which version of the timer triggered this callback and toggle LED
if (htim == &htim6 )
{
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET);
DACA_Buf = DAC_A_Write<<12 | Wave_LUT[z]<<4;
HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,2,100);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET);
DACB_Buf = DAC_B_Write<<12 | Wave_LUT[z]<<4;
HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACB_Buf,2,100);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_SET);
z++;
if (z>=127)
{
z=0;
}
}
}