位置情報を知るための電圧値を表示させる

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
sayan2
Date:
Sat Mar 14 15:59:03 2020 +0000
Parent:
1:c3989f45366c
Commit message:
output Average of V

Changed in this revision

adc.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c3989f45366c -r d665c2b3e24f adc.cpp
--- a/adc.cpp	Tue Mar 10 09:13:16 2020 +0000
+++ b/adc.cpp	Sat Mar 14 15:59:03 2020 +0000
@@ -3,30 +3,42 @@
 SPI mySPI(D11, D12, A4); // mosi, miso, sclk
 DigitalOut cs(D10);
 Serial pc(USBTX, USBRX); // tx, rx
-float Vref = 5.0 ;
+double Vref = 5.0 ;
 
 int main(){ 
-            cs=1;
-            mySPI.format(8,0); //Setup the spi for 10 bit data, spi mode 0
-            mySPI.frequency(1000000); // second edge capture, with a 1MHz clock rate
-
+        cs=1;
+        mySPI.format(8,0); //Setup the spi for 10 bit data, spi mode 0
+        mySPI.frequency(1000000); // second edge capture, with a 1MHz clock rate
+        
+        int n=20;  //size of "n" TBD
+        int i;
+        double sum_volts;
+            
         while (1){
-            cs=0;
+            for(i=0;i<n;i++){
+                        cs=0;
             
-            int highByte = mySPI.write(0b01101000); //Send the command to recieve the slave data
-            pc.printf("highByte: %d ",highByte);
+                        int highByte = mySPI.write(0b01101000); //Send the command to recieve the slave data
+                        //pc.printf("highByte: %d ",highByte);
+            
+                        int lowByte = mySPI.write(0xFF); // send dummy byte to receive the slave data
+                        //pc.printf("lowByte: %d ",lowByte);
             
-            int lowByte = mySPI.write(0xFF); // send dummy byte to receive the slave data
-            pc.printf("lowByte: %d ",lowByte);
+                        int wasteByte = mySPI.write(0x00);
             
-            int wasteByte = mySPI.write(0x00);
-            
-            cs=1;
+                        cs=1;
 
-            int dataCh0 =  ((highByte << 8) + lowByte)&0x03FF;
-            float volts = dataCh0*Vref /1024;
-            pc.printf("CH0 %f V\n",volts);
-            //cs=1;
-            wait(3);
+                        int dataCh0 =  ((highByte << 8) + lowByte) & 0x03FF;
+                        double volts = dataCh0*Vref / 1024;
+            
+                        sum_volts = sum_volts + volts;
             }
+        
+            double ave_volts = sum_volts / n ;        //get the average to reduce the error range
+            pc.printf("CH0 %f V\n",ave_volts);
+            
+            sum_volts = 0.0;        //initialize
+
+            wait(3);        //TBD
+        }
 }
\ No newline at end of file