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@12:623f562fc3db, 2016-03-08 (annotated)
- Committer:
- lyreliu
- Date:
- Tue Mar 08 14:01:32 2016 +0000
- Revision:
- 12:623f562fc3db
- Parent:
- 11:261f1ba05668
- Child:
- 13:8db371de14a7
program test;
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 | 10:0457bb95d21e | 3 | #define SAMPLE_TIME 500 // 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 | 11:261f1ba05668 | 10 | float voltage_measure; |
| lyreliu | 0:d6ad48301389 | 11 | Serial pc(USBTX, USBRX); // Communicate with PC through the serial |
| lyreliu | 2:65d319447726 | 12 | |
| lyreliu | 2:65d319447726 | 13 | DigitalOut high_pin(p5); //test pin, p5 is the high level pin |
| lyreliu | 2:65d319447726 | 14 | DigitalOut low_pin(p6); //test pin, p6 is the low level pin |
| lyreliu | 0:d6ad48301389 | 15 | |
| lyreliu | 0:d6ad48301389 | 16 | float mass; // The mass of the object |
| lyreliu | 7:1f0ab6c43458 | 17 | unsigned char mass_trans_high; // A 8 bits mass used for transmitting |
| lyreliu | 7:1f0ab6c43458 | 18 | unsigned char mass_trans_low; |
| lyreliu | 0:d6ad48301389 | 19 | |
| lyreliu | 5:ff47bcaebc64 | 20 | unsigned short int test; |
| lyreliu | 5:ff47bcaebc64 | 21 | |
| lyreliu | 0:d6ad48301389 | 22 | int main() |
| lyreliu | 0:d6ad48301389 | 23 | { |
| lyreliu | 0:d6ad48301389 | 24 | SerialInit(); //Initial the serial |
| lyreliu | 2:65d319447726 | 25 | |
| lyreliu | 2:65d319447726 | 26 | high_pin = 1; |
| lyreliu | 2:65d319447726 | 27 | low_pin = 0; |
| lyreliu | 2:65d319447726 | 28 | |
| lyreliu | 5:ff47bcaebc64 | 29 | test = 0xff; |
| lyreliu | 5:ff47bcaebc64 | 30 | |
| lyreliu | 0:d6ad48301389 | 31 | while(1) |
| lyreliu | 0:d6ad48301389 | 32 | { |
| lyreliu | 11:261f1ba05668 | 33 | voltage_measure = voltage.read(); |
| lyreliu | 11:261f1ba05668 | 34 | MassCompute(voltage_measure); |
| lyreliu | 6:0d854548023b | 35 | SerialTransmit(); |
| lyreliu | 6:0d854548023b | 36 | //pc.printf("%d",test); |
| lyreliu | 4:8af87a432c12 | 37 | wait_ms(SAMPLE_TIME); //Wait for 100ms, this is the sample time. |
| lyreliu | 0:d6ad48301389 | 38 | } |
| lyreliu | 0:d6ad48301389 | 39 | } |
| lyreliu | 0:d6ad48301389 | 40 | |
| lyreliu | 0:d6ad48301389 | 41 | void MassCompute(float voltage) |
| lyreliu | 0:d6ad48301389 | 42 | { |
| lyreliu | 1:d9a739fe837e | 43 | //float ratio; //The amplified factor of the OA |
| lyreliu | 0:d6ad48301389 | 44 | float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3 |
| lyreliu | 0:d6ad48301389 | 45 | |
| lyreliu | 0:d6ad48301389 | 46 | vol_ref = 3.3; |
| lyreliu | 0:d6ad48301389 | 47 | |
| lyreliu | 1:d9a739fe837e | 48 | mass = 3000*vol_ref*voltage/RATIO + 0.3; //The compute function of mass from the voltage |
| lyreliu | 0:d6ad48301389 | 49 | } |
| lyreliu | 0:d6ad48301389 | 50 | |
| lyreliu | 0:d6ad48301389 | 51 | void SerialInit(void) |
| lyreliu | 0:d6ad48301389 | 52 | { |
| lyreliu | 0:d6ad48301389 | 53 | pc.baud(9600); //Set the baud rate |
| lyreliu | 0:d6ad48301389 | 54 | pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1 |
| lyreliu | 0:d6ad48301389 | 55 | } |
| lyreliu | 0:d6ad48301389 | 56 | |
| lyreliu | 0:d6ad48301389 | 57 | void SerialTransmit(void) |
| lyreliu | 0:d6ad48301389 | 58 | { |
| lyreliu | 12:623f562fc3db | 59 | unsigned char start,end,voltage_high,voltage_low,voltage_test; |
| lyreliu | 3:2566acb12c47 | 60 | start = 0xfe; |
| lyreliu | 3:2566acb12c47 | 61 | end = 0xff; |
| lyreliu | 11:261f1ba05668 | 62 | |
| lyreliu | 11:261f1ba05668 | 63 | |
| lyreliu | 0:d6ad48301389 | 64 | if(mass>3) |
| lyreliu | 0:d6ad48301389 | 65 | mass =3; |
| lyreliu | 10:0457bb95d21e | 66 | mass_trans_low = (unsigned char)(mass*10000); |
| lyreliu | 10:0457bb95d21e | 67 | mass_trans_high = (unsigned char)(mass*10000)>>8; |
| lyreliu | 12:623f562fc3db | 68 | voltage_test = (unsigned char)voltage_measure*10; |
| lyreliu | 9:f4bfd06b3ef1 | 69 | |
| lyreliu | 11:261f1ba05668 | 70 | voltage_high = (unsigned char)(voltage_measure*10000)>>8; |
| lyreliu | 11:261f1ba05668 | 71 | voltage_low = (unsigned char)(voltage_measure*10000); |
| lyreliu | 11:261f1ba05668 | 72 | |
| lyreliu | 10:0457bb95d21e | 73 | pc.printf("%c",start); |
| lyreliu | 11:261f1ba05668 | 74 | //pc.printf("%c",mass_trans_high); |
| lyreliu | 11:261f1ba05668 | 75 | //pc.printf("%c",mass_trans_low); |
| lyreliu | 12:623f562fc3db | 76 | pc.printf("%c",voltage_test); |
| lyreliu | 12:623f562fc3db | 77 | //pc.printf("%c",voltage_low); |
| lyreliu | 10:0457bb95d21e | 78 | pc.printf("%c",end); |
| lyreliu | 0:d6ad48301389 | 79 | } |