Added genral support for DAC MCP4725

This commit is contained in:
Kai Lauterbach 2024-02-14 16:42:47 +01:00
parent 69ac7c3f8a
commit dbd69c7284
4 changed files with 45 additions and 21 deletions

View file

@ -241,14 +241,8 @@ void cc_startMeasurement()
// TODO an diesem punkt muss unterschieden werden ob eine waveform ausgegeben werden soll oder eine frequenz, und welche Art von Kurve
if (freq < WF_FREQ_MAX_HZ)
{
if (freq > PWM_MAX_VALUE)
{
setWaveform(WAVEFORM_SINUS);
setWaveformFrequency(freq);
} else {
setWaveform(WAVEFORM_DUTYCYCLE);
setWaveformDC((uint8_t)(freq & 0xff));
}
setWaveformFrequency(freq);
enableWaveformOutput();
pollWaveformGenerator(); // manually poll the waveformgenerator
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
@ -315,7 +309,7 @@ void cc_getConfig()
send32BitValue(end_freq);
send32BitValue(step_freq);
send16BitValue(intervall);
Serial.write((uint8_t) drive_str);
Serial.write((uint8_t)drive_str);
sendEOM();
}
@ -328,14 +322,8 @@ void cc_enableClk(void)
{
if (start_freq < WF_FREQ_MAX_HZ) // < 8kHz
{
if (start_freq > PWM_MAX_VALUE) // > 8 bit pwm value
{
setWaveform(WAVEFORM_SINUS);
setWaveformFrequency(start_freq);
} else {
setWaveform(WAVEFORM_DUTYCYCLE);
setWaveformDC((uint8_t)(start_freq & 0xff));
}
setWaveformFrequency(start_freq);
enableWaveformOutput();
si5351.output_enable(SI5351_CLK0, 0);
} else {

View file

@ -6,7 +6,9 @@
/*****************************************************************************/
#include <Adafruit_MCP4725.h>
#include <si5351.h>
#include "Wire.h"
#include <EEPROM.h>
@ -19,11 +21,13 @@ extern "C" {
/*****************************************************************************/
Si5351 si5351;
Adafruit_MCP4725 dac;
uint32_t start_freq = 10000;
uint32_t end_freq = 50000000;
uint32_t step_freq = 1000000; // 1 MHz default step size
uint16_t intervall = 1000; // intervall to change the frequency as milli seconds
enum si5351_drive drive_str = SI5351_DRIVE_2MA;
/*****************************************************************************/
@ -49,6 +53,17 @@ void setup()
// initialize the command control module
cc_init();
if (dac.begin(0x62))
{
bool tmp = dac.setVoltage(500, false, 100000);
if (!tmp)
{
blinkCode(150);
}
} else {
blinkCode(500);
}
// init the Si5351
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
@ -192,3 +207,18 @@ uint32_t keepFreqRange(uint32_t freq)
}
/*****************************************************************************/
void blinkCode(uint16_t d)
{
pinMode(13, OUTPUT);
while (true)
{
digitalWrite(13, HIGH);
delay(d);
digitalWrite(13, LOW);
delay(d);
Serial.write(MSG_TYPE_ANSWER_NOK);
}
}
/*****************************************************************************/

View file

@ -5,8 +5,13 @@
/*****************************************************************************/
#define PWM_BIT_WIDTH 8
#define DAC_I2C_ADDRESS 0x62
#define DAC_MAX_VOLTAGE 0xfff
/*****************************************************************************/
#define PWM_PIN 5 // PWM-Pin für DAC0 auf dem Arduino Nano 328
#define PWM_BIT_WIDTH 12
#define PWM_MAX_VALUE ((1 << PWM_BIT_WIDTH)-1)
/*****************************************************************************/

View file

@ -82,10 +82,10 @@ void pollWaveformGenerator()
{
wf_prevMicros = currentMicros;
uint16_t sample = map(waveformsTable[wf_wave0][wf_pos], 0, 0xfff, 0, PWM_MAX_VALUE);
sample = constrain(sample, 0, PWM_MAX_VALUE);
uint16_t sample = map(waveformsTable[wf_wave0][wf_pos], 0, 0xfff, 0, DAC_MAX_VOLTAGE);
sample = constrain(sample, 0, DAC_MAX_VOLTAGE);
// TODO write the selected waveform on DAC0
analogWrite(PWM_PIN, sample);
dac.setVoltage(sample, false, 100000); // do not write to EEPROM, 400000Hz DAC frequency
wf_pos++;
if (wf_pos == WAVEFORM_MAX_SAMPLES_NUM) // Reset the counter to repeat the wave
@ -104,6 +104,7 @@ void pollWaveformGenerator()
} else {
if (wf_pwm_needs_disabling)
{
dac.setVoltage(0, false, 400000);
analogWrite(PWM_PIN, 0);
wf_pwm_needs_disabling = false;
}