Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:8af87a432c12, 2016-03-06 (annotated)
- Committer:
- lyreliu
- Date:
- Sun Mar 06 13:41:41 2016 +0000
- Revision:
- 4:8af87a432c12
- Parent:
- 3:2566acb12c47
- Child:
- 5:ff47bcaebc64
lpc1768 program
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| lyreliu | 0:d6ad48301389 | 1 | #include "mbed.h" |
| lyreliu | 1:d9a739fe837e | 2 | #define RATIO 1 //The amplified factor of the OA ?????????????????????????????????????????????????? |
| lyreliu | 4:8af87a432c12 | 3 | #define SAMPLE_TIME 100 // Sample time, default is 100ms |
| lyreliu | 0:d6ad48301389 | 4 | void MassCompute(float); // The function of computing mass from voltage. |
| lyreliu | 0:d6ad48301389 | 5 | void SerialInit(void); // The function fo serial initial |
| lyreliu | 0:d6ad48301389 | 6 | void SerialTransmit(void); // Transmit the data through the serial |
| lyreliu | 0:d6ad48301389 | 7 | |
| lyreliu | 0:d6ad48301389 | 8 | AnalogIn voltage(p20); // AnalogIn is a class of anlog input, p20 is the pin you use for anlog input, |
| lyreliu | 0:d6ad48301389 | 9 | // You can change the pin from pin15 to pin20 |
| lyreliu | 0:d6ad48301389 | 10 | Serial pc(USBTX, USBRX); // Communicate with PC through the serial |
| lyreliu | 2:65d319447726 | 11 | |
| lyreliu | 2:65d319447726 | 12 | DigitalOut high_pin(p5); //test pin, p5 is the high level pin |
| lyreliu | 2:65d319447726 | 13 | DigitalOut low_pin(p6); //test pin, p6 is the low level pin |
| lyreliu | 0:d6ad48301389 | 14 | |
| lyreliu | 0:d6ad48301389 | 15 | float mass; // The mass of the object |
| lyreliu | 3:2566acb12c47 | 16 | unsigned short int mass_trans_high; // A 8 bits mass used for transmitting |
| lyreliu | 3:2566acb12c47 | 17 | unsigned short int mass_trans_low; |
| lyreliu | 0:d6ad48301389 | 18 | |
| lyreliu | 0:d6ad48301389 | 19 | int main() |
| lyreliu | 0:d6ad48301389 | 20 | { |
| lyreliu | 0:d6ad48301389 | 21 | SerialInit(); //Initial the serial |
| lyreliu | 2:65d319447726 | 22 | |
| lyreliu | 2:65d319447726 | 23 | high_pin = 1; |
| lyreliu | 2:65d319447726 | 24 | low_pin = 0; |
| lyreliu | 2:65d319447726 | 25 | |
| lyreliu | 0:d6ad48301389 | 26 | while(1) |
| lyreliu | 0:d6ad48301389 | 27 | { |
| lyreliu | 0:d6ad48301389 | 28 | MassCompute(voltage.read()); |
| lyreliu | 0:d6ad48301389 | 29 | SerialTransmit(); |
| lyreliu | 4:8af87a432c12 | 30 | wait_ms(SAMPLE_TIME); //Wait for 100ms, this is the sample time. |
| lyreliu | 0:d6ad48301389 | 31 | } |
| lyreliu | 0:d6ad48301389 | 32 | } |
| lyreliu | 0:d6ad48301389 | 33 | |
| lyreliu | 0:d6ad48301389 | 34 | void MassCompute(float voltage) |
| lyreliu | 0:d6ad48301389 | 35 | { |
| lyreliu | 1:d9a739fe837e | 36 | //float ratio; //The amplified factor of the OA |
| lyreliu | 0:d6ad48301389 | 37 | float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3 |
| lyreliu | 0:d6ad48301389 | 38 | |
| lyreliu | 0:d6ad48301389 | 39 | vol_ref = 3.3; |
| lyreliu | 0:d6ad48301389 | 40 | |
| lyreliu | 1:d9a739fe837e | 41 | mass = 3000*vol_ref*voltage/RATIO + 0.3; //The compute function of mass from the voltage |
| lyreliu | 0:d6ad48301389 | 42 | } |
| lyreliu | 0:d6ad48301389 | 43 | |
| lyreliu | 0:d6ad48301389 | 44 | void SerialInit(void) |
| lyreliu | 0:d6ad48301389 | 45 | { |
| lyreliu | 0:d6ad48301389 | 46 | pc.baud(9600); //Set the baud rate |
| lyreliu | 0:d6ad48301389 | 47 | pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1 |
| lyreliu | 0:d6ad48301389 | 48 | } |
| lyreliu | 0:d6ad48301389 | 49 | |
| lyreliu | 0:d6ad48301389 | 50 | void SerialTransmit(void) |
| lyreliu | 0:d6ad48301389 | 51 | { |
| lyreliu | 3:2566acb12c47 | 52 | unsigned int start,end; |
| lyreliu | 3:2566acb12c47 | 53 | start = 0xfe; |
| lyreliu | 3:2566acb12c47 | 54 | end = 0xff; |
| lyreliu | 0:d6ad48301389 | 55 | if(mass>3) |
| lyreliu | 0:d6ad48301389 | 56 | mass =3; |
| lyreliu | 3:2566acb12c47 | 57 | mass_trans_low = (int)(mass*10000); |
| lyreliu | 3:2566acb12c47 | 58 | mass_trans_high = (int)(mass*10000)>>8; |
| lyreliu | 3:2566acb12c47 | 59 | pc.printf("%d",start); |
| lyreliu | 3:2566acb12c47 | 60 | pc.printf("%d",mass_trans_high); |
| lyreliu | 3:2566acb12c47 | 61 | pc.printf("%d",mass_trans_low); |
| lyreliu | 3:2566acb12c47 | 62 | pc.printf("%d",end); |
| lyreliu | 0:d6ad48301389 | 63 | } |