lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

Committer:
lyreliu
Date:
Sun Mar 06 11:28:55 2016 +0000
Revision:
0:d6ad48301389
Child:
1:d9a739fe837e
hte program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lyreliu 0:d6ad48301389 1 #include "mbed.h"
lyreliu 0:d6ad48301389 2
lyreliu 0:d6ad48301389 3 void MassCompute(float); // The function of computing mass from voltage.
lyreliu 0:d6ad48301389 4 void SerialInit(void); // The function fo serial initial
lyreliu 0:d6ad48301389 5 void SerialTransmit(void); // Transmit the data through the serial
lyreliu 0:d6ad48301389 6
lyreliu 0:d6ad48301389 7 AnalogIn voltage(p20); // AnalogIn is a class of anlog input, p20 is the pin you use for anlog input,
lyreliu 0:d6ad48301389 8 // You can change the pin from pin15 to pin20
lyreliu 0:d6ad48301389 9 Serial pc(USBTX, USBRX); // Communicate with PC through the serial
lyreliu 0:d6ad48301389 10
lyreliu 0:d6ad48301389 11 float mass; // The mass of the object
lyreliu 0:d6ad48301389 12 unsigned short int mass_trans; // A 8 bits mass used for transmitting
lyreliu 0:d6ad48301389 13
lyreliu 0:d6ad48301389 14 int main()
lyreliu 0:d6ad48301389 15 {
lyreliu 0:d6ad48301389 16 SerialInit(); //Initial the serial
lyreliu 0:d6ad48301389 17 while(1)
lyreliu 0:d6ad48301389 18 {
lyreliu 0:d6ad48301389 19 MassCompute(voltage.read());
lyreliu 0:d6ad48301389 20 SerialTransmit();
lyreliu 0:d6ad48301389 21 wait_ms(100); //Wait for 100ms, this is the sample time.
lyreliu 0:d6ad48301389 22 }
lyreliu 0:d6ad48301389 23 }
lyreliu 0:d6ad48301389 24
lyreliu 0:d6ad48301389 25 void MassCompute(float voltage)
lyreliu 0:d6ad48301389 26 {
lyreliu 0:d6ad48301389 27 float ratio; //The amplified factor of the OA
lyreliu 0:d6ad48301389 28 float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3
lyreliu 0:d6ad48301389 29
lyreliu 0:d6ad48301389 30 vol_ref = 3.3;
lyreliu 0:d6ad48301389 31
lyreliu 0:d6ad48301389 32 mass = 3000*vol_ref*voltage/ratio + 0.3; //The compute function of mass from the voltage
lyreliu 0:d6ad48301389 33 }
lyreliu 0:d6ad48301389 34
lyreliu 0:d6ad48301389 35 void SerialInit(void)
lyreliu 0:d6ad48301389 36 {
lyreliu 0:d6ad48301389 37 pc.baud(9600); //Set the baud rate
lyreliu 0:d6ad48301389 38 pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1
lyreliu 0:d6ad48301389 39 }
lyreliu 0:d6ad48301389 40
lyreliu 0:d6ad48301389 41 void SerialTransmit(void)
lyreliu 0:d6ad48301389 42 {
lyreliu 0:d6ad48301389 43 if(mass>3)
lyreliu 0:d6ad48301389 44 mass =3;
lyreliu 0:d6ad48301389 45 mass_trans = mass*85;
lyreliu 0:d6ad48301389 46 pc.printf("%d",mass_trans);
lyreliu 0:d6ad48301389 47 }