[fix] modify calc freq/phase and correct bugs
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
#include "ad9833.h"
|
||||
|
||||
// Convenience calculations
|
||||
static uint32_t ad9833_calcFreq(float f); // Calculate AD9833 frequency register from a frequency
|
||||
static uint16_t ad9833_calcPhase(float a); // Calculate AD9833 phase register from phase
|
||||
// static uint32_t ad9833_calcFreq(float f); // Calculate AD9833 frequency register from a frequency
|
||||
// static uint16_t ad9833_calcPhase(float a); // Calculate AD9833 phase register from phase
|
||||
|
||||
static void ad9833_transmit16(ad9833_handle_t *hfg, uint16_t data)
|
||||
{
|
||||
@@ -51,11 +51,10 @@ void ad9833_init(ad9833_handle_t *hfg, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs
|
||||
ad9833_setFrequency(hfg, CHAN_1, AD_DEFAULT_FREQ);
|
||||
ad9833_setPhase(hfg, CHAN_0, AD_DEFAULT_PHASE);
|
||||
ad9833_setPhase(hfg, CHAN_1, AD_DEFAULT_PHASE);
|
||||
ad9833_reset(hfg, 0); // full transition
|
||||
|
||||
ad9833_setMode(hfg, MODE_OFF);
|
||||
ad9833_setActiveChannelFreq(hfg, CHAN_0);
|
||||
ad9833_setActiveChannelPhase(hfg, CHAN_0);
|
||||
ad9833_setActiveChannelFreq(hfg, CHAN_0);
|
||||
ad9833_setMode(hfg, MODE_OFF);
|
||||
ad9833_reset(hfg, 0); // full transition
|
||||
}
|
||||
|
||||
void ad9833_setActiveChannelFreq(ad9833_handle_t *hfg, AD_channel_t chan)
|
||||
@@ -146,26 +145,35 @@ void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode)
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
}
|
||||
|
||||
static uint32_t ad9833_calcFreq(float f)
|
||||
// Calculate register value for AD9833 frequency register from a frequency
|
||||
// static uint32_t ad9833_calcFreq(float f)
|
||||
// // Calculate register value for AD9833 frequency register from a frequency
|
||||
// {
|
||||
// return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5f);
|
||||
// }
|
||||
|
||||
static uint32_t ad9833_calcFreq_uint(uint32_t f)
|
||||
{
|
||||
return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5f);
|
||||
return ((f * AD_2POW28 + AD_MCLK_DIV2) / AD_MCLK); // ((n + d/2)/d)
|
||||
}
|
||||
|
||||
static uint16_t ad9833_calcPhase(float a)
|
||||
// Calculate the value for AD9833 phase register from given phase in tenths of a degree
|
||||
{
|
||||
return (uint16_t)((512.0f * (a / 10) / 45) + 0.5f);
|
||||
}
|
||||
// static uint16_t ad9833_calcPhase(float a)
|
||||
// // Calculate the value for AD9833 phase register from given phase in tenths of a degree
|
||||
// {
|
||||
// return (uint16_t)((512.0f * (a / 10) / 45) + 0.5f);
|
||||
// }
|
||||
|
||||
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq)
|
||||
static uint16_t ad9833_calcPhase_uint(uint16_t p)
|
||||
{
|
||||
return ((p * 4096U + 180) / 360);
|
||||
}
|
||||
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, uint32_t freq)
|
||||
{
|
||||
// PRINT("\nsetFreq CHAN_", chan);
|
||||
uint16_t freq_channel = 0;
|
||||
|
||||
hfg->_freq[chan] = freq;
|
||||
|
||||
hfg->_regFreq[chan] = ad9833_calcFreq(freq);
|
||||
hfg->_regFreq[chan] = ad9833_calcFreq_uint(freq);
|
||||
|
||||
// select the address mask
|
||||
|
||||
@@ -198,7 +206,7 @@ void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase)
|
||||
uint16_t phase_channel = 0;
|
||||
|
||||
hfg->_phase[chan] = phase;
|
||||
hfg->_regPhase[chan] = ad9833_calcPhase(phase);
|
||||
hfg->_regPhase[chan] = ad9833_calcPhase_uint(phase);
|
||||
|
||||
// select the address mask
|
||||
switch (chan)
|
||||
|
||||
Reference in New Issue
Block a user