Read analog value from MCP3201 serial ADC converter SPI interface.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
skelter
Date:
Fri May 06 18:50:55 2016 +0000
Commit message:
commit start

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mcp3201.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f9b5c272f6ed main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 06 18:50:55 2016 +0000
@@ -0,0 +1,27 @@
+/* 
+    Read analog channel from MCP3201 with SPI interface and send it to the serial interface.
+*/
+
+#include "mbed.h"
+#include "mcp3201.h"
+
+#define ADC_SCALE_VALUE 3.3/4096 
+
+Serial pc(USBTX, USBRX); // tx, rx
+ 
+int main() 
+{
+    pc.printf("Analog read test from MCP3201.\n");
+    
+    while(1) 
+    {    
+    float r=(float)ReadAnalogMCP3201() * ADC_SCALE_VALUE; // show value in volts.
+    pc.printf("AD channel value: %4.2f Volts.\r\n", r);
+    pc.printf(" Press any key to get new sample...\r\n\r\n");
+    char c = pc.getc();
+    }
+}
+
+
+    
+
diff -r 000000000000 -r f9b5c272f6ed mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 06 18:50:55 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/99a22ba036c9
\ No newline at end of file
diff -r 000000000000 -r f9b5c272f6ed mcp3201.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp3201.h	Fri May 06 18:50:55 2016 +0000
@@ -0,0 +1,35 @@
+/*
+    Header for MCP3201 Analog SAR Converter with SPI interface 
+    Version: 1.0
+    Julio S.
+*/
+
+// SPI clock freq. in HZ
+// Limits from MCP3201 datasheet are:
+// 1500000Hz when vdd=5V
+// 800000Hz when vdd=2.7V
+// 1MHz should work and is the default value.
+#define MCP3201_CLK_FREQ    1000000
+
+// MCP3201 SPI channel pinout
+// p5 not wired, there are no commands to send for MCP3201 device
+SPI MCP3201(p5,p6,p7);
+// take control from CS pin to avoid transition changes between write commands
+DigitalOut cs(p8); 
+
+
+// Read the analog value from MCP3201.
+// return 12bit interger (0...4095)
+int ReadAnalogMCP3201(void)
+{
+ //uncommente below line only if the MCP3201_CLK_FREQ is diferrent of 1MHz
+ //MCP3201.frequency(MCP3201_CLK_FREQ);
+ 
+ cs=0;
+ int high_byte = MCP3201.write(0x00);
+ int low_byte = MCP3201.write(0x00);
+ cs=1;
+ int conv_result = ((high_byte & 0x1f) << 7) | ((low_byte >> 1) & 0x7f);  
+
+ return conv_result;
+}
\ No newline at end of file