2023-08-27 14:28:30 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: AD9833.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: Arduino library for AD9833 function generator.
|
2024-05-25 04:20:42 -04:00
|
|
|
// VERSION: 0.4.1
|
2023-08-27 14:28:30 -04:00
|
|
|
// URL: https://github.com/RobTillaart/AD9833
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "SPI.h"
|
|
|
|
|
|
|
|
|
2024-05-25 04:20:42 -04:00
|
|
|
#define AD9833_LIB_VERSION (F("0.4.1"))
|
2023-11-27 10:16:26 -05:00
|
|
|
|
|
|
|
|
2024-05-25 04:20:42 -04:00
|
|
|
#ifndef __SPI_CLASS__
|
|
|
|
// MBED must be tested before RP2040
|
|
|
|
#if defined(ARDUINO_ARCH_MBED)
|
|
|
|
#define __SPI_CLASS__ SPIClass
|
|
|
|
#elif defined(ARDUINO_ARCH_RP2040)
|
2023-12-18 05:23:18 -05:00
|
|
|
#define __SPI_CLASS__ SPIClassRP2040
|
|
|
|
#else
|
|
|
|
#define __SPI_CLASS__ SPIClass
|
|
|
|
#endif
|
2023-11-27 10:16:26 -05:00
|
|
|
#endif
|
2023-08-27 14:28:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
#define AD9833_MAX_FREQ (12500000UL) // 12.5 MHz.
|
|
|
|
#define AD9833_MAX_PHASE (360.0)
|
|
|
|
|
|
|
|
|
|
|
|
// MODE OPERANDI
|
|
|
|
#define AD9833_OFF 0
|
|
|
|
#define AD9833_SINE 1
|
|
|
|
#define AD9833_SQUARE1 2
|
|
|
|
#define AD9833_SQUARE2 3
|
|
|
|
#define AD9833_TRIANGLE 4
|
|
|
|
|
|
|
|
|
|
|
|
class AD9833
|
|
|
|
{
|
|
|
|
public:
|
2023-11-27 10:16:26 -05:00
|
|
|
// slaveSelect == selectPin == fsyncPin
|
|
|
|
// HARDWARE SPI
|
|
|
|
AD9833(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
|
|
|
|
// SOFTWARE SPI
|
|
|
|
AD9833(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);
|
2023-08-27 14:28:30 -04:00
|
|
|
|
2023-11-27 10:16:26 -05:00
|
|
|
void begin();
|
2023-08-27 14:28:30 -04:00
|
|
|
|
|
|
|
void reset();
|
2023-09-03 04:16:41 -04:00
|
|
|
void hardwareReset();
|
|
|
|
// mode = 0..3 (datasheet)
|
|
|
|
bool setPowerMode(uint8_t mode = 0);
|
2023-08-27 14:28:30 -04:00
|
|
|
|
2023-09-03 04:16:41 -04:00
|
|
|
|
|
|
|
void setWave(uint8_t waveform = AD9833_OFF);
|
2023-08-27 14:28:30 -04:00
|
|
|
uint8_t getWave();
|
|
|
|
|
|
|
|
|
|
|
|
// returns frequency set
|
|
|
|
// 0 .. 12.5 MHz == AD9833_MAX_FREQ
|
2024-01-12 06:40:21 -05:00
|
|
|
float setFrequency(float frequency, uint8_t channel = 0);
|
2023-08-27 14:28:30 -04:00
|
|
|
float getFrequency(uint8_t channel = 0);
|
|
|
|
float getMaxFrequency();
|
|
|
|
void setFrequencyChannel(uint8_t channel);
|
|
|
|
|
2023-10-16 09:53:57 -04:00
|
|
|
// returns phase set (degrees)
|
2023-08-27 14:28:30 -04:00
|
|
|
// [0 .. 360>
|
|
|
|
float setPhase(float phase, uint8_t channel = 0);
|
|
|
|
float getPhase(uint8_t channel = 0);
|
|
|
|
float getMaxPhase();
|
|
|
|
void setPhaseChannel(uint8_t channel);
|
|
|
|
|
|
|
|
|
|
|
|
// Hardware SPI settings
|
|
|
|
void setSPIspeed(uint32_t speed); // speed in Hz
|
|
|
|
uint32_t getSPIspeed();
|
|
|
|
bool usesHWSPI();
|
|
|
|
|
|
|
|
|
2023-09-03 04:16:41 -04:00
|
|
|
// LOW LEVEL API - Expert users only
|
2023-08-27 14:28:30 -04:00
|
|
|
void writeControlRegister(uint16_t value);
|
2023-12-18 05:23:18 -05:00
|
|
|
void writeFrequencyRegister(uint8_t channel, uint32_t freq);
|
|
|
|
void writePhaseRegister(uint8_t channel, uint16_t value);
|
|
|
|
|
|
|
|
|
2024-01-12 06:40:21 -05:00
|
|
|
// EXPERIMENTAL adjust changed crystal
|
|
|
|
void setCrystalFrequency(float crystalFrequency = 25000000);
|
|
|
|
float getCrystalFrequency();
|
|
|
|
|
|
|
|
|
2023-12-18 05:23:18 -05:00
|
|
|
// EXPERIMENTAL HLB MODE (14 bit)
|
|
|
|
void writeFrequencyRegisterLSB(uint8_t channel, uint16_t LSB);
|
|
|
|
void writeFrequencyRegisterMSB(uint8_t channel, uint16_t MSB);
|
2023-09-03 04:16:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2023-08-27 14:28:30 -04:00
|
|
|
void writeData(uint16_t data);
|
2023-12-18 05:23:18 -05:00
|
|
|
void writeData28(uint16_t LSB, uint16_t MSB);
|
2023-08-27 14:28:30 -04:00
|
|
|
|
|
|
|
bool _hwSPI = true;
|
|
|
|
uint32_t _SPIspeed = 8000000;
|
|
|
|
|
2023-11-27 10:16:26 -05:00
|
|
|
__SPI_CLASS__ * _mySPI;
|
|
|
|
SPISettings _spi_settings;
|
2023-08-27 14:28:30 -04:00
|
|
|
|
|
|
|
// PINS
|
|
|
|
uint8_t _dataPin = 0;
|
|
|
|
uint8_t _clockPin = 0;
|
|
|
|
uint8_t _selectPin = 0;
|
2023-09-03 04:16:41 -04:00
|
|
|
bool _useSelect = false;
|
2023-08-27 14:28:30 -04:00
|
|
|
|
|
|
|
// SIGNAL
|
|
|
|
uint16_t _control = 0;
|
2023-09-03 04:16:41 -04:00
|
|
|
uint8_t _waveform = AD9833_OFF;
|
2023-08-27 14:28:30 -04:00
|
|
|
|
2023-09-03 04:16:41 -04:00
|
|
|
float _freq[2] = { 0, 0 }; // Hz
|
|
|
|
float _phase[2] = { 0, 0 }; // angle 0..360
|
2024-01-12 06:40:21 -05:00
|
|
|
|
|
|
|
float _crystalFreqFactor = 268435456.0 / 25000000.0;
|
2023-08-27 14:28:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|