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.
Diff: AnalogIO_1ch/MCP4921.cpp
- Revision:
- 0:f69d3c64978d
- Child:
- 1:d8449dbcde02
diff -r 000000000000 -r f69d3c64978d AnalogIO_1ch/MCP4921.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AnalogIO_1ch/MCP4921.cpp Mon Jul 28 07:30:22 2014 +0000 @@ -0,0 +1,43 @@ +//------------------------------------------------------ +// Class for 1 DAC in MCO4922 +// +// 2014/07/18, Copyright (c) 2014 MIKAMI, Naoki +//------------------------------------------------------ + +#include "MCP4921.hpp" + +namespace Mikami +{ + DacMCP4921::DacMCP4921(PinName mosi, PinName sclk, + PinName cs, int hz) + : mySpi_(mosi, NC, sclk, PA_4), myCs_(cs, 1) + { + // Set SPI format and bus frequency + mySpi_.format(16, 0); + mySpi_.frequency(hz); + + // Set DAC to 0 + WriteDac(0); + } + + void DacMCP4921::Write(float value) + { + if (value < -1.0f) value = -1.0f; + if (value > 1.0f) value = 1.0f; + + WriteDac((uint16_t)((value + 1.0f)*2047)); + } + + void DacMCP4921::Write(uint16_t value) + { + WriteDac((value > 4095) ? 4095 : value); + } + + void DacMCP4921::WriteDac(uint16_t value) + { + myCs_.write(0); // cs <= L + mySpi_.write(value | 0x3000); + myCs_.write(1); // cs <= H + } +} +