山东电子小菜鸟的个人空间 https://bbs.21ic.com/?1197168 [收藏] [复制] [RSS]

日志

stm8-spi-lis3dh

已有 1565 次阅读2017-8-23 23:21 |个人分类:stm8|系统分类:单片机



/* Includes ------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "math.h"
#include "stm8s.h"
#include "sysclock.h"
#include "uart1.h"
#include "spi_flash.h"
#include "lis3dh_driver.h"
#include "led.h"
/* Private macro -------------------------------------------------------------*/

#define  acchold    2000

/*void Init_Timer4(void)
{
       
        TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0XFF);

  TIM4_ClearFlag(TIM4_FLAG_UPDATE);

        TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
        TIM4_Cmd(ENABLE);
}*/

/* Private variables ---------------------------------------------------------*/

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void lis3dhinit(void)
{
  //u8 response=0;
//response =
LIS3DH_SetODR(LIS3DH_ODR_10Hz);
//printf("设置 ODR:%d\r\n",response); 
//response = 
LIS3DH_SetMode(LIS3DH_NORMAL);
//printf("设置 MODE:%d\r\n",response); 
//response = 
LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);
//printf("设置 SCALE:%d\r\n",response); 
//response = 
LIS3DH_SetAxis(LIS3DH_X_ENABLE| LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);
//printf("设置 ENABLE:%d\r\n",response);  
LIS3DH_SetTemperature(MEMS_ENABLE);

}
void Delay(u32 nCount);


void main(void)
{
  AxesRaw_t data;
  
 // u16 num;
  u8 lisid;
 // i16_t temp[2]={0};
  i16_t old_xdata=0;
  unsigned int minute_xdata=0;
    CLK_HSICmd(ENABLE);//开始内部高频RC
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);//8分频 2M
        /*设置外部24M晶振为主时钟*/ 
        //SystemClock_Init(HSE_Clock);
        //        SPI_FLASH_Init();        /* SPI初始化*/
        SPI_FLASH_Init();        /* SPI初始化*/
        Uart1_Init();
   // Init_Timer4();
        //enableInterrupts();
         //lis3dhinit();
        LED_Init();
     //  LIS3DH_GetWHO_AM_I(&lisid);    
      // printf("传感器ID:%x\r\n",lisid); 
        
    while (1)
        {
         // printf("传感器ID:%x\r\n",0X01); 
        static u8 statis=0;
         statis++;
         if(statis==200)
         {
           statis=0;
         }
         if(statis%30==0)
         {
                  lis3dhinit();        
                 // statis=
                    LIS3DH_GetWHO_AM_I(&lisid);       
                printf("传感器ID:%x\r\n",lisid);          
            // statis=
               LIS3DH_GetAccAxesRaw(&data); 
               if((unsigned int)(data.AXIS_X)>(unsigned int)(old_xdata)){minute_xdata=data.AXIS_X-old_xdata;}
               else{minute_xdata=old_xdata-data.AXIS_X;}
               old_xdata=data.AXIS_X;
             printf("LIS3DH_GetAccAxesRaw(&data):%d\r\n",minute_xdata);    
           // printf("x=%8d,y=%8d,z=%8d\r\n",(int)(data.AXIS_X),data.AXIS_Y,data.AXIS_Z); 
        }
       /*if((statis%10)==0)
        {  static u8 num=0;
          num++;
          if(num==1)
          LED_Operation(LED_1,ON); 
          if(num==1+minute_xdata%200){
            LED_Operation(LED_1,OFF); num=0;}
        }*/
     
       // if((statis%25)==0)  LED_Operation(LED_1,OFF);;
      
     //LIS3DH_SetTemperature(MEMS_ENABLE);
          //LIS3DH_GetTempRaw(&temp[0]);{
  //printf("传感温度:%8x",temp[0]); 
      Delay(650) ;     
 //  
// Delay(650) ; 

     }
       
}

void Delay(u32 nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}



#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(u8* file, u32 line)
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif




路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)