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
- Committer:
- lyreliu
- Date:
- 2016-03-06
- Revision:
- 0:d6ad48301389
- Child:
- 1:d9a739fe837e
File content as of revision 0:d6ad48301389:
#include "mbed.h"
void MassCompute(float); // The function of computing mass from voltage.
void SerialInit(void); // The function fo serial initial
void SerialTransmit(void); // Transmit the data through the serial
AnalogIn voltage(p20); // AnalogIn is a class of anlog input, p20 is the pin you use for anlog input,
// You can change the pin from pin15 to pin20
Serial pc(USBTX, USBRX); // Communicate with PC through the serial
float mass; // The mass of the object
unsigned short int mass_trans; // A 8 bits mass used for transmitting
int main()
{
SerialInit(); //Initial the serial
while(1)
{
MassCompute(voltage.read());
SerialTransmit();
wait_ms(100); //Wait for 100ms, this is the sample time.
}
}
void MassCompute(float voltage)
{
float ratio; //The amplified factor of the OA
float vol_ref; //Reference voltage, the basic voltage of the MCU, the default number is 3.3
vol_ref = 3.3;
mass = 3000*vol_ref*voltage/ratio + 0.3; //The compute function of mass from the voltage
}
void SerialInit(void)
{
pc.baud(9600); //Set the baud rate
pc.format(8,SerialBase::None,1); //8bit number,no parity has been used,stop bits 1
}
void SerialTransmit(void)
{
if(mass>3)
mass =3;
mass_trans = mass*85;
pc.printf("%d",mass_trans);
}