SHENG-HEN HSIEH
/
LSM9DS0_STM32compatible
works fine on STM
Fork of Sample_manerine_SPI_LSM9DS0 by
Diff: main.cpp
- Revision:
- 1:b42c3522a50a
- Parent:
- 0:d68e088dfbcd
- Child:
- 2:0d90c0436797
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Sep 01 16:37:11 2016 +0000 @@ -0,0 +1,129 @@ +#include "mbed.h" +#include "LSM9DS0_SH.h" +#define pi 3.141592 +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓GPIO registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +//~~~structure~~~// +DigitalOut led(D13); //detection +DigitalOut TT_ext(D12); + +//~~~SPI~~~// +DigitalOut SPI_CSG(D7,1); //low for GYRO enable +DigitalOut SPI_CSXM(D6,1); //low for ACC/MAG enable +SPI spi(D4, D5, D3); //MOSI MISO SCLK + +//~~~Serial~~~// +Serial pc(D1, D0); //Serial reg(TX RX) +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of GPIO registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Varible registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +//~~~globle~~~// +Ticker TT; //call a timer +int count = 0; //one second counter for extrenal led blink +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Varible registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Function registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +void init_IO(); //initialize IO state +void init_IMU(); //initialize IMU +void init_TIMER(); //set TT_main() rate +void TT_main(); //timebase function rated by TT +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Function registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓main funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +int main() +{ + init_IO(); //initialized value + init_IMU(); //initialize IMU + init_TIMER(); //start TT_main + pc.baud(115200); //set baud rate + + while(1) { //main() loop + if(count >= 200) { //check if main working + count=0; + led = !led; + } + } + +} +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of main funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓init_IO funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +void init_IO(void) //initialize +{ + TT_ext = 0; + led = 1; +} +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of init_IO funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓init_IMU funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +void init_IMU(void) //initialize +{ + SPI_CSXM = 1; //high as init for disable SPI + SPI_CSG = 1; + spi.format(8, 0); //byte width, spi mode + spi.frequency(4000000); //8MHz + +//for GYRO config + SPI_CSG = 0; //start spi talking + spi.write(CTRL_REG1_G); + spi.write(0x9F); //data rate 380 Hz/ cut off 25 Hz + SPI_CSG = 1; //end spi talking + + SPI_CSG = 0; //start spi talking + spi.write(CTRL_REG4_G); + spi.write(0x10); //Scle 500dps + SPI_CSG = 1; //end spi talking + +//for ACC config + SPI_CSXM = 0; //start spi talking + spi.write(CTRL_REG1_XM); + spi.write(0x87); //data rate 400 Hz/ Enable + SPI_CSXM = 1; //end spi talking + + SPI_CSXM = 0; //start spi talking + spi.write(CTRL_REG2_XM); + spi.write(0xC0); //cut off 50 Hz/ Scale +-2g + SPI_CSXM = 1; //end spi talking + +} +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of init_IMU funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Timebase funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// +void init_TIMER() //set TT_main{} rate +{ + TT.attach_us(&TT_main, 5000); +} +void TT_main() //interrupt function by TT +{ + TT_ext = !TT_ext; //indicate TT_main() function working + count = count+1; //one second counter + + SPI_CSG = 0; //start spi talking + spi.write(0x8f); //read WHO_AM_I_G 0x0f | B1000 0000 + int temp = spi.write(0); + SPI_CSG = 1; //end spi talking + + + SPI_CSXM = 0; //start spi talking + spi.write(0x8f); //read WHO_AM_I_XM 0x0f | B1000 0000 + int temp2 = spi.write(0); + SPI_CSXM = 1; //end spi talking + + pc.printf("who_am_I_G:%d\t", temp); + pc.printf("who_am_I_XM:%d\n", temp2); +} +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Timebase funtion↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// \ No newline at end of file