lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

Committer:
lyreliu
Date:
Sun Mar 06 13:36:17 2016 +0000
Revision:
3:2566acb12c47
Parent:
2:65d319447726
Child:
4:8af87a432c12
lpc1786 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 3:2566acb12c47 15 unsigned short int mass_trans_high; // A 8 bits mass used for transmitting
lyreliu 3:2566acb12c47 16 unsigned short int mass_trans_low;
lyreliu 0:d6ad48301389 17
lyreliu 0:d6ad48301389 18 int main()
lyreliu 0:d6ad48301389 19 {
lyreliu 0:d6ad48301389 20 SerialInit(); //Initial the serial
lyreliu 2:65d319447726 21
lyreliu 2:65d319447726 22 high_pin = 1;
lyreliu 2:65d319447726 23 low_pin = 0;
lyreliu 2:65d319447726 24
lyreliu 0:d6ad48301389 25 while(1)
lyreliu 0:d6ad48301389 26 {
lyreliu 0:d6ad48301389 27 MassCompute(voltage.read());
lyreliu 0:d6ad48301389 28 SerialTransmit();
lyreliu 0:d6ad48301389 29 wait_ms(100); //Wait for 100ms, this is the sample time.
lyreliu 0:d6ad48301389 30 }
lyreliu 0:d6ad48301389 31 }
lyreliu 0:d6ad48301389 32
lyreliu 0:d6ad48301389 33 void MassCompute(float voltage)
lyreliu 0:d6ad48301389 34 {
lyreliu 1:d9a739fe837e 35 //float ratio; //The amplified factor of the OA
lyreliu 0:d6ad48301389 36 float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3
lyreliu 0:d6ad48301389 37
lyreliu 0:d6ad48301389 38 vol_ref = 3.3;
lyreliu 0:d6ad48301389 39
lyreliu 1:d9a739fe837e 40 mass = 3000*vol_ref*voltage/RATIO + 0.3; //The compute function of mass from the voltage
lyreliu 0:d6ad48301389 41 }
lyreliu 0:d6ad48301389 42
lyreliu 0:d6ad48301389 43 void SerialInit(void)
lyreliu 0:d6ad48301389 44 {
lyreliu 0:d6ad48301389 45 pc.baud(9600); //Set the baud rate
lyreliu 0:d6ad48301389 46 pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1
lyreliu 0:d6ad48301389 47 }
lyreliu 0:d6ad48301389 48
lyreliu 0:d6ad48301389 49 void SerialTransmit(void)
lyreliu 0:d6ad48301389 50 {
lyreliu 3:2566acb12c47 51 unsigned int start,end;
lyreliu 3:2566acb12c47 52 start = 0xfe;
lyreliu 3:2566acb12c47 53 end = 0xff;
lyreliu 0:d6ad48301389 54 if(mass>3)
lyreliu 0:d6ad48301389 55 mass =3;
lyreliu 3:2566acb12c47 56 mass_trans_low = (int)(mass*10000);
lyreliu 3:2566acb12c47 57 mass_trans_high = (int)(mass*10000)>>8;
lyreliu 3:2566acb12c47 58 pc.printf("%d",start);
lyreliu 3:2566acb12c47 59 pc.printf("%d",mass_trans_high);
lyreliu 3:2566acb12c47 60 pc.printf("%d",mass_trans_low);
lyreliu 3:2566acb12c47 61 pc.printf("%d",end);
lyreliu 0:d6ad48301389 62 }