GY-63_MS5611/libraries/AD985X/AD985X.h

148 lines
3.5 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: AD985X.h
// AUTHOR: Rob Tillaart
2023-01-11 10:49:14 -05:00
// VERSION: 0.3.5
2021-01-29 06:31:58 -05:00
// DATE: 2019-02-08
// PURPOSE: Class for AD9850 and AD9851 function generator
// URL: https://github.com/RobTillaart/AD985X
2021-06-06 13:57:20 -04:00
2021-01-29 06:31:58 -05:00
#include "Arduino.h"
#include "SPI.h"
2023-01-11 10:49:14 -05:00
#define AD985X_LIB_VERSION (F("0.3.5"))
2021-01-29 06:31:58 -05:00
#define AD9850_MAX_FREQ (40UL * 1000UL * 1000UL)
#define AD9851_MAX_FREQ (70UL * 1000UL * 1000UL)
class AD9850
{
public:
AD9850();
2022-10-25 13:47:34 -04:00
// for HW SPI only use lower 3 parameters.
2021-06-06 13:57:20 -04:00
void begin(uint8_t select, uint8_t resetPin, uint8_t FQUDPin, uint8_t dataOut = 0, uint8_t clock = 0);
2021-01-29 06:31:58 -05:00
void reset();
void powerDown();
void powerUp();
2023-01-11 10:49:14 -05:00
// returns false if limited to AD9850_MAX_FREQ
bool setFrequency(uint32_t freq); // 0..AD9850_MAX_FREQ
bool setFrequencyF(float freq); // works best for low frequencies.
float getFrequency();
uint32_t getMaxFrequency();
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// 0 .. 31 steps of 11.25 degrees
2023-01-11 10:49:14 -05:00
// returns false if phase > 31.
bool setPhase(uint8_t phase = 0);
uint8_t getPhase();
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// offset to calibrate the frequency (internal counter)
// offset must be stored by the user.
2021-01-29 06:31:58 -05:00
void setCalibration(int32_t offset = 0) { _offset = offset; };
int32_t getCalibration() { return _offset; };
2023-01-11 10:49:14 -05:00
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// autoUpdate is default true;
2023-01-11 10:49:14 -05:00
void setAutoUpdate(bool update = true) { _autoUpdate = update; };
2021-01-29 06:31:58 -05:00
bool getAutoUpdate() { return _autoUpdate; };
void update();
2023-01-11 10:49:14 -05:00
2021-08-25 03:52:59 -04:00
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
2022-10-25 13:47:34 -04:00
// debugging
2021-08-25 03:52:59 -04:00
bool usesHWSPI() { return _hwSPI; };
2023-01-11 10:49:14 -05:00
// internal chip factor used for frequency. (debugging only)
uint32_t getFactor() { return _factor; };
2022-10-25 13:47:34 -04:00
// ESP32 specific
2021-08-25 03:52:59 -04:00
#if defined(ESP32)
void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };
2022-10-25 13:47:34 -04:00
// to overrule ESP32 default hardware pins
2021-08-25 03:52:59 -04:00
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
2021-01-29 06:31:58 -05:00
protected:
void pulsePin(uint8_t pin);
void writeData();
2021-08-25 03:52:59 -04:00
void swSPI_transfer(uint8_t val);
bool _hwSPI = true;
uint32_t _SPIspeed = 2000000;
SPIClass * mySPI;
SPISettings _spi_settings;
#if defined(ESP32)
bool _useHSPI = true;
#endif
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// PINS
2021-01-29 06:31:58 -05:00
uint8_t _dataOut = 0;
uint8_t _clock = 0;
uint8_t _select = 0;
2021-06-06 13:57:20 -04:00
uint8_t _reset = 0;
2022-10-25 13:47:34 -04:00
uint8_t _fqud = 0; // frequency update
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// SIGNAL
2021-01-29 06:31:58 -05:00
float _freq = 1;
uint32_t _factor = 0;
uint8_t _config = 0;
int32_t _offset = 0;
bool _autoUpdate = true;
};
2023-01-11 10:49:14 -05:00
/////////////////////////////////////////////////////////////////////
//
// DERIVED CLASS
//
2021-01-29 06:31:58 -05:00
class AD9851 : public AD9850
{
public:
2023-01-11 10:49:14 -05:00
// returns false if limited to AD9851_MAX_FREQ
bool setFrequency(uint32_t freq); // 0..AD9851_MAX_FREQ
bool setFrequencyF(float freq); // works best for low frequencies.
uint32_t getMaxFrequency();
void setRefClockHigh(); // 180 MHz
void setRefClockLow(); // 30 MHz
uint8_t getRefClock(); // returns 180 or 30
2021-01-29 06:31:58 -05:00
void setAutoRefClock(bool arc);
2023-01-11 10:49:14 -05:00
bool getAutoRefClock();
2021-01-29 06:31:58 -05:00
2022-10-25 13:47:34 -04:00
// 10 MHz is default, set in Hz.
// will be kept <= 30 MHz as that is the freq of LOW mode.
2021-01-29 06:31:58 -05:00
void setARCCutOffFreq(uint32_t Hz = 10000000UL );
2023-01-11 10:49:14 -05:00
uint32_t getARCCutOffFreq();
2021-01-29 06:31:58 -05:00
protected:
bool _autoRefClock = false;
uint32_t _ARCCutOffFreq = 10000000UL;
};
2022-10-25 13:47:34 -04:00
2021-01-29 06:31:58 -05:00
// -- END OF FILE --
2022-10-25 13:47:34 -04:00