SHENG-HEN HSIEH / Mbed 2 deprecated VL53L0X_STM32compatible

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "VL53L0X_SH.h"
00003 
00004 #define Rms 5000            //TT rate
00005 #define dt  0.005f
00006 #define NN  200
00007 
00008 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
00009 
00010 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓GPIO registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00011 //~~~structure~~~//
00012 DigitalOut  led(D13);           //detection
00013 DigitalOut  TT_ext(D12);
00014 
00015 //~~~VL53L0X_I2C~~~//
00016 //I2C         i2c(D14, D15);      //I2C reg(SDA, SCL)
00017 VL53L0X sensor;
00018 //~~~Serial~~~//
00019 Serial      pc(D1, D0);         //Serial reg(TX RX)
00020 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of GPIO registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00021 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Varible registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00022 //~~~globle~~~//
00023 Ticker  TT;                     //call a timer
00024 int     count = 0;              //one second counter for extrenal led blink
00025 
00026 //~~~VL53L0X_I2C~~~//
00027 int     Distance = 0;
00028 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Varible registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00029 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Function registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00030 void    init_TIMER();           //set TT_main() rate
00031 void    TT_main();              //timebase function rated by TT
00032 void    init_IO();              //initialize IO state
00033 float   lpf(float input, float output_old, float frequency);    //lpf discrete
00034 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Function registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00035 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓main funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00036 int main()
00037 {
00038     init_IO();                  //initialized value
00039     sensor.init();              //init SENSOR
00040     sensor.setTimeout(500);
00041     sensor.startContinuous();
00042     NVIC_SetPriority(TIM5_IRQn, 51);    //!!!!!!!!!!!!!!!!!!!!!!!!!
00043     init_TIMER();               //start TT_main
00044 
00045     while(1) {                  //main() loop
00046         if(count >= NN) {       //check if main working
00047             count=0;
00048             led = !led;
00049         }
00050     }
00051 
00052 }
00053 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of main funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00054 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Timebase funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00055 void init_TIMER()                   //set TT_main{} rate
00056 {
00057     TT.attach_us(&TT_main, Rms);
00058 }
00059 void TT_main()                      //interrupt function by TT
00060 {
00061     TT_ext = !TT_ext;               //indicate TT_main() function working
00062     count = count+1;                //one second counter
00063     Distance = sensor.readRangeContinuousMillimeters();
00064 
00065 //for Serial-Oscilloscope
00066     pc.printf("%d\r", Distance);
00067 }
00068 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Timebase funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00069 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓init_IO funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00070 void init_IO(void)                  //initialize
00071 {
00072     pc.baud(9600);            //set baud rate
00073     TT_ext = 0;
00074     led = 0;
00075 }
00076 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of init_IO funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00077 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓lpf funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00078 float lpf(float input, float output_old, float frequency)
00079 {
00080     float output = 0;
00081     output = (output_old + frequency*dt*input) / (1 + frequency*dt);
00082     return output;
00083 }
00084 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of lpf funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//