Read analog value from MCP3201 and send it to serial channel. A vector with 255 pos.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
skelter
Date:
Fri May 06 18:52:40 2016 +0000
Commit message:
Commit start

Changed in this revision

VoltageRead.h Show annotated file Show diff for this revision Revisions of this file
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 4d02779757de VoltageRead.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VoltageRead.h	Fri May 06 18:52:40 2016 +0000
@@ -0,0 +1,36 @@
+
+#include <math.h>
+
+#define VOLTAGE_SCALE_FACTOR    3.3/4096
+#define CHANNEL_OFFSET          1.72*(4096/3.3)//2048
+
+
+DigitalOut flag(p9);
+
+float get_voltage_rms(void)
+{
+  
+  int sum=0;
+  int voltage[VOLTAGE_SAMPLES];
+
+  
+  // loop takes 7.3ms
+  // with wait_us(1) loop takes 7.65ms. diff = 0.35ms
+  // with wait_us(2) loop takes 7.91ms. diff = 0,61ms
+  // with wait_us(3) loop takes 8.15ms. diff = 0,85ms
+  flag=1;
+  for(int i=0;i<VOLTAGE_SAMPLES;i++)
+  {
+    voltage[i]=(float)ReadAnalogMCP3201() - CHANNEL_OFFSET;
+    sum+=(voltage[i]*voltage[i]);
+    wait_us(36);
+  }  
+  flag=0;   
+  
+  //dump the AD samples to global variable "samples"
+  for(int i=0;i<VOLTAGE_SAMPLES;i++) samples[i]=voltage[i];
+  
+  //calculate RMS
+  return (float)sqrt((double)(((float)sum / VOLTAGE_SAMPLES)))*VOLTAGE_SCALE_FACTOR;
+      
+}
\ No newline at end of file
diff -r 000000000000 -r 4d02779757de main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 06 18:52:40 2016 +0000
@@ -0,0 +1,84 @@
+/* 
+    Read analog channel from MCP3201 with SPI interface and send it to the serial interface.
+    Matlab can be used to evaluate the sampled wave.
+*/
+
+
+#include "mbed.h"
+
+#define VOLTAGE_SAMPLES         255
+
+// for samples serial dumping 
+int samples[255];
+
+#include "mcp3201.h"
+#include "VoltageRead.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+int main() 
+{
+    pc.baud(115200);
+    pc.printf("Ola Sr. Matlab!");
+    
+    while(1) 
+    {  
+        float rms_value=get_voltage_rms();
+        for(int i=0;i<VOLTAGE_SAMPLES;i++) pc.printf("%i\r\n", samples[i]);
+        //end of com
+        pc.printf(" ");
+        
+        //run Matlab script again to get new sample
+        char c = pc.getc();
+    }
+}
+
+/*
+s=serial('COM6','BAUD',115200,'Terminator',' ','InputBufferSize', 8096,'OutputBufferSize',1024);
+fopen(s);
+fprintf(s,'c','async');
+x=fscanf(s);
+y=str2num(x);
+
+ADC_OFFSET=floor(1.7*(4096/3.3));
+ADC_SCALE=(2110*sqrt(2))/4096; %Valor de Tensao no pino 3.3/4096
+VOLTAGE_SAMPLES=256;
+
+t=linspace(0,inv(60)*VOLTAGE_SAMPLES/256,VOLTAGE_SAMPLES-1);
+
+figure(1);
+subplot(2,1,1)
+%stem(y.*(3.3/4096),'x.'); grid;
+stem(y+ADC_OFFSET,'.'); grid;
+title('Sampled Wave from MCP3201');
+xlabel('Sample Number [0...255]');
+ylabel('Sampled Value [0...4095)');
+
+subplot(2,1,2)
+stem(y,'.','b'); grid;
+title('Offset Adjustment to RMS calculation');
+xlabel('Sample Number [0...255]');
+ylabel('Sampled Value [-2048...2047)');
+
+figure(2);
+stairs(t,y.*(ADC_SCALE)); grid;
+title('Reconstructed wave');
+xlabel('time [s]');
+ylabel('Voltage [Volts)');
+%hold on
+%plot(y.*(ADC_SCALE),'--');
+%hold off
+
+rms=sqrt(mean((y.^2)))*ADC_SCALE;
+disp('  Valor RMS')
+disp(rms)
+
+fclose(s);
+clear s;
+% sometimes the channel remains open, use this
+% fclose(instrfind)
+*/
+
+
+    
+
diff -r 000000000000 -r 4d02779757de mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 06 18:52:40 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 4d02779757de mcp3201.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp3201.h	Fri May 06 18:52:40 2016 +0000
@@ -0,0 +1,34 @@
+/*
+    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.
+#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)
+{
+ 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