Potentiometer AD5204,AD5206 library.
Revision 0:1d45c35a717b, committed 2015-10-18
- Comitter:
- matsujirushi
- Date:
- Sun Oct 18 05:12:59 2015 +0000
- Commit message:
- created.
Changed in this revision
MjAD520x.cpp | Show annotated file Show diff for this revision Revisions of this file |
MjAD520x.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 1d45c35a717b MjAD520x.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MjAD520x.cpp Sun Oct 18 05:12:59 2015 +0000 @@ -0,0 +1,27 @@ +#include "MjAD520x.h" + +MjAD520x::MjAD520x(PinName mosi, PinName miso, PinName sclk, PinName cs_n, int terminalResistance) + : HwSpi(mosi, miso, sclk), HwCs_n(cs_n, 1) +{ + TerminalResistance = terminalResistance; + + HwSpi.format(11, 0); + HwSpi.frequency(1000000); +} + +void MjAD520x::write(int channel, int resistance) +{ + write_u8(channel, resistance <= TerminalResistance ? resistance * 255 / TerminalResistance : 255); +} + +void MjAD520x::write_u8(int channel, uint8_t value) +{ + HwCs_n.write(0); + wait_us(1); + + HwSpi.write(channel << 8 | value); + + HwCs_n.write(1); + wait_us(1); +} +
diff -r 000000000000 -r 1d45c35a717b MjAD520x.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MjAD520x.h Sun Oct 18 05:12:59 2015 +0000 @@ -0,0 +1,20 @@ +#ifndef MJ_AD520X_H +#define MJ_AD520X_H + +#include "mbed.h" + +class MjAD520x +{ +public: + MjAD520x(PinName mosi, PinName miso, PinName sclk, PinName cs_n, int terminalResistance); + void write(int channel, int resistance); + void write_u8(int channel, uint8_t value); + +private: + SPI HwSpi; + DigitalOut HwCs_n; + int TerminalResistance; + +}; + +#endif // MJ_AD520X_H \ No newline at end of file