111 lines
5.0 KiB
C
111 lines
5.0 KiB
C
|
|
|
|
#include "dds.h"
|
|
|
|
const uint16_t SINEWAVE_LUT[SINEWAVE_LUT_SIZE] = {
|
|
2048, 2073, 2098, 2123, 2148, 2173, 2198, 2223, 2248, 2273, 2298, 2323, 2348, 2373, 2398, 2422,
|
|
2447, 2472, 2496, 2521, 2545, 2569, 2594, 2618, 2642, 2666, 2690, 2714, 2737, 2761, 2784, 2808,
|
|
2831, 2854, 2877, 2900, 2923, 2946, 2968, 2990, 3013, 3035, 3057, 3078, 3100, 3122, 3143, 3164,
|
|
3185, 3206, 3226, 3247, 3267, 3287, 3307, 3327, 3346, 3366, 3385, 3404, 3423, 3441, 3459, 3477,
|
|
3495, 3513, 3530, 3548, 3565, 3581, 3598, 3614, 3630, 3646, 3662, 3677, 3692, 3707, 3722, 3736,
|
|
3750, 3764, 3777, 3791, 3804, 3816, 3829, 3841, 3853, 3865, 3876, 3888, 3898, 3909, 3919, 3929,
|
|
3939, 3949, 3958, 3967, 3975, 3984, 3992, 3999, 4007, 4014, 4021, 4027, 4034, 4040, 4045, 4051,
|
|
4056, 4060, 4065, 4069, 4073, 4076, 4080, 4083, 4085, 4087, 4089, 4091, 4093, 4094, 4094, 4095,
|
|
4095, 4095, 4094, 4094, 4093, 4091, 4089, 4087, 4085, 4083, 4080, 4076, 4073, 4069, 4065, 4060,
|
|
4056, 4051, 4045, 4040, 4034, 4027, 4021, 4014, 4007, 3999, 3992, 3984, 3975, 3967, 3958, 3949,
|
|
3939, 3929, 3919, 3909, 3898, 3888, 3876, 3865, 3853, 3841, 3829, 3816, 3804, 3791, 3777, 3764,
|
|
3750, 3736, 3722, 3707, 3692, 3677, 3662, 3646, 3630, 3614, 3598, 3581, 3565, 3548, 3530, 3513,
|
|
3495, 3477, 3459, 3441, 3423, 3404, 3385, 3366, 3346, 3327, 3307, 3287, 3267, 3247, 3226, 3206,
|
|
3185, 3164, 3143, 3122, 3100, 3078, 3057, 3035, 3013, 2990, 2968, 2946, 2923, 2900, 2877, 2854,
|
|
2831, 2808, 2784, 2761, 2737, 2714, 2690, 2666, 2642, 2618, 2594, 2569, 2545, 2521, 2496, 2472,
|
|
2447, 2422, 2398, 2373, 2348, 2323, 2298, 2273, 2248, 2223, 2198, 2173, 2148, 2123, 2098, 2073,
|
|
2048, 2022, 1997, 1972, 1947, 1922, 1897, 1872, 1847, 1822, 1797, 1772, 1747, 1722, 1697, 1673,
|
|
1648, 1623, 1599, 1574, 1550, 1526, 1501, 1477, 1453, 1429, 1405, 1381, 1358, 1334, 1311, 1287,
|
|
1264, 1241, 1218, 1195, 1172, 1149, 1127, 1105, 1082, 1060, 1038, 1017, 995, 973, 952, 931,
|
|
910, 889, 869, 848, 828, 808, 788, 768, 749, 729, 710, 691, 672, 654, 636, 618,
|
|
600, 582, 565, 547, 530, 514, 497, 481, 465, 449, 433, 418, 403, 388, 373, 359,
|
|
345, 331, 318, 304, 291, 279, 266, 254, 242, 230, 219, 207, 197, 186, 176, 166,
|
|
156, 146, 137, 128, 120, 111, 103, 96, 88, 81, 74, 68, 61, 55, 50, 44,
|
|
39, 35, 30, 26, 22, 19, 15, 12, 10, 8, 6, 4, 2, 1, 1, 0,
|
|
0, 0, 1, 1, 2, 4, 6, 8, 10, 12, 15, 19, 22, 26, 30, 35,
|
|
39, 44, 50, 55, 61, 68, 74, 81, 88, 96, 103, 111, 120, 128, 137, 146,
|
|
156, 166, 176, 186, 197, 207, 219, 230, 242, 254, 266, 279, 291, 304, 318, 331,
|
|
345, 359, 373, 388, 403, 418, 433, 449, 465, 481, 497, 514, 530, 547, 565, 582,
|
|
600, 618, 636, 654, 672, 691, 710, 729, 749, 768, 788, 808, 828, 848, 869, 889,
|
|
910, 931, 952, 973, 995, 1017, 1038, 1060, 1082, 1105, 1127, 1149, 1172, 1195, 1218, 1241,
|
|
1264, 1287, 1311, 1334, 1358, 1381, 1405, 1429, 1453, 1477, 1501, 1526, 1550, 1574, 1599, 1623,
|
|
1648, 1673, 1697, 1722, 1747, 1772, 1797, 1822, 1847, 1872, 1897, 1922, 1947, 1972, 1997, 2022
|
|
|
|
};
|
|
|
|
uint8_t numBits(uint32_t num)
|
|
{
|
|
uint8_t width = 32; // maximum bit width of number being tested
|
|
|
|
while (width)
|
|
{
|
|
width--;
|
|
if (num & (1 << width))
|
|
break; // look for bit position of first "1" from MSB toward LSB
|
|
}
|
|
return (width + 1);
|
|
}
|
|
|
|
uint8_t tableWidth;
|
|
uint32_t fOut;
|
|
uint32_t tuningWord;
|
|
uint32_t phInc; // dds phase increment
|
|
uint32_t phAcc; // dds phase accumulator
|
|
uint16_t dac_buffor[DDS_DAC_BUF_SIZE];
|
|
|
|
void DDS_Init(void)
|
|
{
|
|
tableWidth = numBits(SINEWAVE_LUT_SIZE - 1);
|
|
|
|
fOut = 1000; // set output frequency in Hz
|
|
tuningWord = UINT32_MAX * fOut / DDS_TIM_FREQ; // DDS tuning word for target frequency
|
|
|
|
// __HAL_TIM_ENABLE_DMA()
|
|
}
|
|
|
|
/**
|
|
* @brief Conversion complete callback in non blocking mode for Channel1
|
|
* @param hdac pointer to a DAC_HandleTypeDef structure that contains
|
|
* the configuration information for the specified DAC.
|
|
* @retval None
|
|
*/
|
|
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
|
|
{
|
|
for (uint8_t i = DDS_DAC_BUF_SIZE / 2; i < DDS_DAC_BUF_SIZE; i++)
|
|
{
|
|
phAcc += tuningWord;
|
|
phInc = phAcc >> (32 - tableWidth);
|
|
|
|
dac_buffor[i] = SINEWAVE_LUT[phInc];
|
|
if (phAcc > UINT32_MAX / 2)
|
|
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
|
|
else
|
|
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Conversion half DMA transfer callback in non blocking mode for Channel1
|
|
* @param hdac pointer to a DAC_HandleTypeDef structure that contains
|
|
* the configuration information for the specified DAC.
|
|
* @retval None
|
|
*/
|
|
void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
|
|
{
|
|
for (uint8_t i = 0; i < DDS_DAC_BUF_SIZE / 2; i++)
|
|
{
|
|
phAcc += tuningWord;
|
|
phInc = phAcc >> (32 - tableWidth);
|
|
|
|
dac_buffor[i] = SINEWAVE_LUT[phInc];
|
|
if (phAcc > UINT32_MAX / 2)
|
|
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
|
|
else
|
|
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
|
}
|
|
} |