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.
Fork of mbed_blinky by
main.cpp
00001 #include<mbed.h> 00002 00003 uint16_t x_a,y_a,z_a; 00004 bool flag = 0; 00005 00006 SPI spi_master(P0_6,P0_5,P0_7); //mosi miso sclk 00007 DigitalOut cs(P0_4); 00008 00009 //Serial pc(P0_23,P0_25); 00010 00011 Serial pc(P0_9,P0_11); 00012 00013 uint8_t LIS3DH_SPI_RD(uint8_t addr) 00014 { 00015 uint8_t temp; 00016 cs = 0; 00017 wait_us(10); 00018 spi_master.write(addr); 00019 temp=spi_master.write(0xff); 00020 wait_us(10); 00021 cs = 1; 00022 return temp; 00023 } 00024 00025 void LIS3DH_SPI_WR(uint8_t addr,uint8_t wrdata) 00026 { 00027 cs = 0; 00028 wait_us(10); 00029 spi_master.write(addr); 00030 spi_master.write(wrdata); 00031 wait_us(10); 00032 cs = 1; 00033 } 00034 00035 void SPI_LIS3DH_Init() 00036 { 00037 spi_master.format(8,3); 00038 spi_master.frequency(100000); 00039 wait_ms(5); 00040 LIS3DH_SPI_WR(0x24,0x80); 00041 wait_ms(5); 00042 LIS3DH_SPI_WR(0x20,0x17); 00043 LIS3DH_SPI_WR(0x23,0x80); 00044 } 00045 00046 void get_val(void) 00047 { 00048 uint8_t Dx_L=1,Dy_L=1,Dz_L=1; 00049 uint8_t Dx_H=1,Dy_H=1,Dz_H=1; 00050 if(LIS3DH_SPI_RD(0x0f|0x80)==0x33) 00051 { 00052 printf("check device ok!\r\n"); 00053 flag=1; 00054 Dx_H=LIS3DH_SPI_RD(0x29|0x80); 00055 Dx_L=LIS3DH_SPI_RD(0x28|0x80); 00056 Dy_H=LIS3DH_SPI_RD(0x2b|0x80); 00057 Dy_L=LIS3DH_SPI_RD(0x2A|0x80); 00058 Dz_H=LIS3DH_SPI_RD(0x2d|0x80); 00059 Dz_L=LIS3DH_SPI_RD(0x2C|0x80); 00060 } 00061 else 00062 { 00063 printf("check device err!\r\n"); 00064 wait(1); 00065 } 00066 x_a=Dx_H<<8|Dx_L/16; 00067 y_a=Dy_H<<8|Dy_L/16; 00068 z_a=Dz_H<<8|Dz_L/16; 00069 } 00070 00071 int main(void) 00072 { 00073 SPI_LIS3DH_Init(); 00074 while(1) 00075 { 00076 get_val(); 00077 if(flag) 00078 { 00079 printf("Dx=:%d\r\n",x_a); 00080 printf("Dy=:%d\r\n",y_a); 00081 printf("Dz=:%d\r\n",z_a); 00082 flag=0; 00083 wait(1); 00084 } 00085 } 00086 }
Generated on Tue Jul 12 2022 20:45:15 by
1.7.2
