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;
}
}
}
uint16_t DACA_Buf; // Output A of the DAC is designated
uint16_t DACB_Buf; // Output B of the DAC is designated
uint16_t DAC_A_Write = 0x1000;
uint16_t DAC_B_Write = 0x5000;
uint16_t DAC_val;
uint16_t I;
void DAC_A_Conversion()
{
for (I = 0 ; I < 128 ; I++)
{
DAC_val = 2048 + round ( 2047 * sin( 360 * I / 128 ));
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET);
DACA_Buf = DAC_A_Write<<12 | DAC_val;
HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,2,100);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_SET);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Check which version of the timer triggered this callback and toggle LED
if (htim == &htim6 )
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
DAC_A_Conversion();
}
}
#define NS 128
static uint16_t z;
const uint16_t function[NS] = { 2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897,
2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640,
3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002,
4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894,
3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344,
3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483,
2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517,
1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679,
610, 545, 484, 426, 373, 323, 278, 237, 201, 169,
141, 118, 100, 87, 79, 75, 76, 82, 93, 109,
129, 154, 184, 218, 257, 300, 347, 399, 455, 514,
577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287,
1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 };
void DAC_A_Conversion()
{
for (z = 0 ; z < 128 ; z++)
{
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET);
DACA_Buf = DAC_A_Write<<12 | Wave_LUT[z];
HAL_SPI_Transmit(&hspi2,(uint8_t*)&DACA_Buf,2,100);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_SET);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Check which version of the timer triggered this callback and toggle LED
if (htim == &htim6 )
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
DAC_A_Conversion();
}
}
Lookup table. But I guess you calculate them using Excel or anything else. Thus I gave the formula.should I create the sine wave with the lookup table or sine wave?
clear; clc; % Clear The Previous Points
Ns = 128; % Set The Number of Sample Points
RES = 12; % Set The DAC Resolution
OFFSET = 0; % Set An Offset Value For The DAC Output
%------------[ Calculate The Sample Points ]-------------
T = 0:((2*pi/(Ns-1))):(2*pi);
Y = sin(T);
Y = Y + 1;
Y = Y*((2^RES-1)-2*OFFSET)/(2+OFFSET);
Y = round(Y);
plot(T, Y);
grid
(wrong: T = 0:((2*pi/(Ns-1))):(2*pi);)
correct: T = 0:((2*pi/(Ns))):(2*pi);
#define NS 128
const uint16_t Wave_LUT[NS] = { 2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897,
2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640,
3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002,
4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894,
3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344,
3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483,
2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517,
1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679,
610, 545, 484, 426, 373, 323, 278, 237, 201, 169,
141, 118, 100, 87, 79, 75, 76, 82, 93, 109,
129, 154, 184, 218, 257, 300, 347, 399, 455, 514,
577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287,
1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 };
static uint16_t z;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// Check which version of the timer triggered this callback and toggle LED
if (htim == &htim6 )
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET);
DACA_Buf = DAC_A_Write<<12 | Wave_LUT[z];
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];
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;
}
}
T = 0:((2*pi/(Ns))):(2*pi);
static void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 96-1;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 100-1;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
uint16_t DAC_A_Write = 0x1000;
uint16_t DAC_B_Write = 0x5000;
uint16_t DACA_Buf; // Output A of the DAC is designated
uint16_t DACB_Buf; // Output B of the DAC is designated
static uint16_t z;
Thank you @KlausST and @Aussie Susan!
Based on your formula, I got the same lookup table as in the post#1. So far, what I have
Code:#define NS 128 const uint16_t Wave_LUT[NS] = { 2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897, 2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640, 3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002, 4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894, 3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344, 3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483, 2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517, 1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679, 610, 545, 484, 426, 373, 323, 278, 237, 201, 169, 141, 118, 100, 87, 79, 75, 76, 82, 93, 109, 129, 154, 184, 218, 257, 300, 347, 399, 455, 514, 577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287, 1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 }; static uint16_t z; void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { // Check which version of the timer triggered this callback and toggle LED if (htim == &htim6 ) { HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, GPIO_PIN_RESET); DACA_Buf = DAC_A_Write<<12 | Wave_LUT[z]; 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]; 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; } }
I also initialized the timer. Then, I got 4.4V from DACA output and 0V from DACB output. In the watch window, the DACA_Buf and DACB_Buf are updating.
--- Updated ---
Thank you @danadakk for your report! How can I read your results? Can you explain them?
It should be like this,
@KlausSTCode:uint16_t DAC_A_Write = 0x0001; uint16_t DAC_B_Write = 0x0101;
I am sorry I understand and fixed them. Now I get the sine wave. Its frequency is 80hZ. How is it related to the timer interrupt frequency?
No.uint16_t DAC_A_Write = 0x0001; uint16_t DAC_B_Write = 0x0101;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?