lyre liu / Mbed 2 deprecated project1

Dependencies:   mbed

main.cpp

Committer:
lyreliu
Date:
2016-03-06
Revision:
4:8af87a432c12
Parent:
3:2566acb12c47
Child:
5:ff47bcaebc64

File content as of revision 4:8af87a432c12:

#include "mbed.h"
#define RATIO 1                      //The amplified factor of the OA  ??????????????????????????????????????????????????
#define SAMPLE_TIME 100             // Sample time, default is 100ms
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

DigitalOut high_pin(p5);            //test pin, p5 is the high level pin
DigitalOut low_pin(p6);             //test pin, p6 is the low level pin
                                    
float mass;                         // The mass of the object
unsigned short int mass_trans_high; // A 8 bits mass used for transmitting
unsigned short int mass_trans_low;

int main()
{
    SerialInit();                   //Initial the serial
    
    high_pin = 1;
    low_pin = 0;
    
    while(1)
    {
        MassCompute(voltage.read());  
        SerialTransmit();
        wait_ms(SAMPLE_TIME);               //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)
{   
    unsigned int start,end;
    start = 0xfe;
    end   = 0xff;
    if(mass>3)
        mass =3;
    mass_trans_low = (int)(mass*10000);
    mass_trans_high = (int)(mass*10000)>>8;
    pc.printf("%d",start);
    pc.printf("%d",mass_trans_high);
    pc.printf("%d",mass_trans_low);
    pc.printf("%d",end);
}