-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
110 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef __MCP_ADC_H__ | ||
#define __MCP_ADC_H__ | ||
|
||
#include "AnalogSensorsInterface.h" | ||
|
||
// Definitions | ||
const int MCP_ADC_DEFAULT_SPI_SDI = 12; | ||
const int MCP_ADC_DEFAULT_SPI_SDO = 11; | ||
const int MCP_ADC_DEFAULT_SPI_CLK = 13; | ||
const int MCP_ADC_DEFAULT_SPI_SPEED = 2000000; | ||
|
||
template <int MCP_ADC_NUM_CHANNELS> | ||
class MCP_ADC : public AnalogMultiSensor<MCP_ADC_NUM_CHANNELS> | ||
{ | ||
private: | ||
const int spiPinCS_; | ||
const int spiPinSDI_; | ||
const int spiPinSDO_; | ||
const int spiPinCLK_; | ||
const int spiSpeed_; | ||
public: | ||
// Constructors | ||
MCP_ADC(int spiPinCS, const int spiPinSDI, const int spiPinSDO, const int spiPinCLK, const int spiSpeed); | ||
MCP_ADC(int spiPinCS); | ||
|
||
// Functions | ||
void tick(const SysTick_s &tick); | ||
void sample(); | ||
}; | ||
|
||
#include "MCP_ADC.tpp" | ||
|
||
#endif /* __MCP_ADC_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "MCP_ADC.h" | ||
#include <SPI.h> | ||
|
||
template <int MCP_ADC_NUM_CHANNELS> | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::MCP_ADC(const int spiPinCS, const int spiPinSDI, const int spiPinSDO, const int spiPinCLK, const int spiSpeed) | ||
: spiPinCS_(spiPinCS) | ||
, spiPinSDI_(spiPinSDI) | ||
, spiPinSDO_(spiPinSDO) | ||
, spiPinCLK_(spiPinCLK) | ||
, spiSpeed_(spiSpeed) | ||
{ | ||
for (int i = 0; i < MCP_ADC_NUM_CHANNELS; i++) | ||
{ | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::channels_[i] = AnalogChannel(); | ||
} | ||
|
||
pinMode(spiPinCS_, OUTPUT); | ||
pinMode(spiPinSDI_, INPUT); | ||
pinMode(spiPinSDO_, OUTPUT); | ||
pinMode(spiPinCLK_, OUTPUT); | ||
|
||
digitalWrite(spiPinCS_, HIGH); | ||
} | ||
|
||
template <int MCP_ADC_NUM_CHANNELS> | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::MCP_ADC(const int spiPinCS__) | ||
: MCP_ADC<MCP_ADC_NUM_CHANNELS>(spiPinCS__, MCP_ADC_DEFAULT_SPI_SDI, MCP_ADC_DEFAULT_SPI_SDO, MCP_ADC_DEFAULT_SPI_CLK, MCP_ADC_DEFAULT_SPI_SPEED) {} | ||
|
||
template <int MCP_ADC_NUM_CHANNELS> | ||
void MCP_ADC<MCP_ADC_NUM_CHANNELS>::tick(const SysTick_s &tick) | ||
{ | ||
// Sample at 100hz | ||
if (tick.triggers.trigger100) | ||
{ | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::sample(); | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::convert(); | ||
} | ||
} | ||
|
||
template <int MCP_ADC_NUM_CHANNELS> | ||
void MCP_ADC<MCP_ADC_NUM_CHANNELS>::sample() | ||
{ | ||
uint16_t command = ( | ||
(0b1 << 15) | // start bit | ||
(0b1 << 14) // single ended mode | ||
); | ||
|
||
SPI.beginTransaction(SPISettings(spiSpeed_, MSBFIRST, SPI_MODE0)); | ||
|
||
for (int channelIndex = 0; channelIndex < MCP_ADC_NUM_CHANNELS; channelIndex++) | ||
{ | ||
digitalWrite(spiPinCS_, LOW); | ||
uint16_t value = SPI.transfer16(command | channelIndex << 11); | ||
MCP_ADC<MCP_ADC_NUM_CHANNELS>::channels_[channelIndex].lastSample = (value & 0x0FFF); | ||
digitalWrite(spiPinCS_, HIGH); | ||
delayMicroseconds(1); // MCP_ADC Tcsh = 500ns | ||
} | ||
|
||
SPI.endTransaction(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters