lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

main.cpp

Committer:
lyreliu
Date:
2016-03-06
Revision:
1:d9a739fe837e
Parent:
0:d6ad48301389
Child:
2:65d319447726

File content as of revision 1:d9a739fe837e:

#include "mbed.h"
#define RATIO 1                      //The amplified factor of the OA  ??????????????????????????????????????????????????
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);
}