Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:2b1457f78377, committed 2021-02-25
- Comitter:
- kanope_benny
- Date:
- Thu Feb 25 13:50:05 2021 +0000
- Parent:
- 1:f3de66b30fc3
- Commit message:
- dddd
Changed in this revision
MCP4822.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP4822.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MCP4822.cpp Thu Feb 25 12:18:14 2021 +0000 +++ b/MCP4822.cpp Thu Feb 25 13:50:05 2021 +0000 @@ -1,3 +1,247 @@ +/* MCP4922 Driver Library + * Copyright (c) 2014 Neil Thiessen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MCP4822.h" + +#define SPI_16_BITS 0 +#define SPI_8_BITS 1 + + +MCP4922::MCP4922(PinName mosi, PinName miso, PinName sclk, PinName cs, int hz) : m_SPI(mosi, miso, sclk), m_CS(cs, 1) +{ + //Initialize the member variables + m_DacValueA = 0; + m_DacValueB = 0; + + //Set the SPI format and bus frequency + #if SPI_16_BITS + m_SPI.format(16, 0); + #endif + + #if SPI_8_BITS + m_SPI.format(8, 0); + #endif + + m_SPI.frequency(hz); + + //Perform an initial write to both DACs so the variables are in sync + writeDac(m_DacValueA | (DAC_A << 15)); + writeDac(m_DacValueB | (DAC_B << 15)); +} + +MCP4922::ReferenceMode MCP4922::referenceMode(DAC dac) +{ + //Return the current reference mode for the specified DAC + if (dac == DAC_A) + return (ReferenceMode)((m_DacValueA >> 14) & 0x01); + else + return (ReferenceMode)((m_DacValueB >> 14) & 0x01); +} + +void MCP4922::referenceMode(DAC dac, ReferenceMode mode) +{ + //Update the reference mode for the specified DAC + if (dac == DAC_A) { + //Mask off the old mode, and set the new one + m_DacValueA &= ~(1 << 14); + m_DacValueA |= (mode << 14); + + //Update the DAC A + writeDac(m_DacValueA | (dac << 15)); + } else { + //Mask off the old mode, and set the new one + m_DacValueB &= ~(1 << 14); + m_DacValueB |= (mode << 14); + + //Update the DAC B + writeDac(m_DacValueB | (dac << 15)); + } +} + +MCP4922::GainMode MCP4922::gainMode(DAC dac) +{ + //Return the current gain mode for the specified DAC + if (dac == DAC_A) + return (GainMode)((m_DacValueA >> 13) & 0x01); + else + return (GainMode)((m_DacValueB >> 13) & 0x01); +} + +void MCP4922::gainMode(DAC dac, GainMode mode) +{ + //Update the gain mode for the specified DAC + if (dac == DAC_A) { + //Mask off the old mode, and set the new one + m_DacValueA &= ~(1 << 13); + m_DacValueA |= (mode << 13); + + //Update the DAC A + writeDac(m_DacValueA | (dac << 15)); + } else { + //Mask off the old mode, and set the new one + m_DacValueB &= ~(1 << 13); + m_DacValueB |= (mode << 13); + + //Update the DAC B + writeDac(m_DacValueB | (dac << 15)); + } +} + +MCP4922::PowerMode MCP4922::powerMode(DAC dac) +{ + //Return the current power mode for the specified DAC + if (dac == DAC_A) + return (PowerMode)((m_DacValueA >> 12) & 0x01); + else + return (PowerMode)((m_DacValueB >> 12) & 0x01); +} + +void MCP4922::powerMode(DAC dac, PowerMode mode) +{ + //Update the power mode for the specified DAC + if (dac == DAC_A) { + //Mask off the old mode, and set the new one + m_DacValueA &= ~(1 << 12); + m_DacValueA |= (mode << 12); + + //Update the DAC A + writeDac(m_DacValueA | (dac << 15)); + } else { + //Mask off the old mode, and set the new one + m_DacValueB &= ~(1 << 12); + m_DacValueB |= (mode << 12); + + //Update the DAC B + writeDac(m_DacValueB | (dac << 15)); + } +} + +float MCP4922::read(DAC dac) +{ + //Return the current value for the specified DAC as a float + if (dac == DAC_A) + return (m_DacValueA & 0x0FFF) / 4095.0; + else + return (m_DacValueB & 0x0FFF) / 4095.0; +} +uint16_t MCP4922::read_u16(DAC dac){ + + //Return the current value for the specified DAC as a float + if (dac == DAC_A) + return m_DacValueA; + else + return m_DacValueB; +} + +void MCP4922::write(DAC dac, float value) +{ + //Range limit value + if (value < 0.00f) + value = 0.00f; + else if (value > 1.00f) + value = 1.00f; + + //Convert value to an unsigned short, and pass it to write_u16() + write_u16(dac, (unsigned short)(value * 4095) << 4); +} + +void MCP4922::write_u16(DAC dac, uint16_t value) +{ + //Update the value for the specified DAC + if (dac == DAC_A) { + //Mask off the old value, and set the new one + m_DacValueA &= 0xF000; + m_DacValueA |= value >> 4; + + //Update the DAC A + writeDac(m_DacValueA | (dac << 15)); + } else { + //Mask off the old value, and set the new one + m_DacValueB &= 0xF000; + m_DacValueB |= value >> 4; + + //Update the DAC B + writeDac(m_DacValueB | (dac << 15)); + } +} + +void MCP4922::write_bits(DAC dac, uint16_t value) +{ + if (dac == DAC_A) { + write_DAC_A(value); + } + else{ + write_DAC_B(value); + } +} + +// DAC A : +// avec GAIN x2 etc pour le CV +void MCP4922::write_DAC_A(uint16_t value){ + // on clip : + if( value > 0x0FFF ){ + value = 0x0FFF; + } + // DAC A : + // si unbuffered : 0001 0x1000 + // si buffered : 0101 0x5000 + writeDac( 0x1000 | value ); // unbuffered + //writeDac( 0x5000 | value ); // buffered + +} + + // DAC B : +void MCP4922::write_DAC_B(uint16_t value){ + // on clip : + if( value > 0x0FFF ){ + value = 0x0FFF; + } + + // DAC B : + // si unbuffered : 1001 0x9000 + // si buffered : 1101 0xD000 + writeDac( 0x9000 | value ); // unbuffered + //writeDac( 0xD000 | value ); // buffered + +} + +void MCP4922::writeDac(unsigned short value) +{ + //Pull CS low + m_CS = 0; + + //Perform the 16-bit write + #if SPI_16_BITS + m_SPI.write(value); + #endif + + #if SPI_8_BITS + m_SPI.write( value >> 8 ); + m_SPI.write( value & 0xFF); + #endif + + //Pull CS high + m_CS = 1; +} + + + + + +#if 0 /* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) * @@ -116,4 +360,6 @@ _spi.write(_upper_half); _spi.write(_lower_half); _nCs = 1; - } \ No newline at end of file + } + +#endif \ No newline at end of file
--- a/MCP4822.h Thu Feb 25 12:18:14 2021 +0000 +++ b/MCP4822.h Thu Feb 25 13:50:05 2021 +0000 @@ -1,3 +1,190 @@ +/* MCP4922 Driver Library + * Copyright (c) 2014 Neil Thiessen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MCP4922_H +#define MCP4922_H + +#include "mbed.h" + +/** MCP4922 class. + * Used for controlling an MCP4922 DAC connected via SPI. + * + * Example: + * @code + * #include "mbed.h" + * #include "MCP4922.h" + * + * //Create an MCP4922 object + * MCP4922 dac(p11, p13, p14); + * + * int main() + * { + * //Configure DAC A + * dac.referenceMode(MCP4922::DAC_A, MCP4922::REF_UNBUFFERED); + * dac.gainMode(MCP4922::DAC_A, MCP4922::GAIN_1X); + * dac.powerMode(MCP4922::DAC_A, MCP4922::POWER_NORMAL); + * + * while (1) { + * //Generate a sine wave on DAC A + * for (float i = 0.0; i < 360.0; i += 0.1) + * dac.write(MCP4922::DAC_A, 0.5 * (sinf(i * 3.14159265 / 180.0) + 1)); + * } + * } + * @endcode + */ +class MCP4922 +{ +public: + /** Represents the different DACs in the MCP4922 + */ + enum DAC { + DAC_A, /**< DAC A */ + DAC_B /**< DAC B */ + }; + + /** Represents the reference buffer mode of DAC A or B in the MCP4922 + */ + enum ReferenceMode { + REF_UNBUFFERED, /**< DAC VREF is unbuffered */ + REF_BUFFERED /**< DAC VREF is buffered */ + }; + + /** Represents the gain mode of DAC A or B in the MCP4922 + */ + enum GainMode { + GAIN_2X, /**< DAC output = 2 * VREF * D/4096 */ + GAIN_1X /**< DAC output = VREF * D/4096 */ + }; + + /** Represents the power mode of DAC A or B in the MCP4922 + */ + enum PowerMode { + POWER_SHUTDOWN, /**< DAC output is high impedance */ + POWER_NORMAL /**< DAC output is enabled */ + }; + + /** Create an MCP4922 object connected to the specified SPI pins + * + * @param mosi The SPI data out pin. + * @param sclk The SPI clock pin. + * @param cs The SPI chip select pin. + * @param hz The SPI bus frequency (defaults to 20MHz). + */ + MCP4922(PinName mosi, PinName miso, PinName sclk, PinName cs, int hz = 20000000); + + /** Get the current reference mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to read from. + * + * @returns The current reference mode of the specified DAC as a ReferenceMode enum. + */ + MCP4922::ReferenceMode referenceMode(DAC dac); + + /** Set the reference mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to write to. + * @param mode The new reference mode for the specified DAC as a ReferenceMode enum. + */ + void referenceMode(DAC dac, ReferenceMode mode); + + /** Get the current gain mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to read from. + * + * @returns The current gain mode of the specified DAC as a GainMode enum. + */ + MCP4922::GainMode gainMode(DAC dac); + + /** Set the gain mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to write to. + * @param mode The new gain mode for the specified DAC as a GainMode enum. + */ + void gainMode(DAC dac, GainMode mode); + + /** Get the current power mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to read from. + * + * @returns The current power mode of the specified DAC as a PowerMode enum. + */ + MCP4922::PowerMode powerMode(DAC dac); + + /** Set the power mode of the specified DAC in the MCP4922 + * + * @param dac The DAC to write to. + * @param mode The new power mode for the specified DAC as a PowerMode enum. + */ + void powerMode(DAC dac, PowerMode mode); + + /** Get the current output voltage of the specified DAC in the MCP4922 as a percentage + * + * @param dac The DAC to read from. + * + * @returns The current output voltage of the specified DAC as a percentage (0.0 to 1.0 * VDD). + */ + float read(DAC dac); + + uint16_t read_u16(DAC dac); + + /** Set the output voltage of the specified DAC in the MCP4922 from a percentage + * + * @param dac The DAC to write to. + * @param value The new output voltage for the specified DAC as a percentage (0.0 to 1.0 * VDD). + */ + void write(DAC dac, float value); + + /** Set the output voltage of the specified DAC in the MCP4922 from a 16-bit range + * + * @param dac The DAC to write to. + * @param value The new output voltage for the specified DAC as a 16-bit unsigned short (0x0000 to 0xFFFF). + */ + void write_u16(DAC dac, uint16_t value); + + // focntion directte sur les bits : + void write_bits(DAC dac, uint16_t value); + + // DAC A : + void write_DAC_A(uint16_t value); + + // DAC B : + void write_DAC_B(uint16_t value); + +private: + //SPI member variables + SPI m_SPI; + DigitalOut m_CS; + + //DAC settings member variables + unsigned short m_DacValueA; + unsigned short m_DacValueB; + + //Internal functions + void writeDac(unsigned short value); +}; + +#endif + + + + + + +#if 0 + /* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) * @@ -112,4 +299,5 @@ }; +#endif #endif \ No newline at end of file