lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

Revision:
0:d6ad48301389
Child:
1:d9a739fe837e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 06 11:28:55 2016 +0000
@@ -0,0 +1,47 @@
+#include "mbed.h"
+
+void MassCompute(float);            // The function of computing mass from voltage.
+void SerialInit(void);              // The function fo serial initial
+void SerialTransmit(void);          // Transmit the data through the serial
+
+AnalogIn voltage(p20);              // AnalogIn is a class of anlog input, p20 is the pin you use for anlog input,
+                                    // You can change the pin from pin15 to pin20
+Serial pc(USBTX, USBRX);            // Communicate with PC through the serial
+                                    
+float mass;                         // The mass of the object
+unsigned short int mass_trans;       // A 8 bits mass used for transmitting
+
+int main()
+{
+    SerialInit();                   //Initial the serial
+    while(1)
+    {
+        MassCompute(voltage.read());  
+        SerialTransmit();
+        wait_ms(100);               //Wait for 100ms, this is the sample time.
+    }      
+}
+
+void MassCompute(float voltage)
+{
+    float ratio;                    //The amplified factor of the OA
+    float vol_ref;                  //Reference voltage, the basic voltage of the MCU, the default number is 3.3
+    
+    vol_ref = 3.3;
+    
+    mass = 3000*vol_ref*voltage/ratio + 0.3;        //The compute function of mass from the voltage
+}
+
+void SerialInit(void)
+{
+    pc.baud(9600);                     //Set the baud rate
+    pc.format(8,SerialBase::None,1);   //8bit number,no parity has been used,stop bits 1 
+}
+
+void SerialTransmit(void)
+{   
+    if(mass>3)
+        mass =3;
+    mass_trans = mass*85;
+    pc.printf("%d",mass_trans);
+}
\ No newline at end of file