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 0:95be7403a10c, committed 2012-08-29
- Comitter:
- ykuroda
- Date:
- Wed Aug 29 11:01:51 2012 +0000
- Child:
- 1:ac3b7f978aa3
- Commit message:
- Analog Devices AD7490 16 channels, 1MSPS, 12Bit ADC with SPI interface
Changed in this revision
| AD7490.cpp | Show annotated file Show diff for this revision Revisions of this file |
| AD7490.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AD7490.cpp Wed Aug 29 11:01:51 2012 +0000
@@ -0,0 +1,179 @@
+#include "mbed.h"
+#include "AD7490.h"
+
+AD7490::AD7490(SPI _spi, PinName _cs)
+: spi(_spi),
+ cs(_cs)
+{
+ short x = 0;
+
+ for(int i=0; i<3; i++){
+
+ x = 0;
+ x |= CREG_WRITE;
+ // x |= CREG_SEQ;
+ // x |= CREG_SHADOW;
+ x |= CREG_ADD3;
+ x |= CREG_ADD2;
+ x |= CREG_ADD1;
+ x |= CREG_ADD0;
+ x |= CREG_PM1;
+ x |= CREG_PM0;
+ x |= CREG_RANGE;
+ x |= CREG_CODING;
+
+ cs = 0;
+ ret = spi.write(x<<4);
+ cs = 1;
+
+// printf("INIT: send = 0x%X, ret = 0x%X\n", x, ret);
+ }
+}
+
+AD7490::~AD7490()
+{
+}
+
+unsigned short
+AD7490::convert(int ch)
+{
+ unsigned short x = 0;
+
+ x |= CREG_WRITE;
+// x |= CREG_SEQ;
+// x |= CREG_SHADOW;
+// x |= CREG_ADD3;
+// x |= CREG_ADD2;
+// x |= CREG_ADD1;
+ x |= (CREG_ADD0|CREG_ADD1|CREG_ADD2|CREG_ADD3) & (ch<<6);
+
+ x |= CREG_PM1;
+ x |= CREG_PM0;
+ x |= CREG_RANGE;
+ x |= CREG_CODING;
+
+ cs = 0;
+ ret = spi.write(x<<4);
+ cs = 1;
+
+// printf("send = 0x%X, ch = %2d, ret = %d\n", x, (ret>>12)&0xF, ret&0xFFF);
+
+ return ret;
+}
+
+void
+AD7490::convert(short data[])
+{
+ for(int ch=0; ch<16; ch++) data[ch]=0;
+ for(int ch=0; ch<16; ch++){
+ unsigned short ret = convert(ch);
+
+ data[ ret>>12&0xF ] = ret&0xFFF;
+ }
+}
+
+
+unsigned short
+AD7490::sequential(int ch)
+{
+ unsigned short x = 0;
+
+ x |= CREG_WRITE;
+ x |= CREG_SEQ;
+ x |= CREG_SHADOW;
+// x |= CREG_ADD3;
+// x |= CREG_ADD2;
+// x |= CREG_ADD1;
+ x |= (CREG_ADD0|CREG_ADD1|CREG_ADD2|CREG_ADD3) & (ch<<6);
+
+ x |= CREG_PM1;
+ x |= CREG_PM0;
+ x |= CREG_RANGE;
+ x |= CREG_CODING;
+
+ cs = 0;
+ ret = spi.write(x<<4);
+ cs = 1;
+
+ return ret;
+}
+
+
+
+unsigned short
+AD7490::read(void)
+{
+ unsigned short x = 0;
+
+// x |= CREG_WRITE;
+// x |= CREG_SEQ;
+// x |= CREG_SHADOW;
+// x |= CREG_ADD3;
+// x |= CREG_ADD2;
+// x |= CREG_ADD1;
+// x |= (CREG_ADD0|CREG_ADD1|CREG_ADD2|CREG_ADD3) & (ch<<6);
+
+// x |= CREG_PM1;
+// x |= CREG_PM0;
+// x |= CREG_RANGE;
+// x |= CREG_CODING;
+
+ cs = 0;
+ ret = spi.write(x<<4);
+ cs = 1;
+
+// printf("send = 0x%X, ch = %2d, ret = %d\n", x, (ret>>12)&0xF, ret&0xFFF);
+
+ return ret;
+}
+
+void
+AD7490::read(short data[])
+{
+ for(int ch=0; ch<16; ch++) data[ch]=0;
+ for(int ch=0; ch<16; ch++){
+ unsigned short ret = read();
+
+ data[ ret>>12&0xF ] = ret&0xFFF;
+ }
+}
+
+
+
+
+
+
+#if 0
+int main() {
+
+ wait(1);
+
+ // set up SPI interface
+ SPI spi(p11,p12,p13);
+ spi.format(16,0);
+ spi.frequency(1000000);
+
+ AD7490 ad1(spi, p9);
+ AD7490 ad2(spi, p10);
+
+ while(1) {
+
+ int ret=0;
+ for(int ch=0; ch<16; ch++){
+ ret = ad1.convert(ch);
+ printf("ch=%2d, ret= %d, %f\n", (ret>>12)&0xF, ret&0xFFF, 3.3*(float)(ret&0xFFF)/0xFFF);
+ }
+ printf("\n");
+ for(int ch=0; ch<16; ch++){
+ ret = ad2.convert(ch);
+ printf("ch=%2d, ret= %d, %f\n", (ret>>12)&0xF, ret&0xFFF, 3.3*(float)(ret&0xFFF)/0xFFF);
+ }
+ printf("\n");
+
+ myled = 1;
+ wait(0.1);
+ myled = 0;
+ wait(0.9);
+ }
+}
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AD7490.h Wed Aug 29 11:01:51 2012 +0000
@@ -0,0 +1,73 @@
+//
+// AD7490
+//
+#ifndef _AD7490_H
+#define _AD7490_H
+
+class AD7490 {
+ protected:
+ #define CREG_WRITE 0x800
+ #define CREG_SEQ 0x400
+ #define CREG_ADD3 0x200
+ #define CREG_ADD2 0x100
+ #define CREG_ADD1 0x080
+ #define CREG_ADD0 0x040
+ #define CREG_PM1 0x020
+ #define CREG_PM0 0x010
+ #define CREG_SHADOW 0x008
+ #define CREG_WEAK 0x004
+ #define CREG_RANGE 0x002
+ #define CREG_CODING 0x001
+
+ SPI spi;
+ DigitalOut cs;
+ short ret;
+
+ public:
+ AD7490(SPI _spi, PinName _cs);
+ ~AD7490();
+ unsigned short convert(int ch=0);
+ void convert(short data[]);
+
+ unsigned short sequential(int ch=15);
+ unsigned short read(void);
+ void read(short data[]);
+};
+
+
+#if 0
+int main() {
+
+ wait(1);
+
+ // set up SPI interface
+ SPI spi(p11,p12,p13);
+ spi.format(16,0);
+ spi.frequency(1000000);
+
+ AD7490 ad1(spi, p9);
+ AD7490 ad2(spi, p10);
+
+ while(1) {
+
+ int ret=0;
+ for(int ch=0; ch<16; ch++){
+ ret = ad1.convert(ch);
+ printf("ch=%2d, ret= %d, %f\n", (ret>>12)&0xF, ret&0xFFF, 3.3*(float)(ret&0xFFF)/0xFFF);
+ }
+ printf("\n");
+ for(int ch=0; ch<16; ch++){
+ ret = ad2.convert(ch);
+ printf("ch=%2d, ret= %d, %f\n", (ret>>12)&0xF, ret&0xFFF, 3.3*(float)(ret&0xFFF)/0xFFF);
+ }
+ printf("\n");
+
+ myled = 1;
+ wait(0.1);
+ myled = 0;
+ wait(0.9);
+ }
+}
+#endif // test main
+
+#endif // _AD7490_H