Read an analog value using ADC.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
margadon
Date:
Tue Dec 30 11:26:34 2014 +0000
Parent:
0:c2d1ad5059da
Commit message:
smooth function

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
smooth.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c2d1ad5059da -r 9685565fd3cc main.cpp
--- a/main.cpp	Fri Feb 21 10:22:49 2014 +0000
+++ b/main.cpp	Tue Dec 30 11:26:34 2014 +0000
@@ -1,18 +1,32 @@
 #include "mbed.h"
- 
-AnalogIn analog_value(A0);
- 
+#include "smooth.cpp"
+AnalogIn analog_value(PA_0);
+Serial pc(PA_11, PA_12);/* Serial6 tx rx */
 DigitalOut myled(LED1);
  
 // Calculate the corresponding acquisition measure for a given value in mV
 #define MV(x) ((0xFFF*x)/3300)
+
+uint16_t pre_val;
  
 int main() {
-    while(1) {      
+    while(1) {    
+        //pc.printf("POPIZDOVALI\r\n"); 
         uint16_t meas = analog_value.read_u16(); // Converts and read the analog input value
         if (meas > MV(1000)) { // If the value is greater than 1000 mV toggle the LED
           myled = !myled;
         }
-        wait(0.2); // 200 ms
+        wait(0.8); // 200 mssdfg
+        //char c = pc.getc();
+        //if(c!='q')
+        uint16_t val = 2;//meas/16;
+        uint16_t pre_val = 3;//meas/16;
+        //smooth(&val,&pre_val,3,5);
+        int ert = &val;
+        int ert2 = &pre_val;
+        
+        smooth(ert,ert2,3,5);
+        
+        pc.printf("value %f - %d\r\n",meas/0xFFFF*3.30000,meas/16);
     }
-}
+}
\ No newline at end of file
diff -r c2d1ad5059da -r 9685565fd3cc mbed.bld
--- a/mbed.bld	Fri Feb 21 10:22:49 2014 +0000
+++ b/mbed.bld	Tue Dec 30 11:26:34 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/ed8466a608b4
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file
diff -r c2d1ad5059da -r 9685565fd3cc smooth.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smooth.cpp	Tue Dec 30 11:26:34 2014 +0000
@@ -0,0 +1,33 @@
+//#include "math.h"
+void smooth(uint16_t *input, uint16_t *output, int n, int window)
+{
+   int i,j,z,k1,k2,hw;
+   uint16_t tmp;
+   if((window%2)==0) window++;
+   hw=(window-1)/2;
+   output[0]=input[0];
+
+   for (i=1;i<n;i++){
+       tmp=0;
+       if(i<hw){
+           k1=0;
+           k2=2*i;
+           z=k2+1;
+       }
+       else if((i+hw)>(n-1)){
+           k1=i-n+i+1;
+           k2=n-1;
+           z=k2-k1+1;
+       }
+       else{
+           k1=i-hw;
+           k2=i+hw;
+           z=window;
+       }
+
+       for (j=k1;j<=k2;j++){
+           tmp=tmp+input[j];
+       }
+       output[i]=tmp/z;
+   }
+}
\ No newline at end of file