lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

Committer:
lyreliu
Date:
Sun Mar 06 12:05:15 2016 +0000
Revision:
2:65d319447726
Parent:
1:d9a739fe837e
Child:
3:2566acb12c47
lpc1768 program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lyreliu 0:d6ad48301389 1 #include "mbed.h"
lyreliu 1:d9a739fe837e 2 #define RATIO 1 //The amplified factor of the OA ??????????????????????????????????????????????????
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 2:65d319447726 10
lyreliu 2:65d319447726 11 DigitalOut high_pin(p5); //test pin, p5 is the high level pin
lyreliu 2:65d319447726 12 DigitalOut low_pin(p6); //test pin, p6 is the low level pin
lyreliu 0:d6ad48301389 13
lyreliu 0:d6ad48301389 14 float mass; // The mass of the object
lyreliu 0:d6ad48301389 15 unsigned short int mass_trans; // A 8 bits mass used for transmitting
lyreliu 0:d6ad48301389 16
lyreliu 0:d6ad48301389 17 int main()
lyreliu 0:d6ad48301389 18 {
lyreliu 0:d6ad48301389 19 SerialInit(); //Initial the serial
lyreliu 2:65d319447726 20
lyreliu 2:65d319447726 21 high_pin = 1;
lyreliu 2:65d319447726 22 low_pin = 0;
lyreliu 2:65d319447726 23
lyreliu 0:d6ad48301389 24 while(1)
lyreliu 0:d6ad48301389 25 {
lyreliu 0:d6ad48301389 26 MassCompute(voltage.read());
lyreliu 0:d6ad48301389 27 SerialTransmit();
lyreliu 0:d6ad48301389 28 wait_ms(100); //Wait for 100ms, this is the sample time.
lyreliu 0:d6ad48301389 29 }
lyreliu 0:d6ad48301389 30 }
lyreliu 0:d6ad48301389 31
lyreliu 0:d6ad48301389 32 void MassCompute(float voltage)
lyreliu 0:d6ad48301389 33 {
lyreliu 1:d9a739fe837e 34 //float ratio; //The amplified factor of the OA
lyreliu 0:d6ad48301389 35 float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3
lyreliu 0:d6ad48301389 36
lyreliu 0:d6ad48301389 37 vol_ref = 3.3;
lyreliu 0:d6ad48301389 38
lyreliu 1:d9a739fe837e 39 mass = 3000*vol_ref*voltage/RATIO + 0.3; //The compute function of mass from the voltage
lyreliu 0:d6ad48301389 40 }
lyreliu 0:d6ad48301389 41
lyreliu 0:d6ad48301389 42 void SerialInit(void)
lyreliu 0:d6ad48301389 43 {
lyreliu 0:d6ad48301389 44 pc.baud(9600); //Set the baud rate
lyreliu 0:d6ad48301389 45 pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1
lyreliu 0:d6ad48301389 46 }
lyreliu 0:d6ad48301389 47
lyreliu 0:d6ad48301389 48 void SerialTransmit(void)
lyreliu 0:d6ad48301389 49 {
lyreliu 0:d6ad48301389 50 if(mass>3)
lyreliu 0:d6ad48301389 51 mass =3;
lyreliu 0:d6ad48301389 52 mass_trans = mass*85;
lyreliu 0:d6ad48301389 53 pc.printf("%d",mass_trans);
lyreliu 0:d6ad48301389 54 }