コード例 #1
0
ファイル: main.c プロジェクト: nhaberla/stm32f4
/**

  * @brief  
  * @param  None
  * @retval None
  */
void LIS302DL_Reset(void)
{
  uint8_t ctrl = 0;
  
  LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;  
  
  /* Set configuration of LIS302DL*/
  LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
  LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_Init(&LIS302DL_InitStruct);
    
  /* Set configuration of Internal High Pass Filter of LIS302DL*/
  LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
  LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

  /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
                                                             = 3/100 = 30ms */
  Delay(30);
  
  /* Configure Click Window */
  ctrl = 0xC0;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_CTRL_REG3_ADDR, 1);
}
コード例 #2
0
ファイル: main.c プロジェクト: Marcin1311/stm32f4-examples
void init(void) {
    initLeds();
    initTimer();
    initPWM();
    initSpi();
    LIS302DL_Init();
}
コード例 #3
0
ファイル: initialization.c プロジェクト: PUT-PTM/STMInvaders
void AccInit(){
	LIS302DL_InitTypeDef AccInitStr;
	AccInitStr.Axes_Enable = LIS302DL_XYZ_ENABLE;
	AccInitStr.Full_Scale = LIS302DL_FULLSCALE_2_3;
	AccInitStr.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	AccInitStr.Output_DataRate = LIS302DL_DATARATE_100;
	AccInitStr.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&AccInitStr);
}
コード例 #4
0
static void MemsConfigure1(void) {
  LIS302DL_InitTypeDef LIS302DL_InitStruct;

  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Power_Mode      = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Full_Scale      = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test       = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_InitStruct.Axes_Enable     = LIS302DL_XY_ENABLE;
  LIS302DL_Init(&LIS302DL_InitStruct);
}
コード例 #5
0
/**
 * @brief  configure the mems accelometer
 * @param  None
 * @retval None
 */
static void Mems_Config(void)
{ 
  uint8_t ctrl = 0;

  LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;

  /* Set configuration of LIS302DL*/
  LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
  LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_Init(&LIS302DL_InitStruct);

  /* Set configuration of Internal High Pass Filter of LIS302DL*/
  LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
  LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

  /* Configure Interrupt control register: enable Click interrupt on INT1 and
     INT2 on Z axis high event */
  ctrl = 0x3F;
  LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG3_ADDR, 1);

  /* Enable Interrupt generation on click on Z axis */
  ctrl = 0x50;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);

  /* Configure Click Threshold on X/Y axis (10 x 0.5g) */
  ctrl = 0xAA;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSY_X_REG_ADDR, 1);

  /* Configure Click Threshold on Z axis (10 x 0.5g) */
  ctrl = 0x0A;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSZ_REG_ADDR, 1);

  /* Enable interrupt on Y axis high event */
  ctrl = 0x4C;
  LIS302DL_Write(&ctrl, LIS302DL_FF_WU_CFG1_REG_ADDR, 1);

  /* Configure Time Limit */
  ctrl = 0x03;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_TIMELIMIT_REG_ADDR, 1);

  /* Configure Latency */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_LATENCY_REG_ADDR, 1);

  /* Configure Click Window */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_WINDOW_REG_ADDR, 1);

}
コード例 #6
0
ファイル: hw_config.cpp プロジェクト: darklukee/poziomica
void prvMEMS_Config(void)
{
	uint8_t ctrl = 0;
	uint32_t i=0;		//simple index for software delay

	LIS302DL_InitTypeDef  LIS302DL_InitStruct;
	LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;

	/* Set configuration of LIS302DL*/
	LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_400;
	LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
	LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
	LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&LIS302DL_InitStruct);

	/* Set configuration of Internal High Pass Filter of LIS302DL*/
	LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
	LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

	/* Required delay for the MEMS Accelerometer: Turn-on time = 3/Output data Rate
	                                                            = 3/100 = 30ms */
	for(i=0;i<0x1FFFF;i++);

	/* Configure Interrupt control register: enable Click interrupt1 */
	ctrl = 0x07;
	LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG3_ADDR, 1);

	/* Enable Interrupt generation on click/double click on Z axis */
	ctrl = 0x70;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);

	/* Configure Click Threshold on X/Y axis (10 x 0.5g) */
	ctrl = 0xAA;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSY_X_REG_ADDR, 1);

	/* Configure Click Threshold on Z axis (10 x 0.5g) */
	ctrl = 0x0A;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSZ_REG_ADDR, 1);

	/* Configure Time Limit */
	ctrl = 0x03;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_TIMELIMIT_REG_ADDR, 1);

	/* Configure Latency */
	ctrl = 0x7F;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_LATENCY_REG_ADDR, 1);

	/* Configure Click Window */
	ctrl = 0x7F;
	LIS302DL_Write(&ctrl, LIS302DL_CLICK_WINDOW_REG_ADDR, 1);
}
コード例 #7
0
void STM32::InitAccelero()
{
	//Accelerometer init
	LIS302DL_InitTypeDef inittype;
	inittype.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	inittype.Output_DataRate = LIS302DL_DATARATE_400;
	inittype.Axes_Enable = LIS302DL_XYZ_ENABLE;
	inittype.Full_Scale = LIS302DL_FULLSCALE_2_3;
	inittype.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&inittype);
}
コード例 #8
0
ファイル: main.c プロジェクト: nraynaud/DiscoveryF4_RTOS
void init_accelerometer(void) {
	LIS302DL_InitTypeDef LIS302DL_InitStruct;
	LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
	LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE
			| LIS302DL_Z_ENABLE;
	LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
	LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&LIS302DL_InitStruct);
	send_byte_to_LIS(0x4, LIS302DL_CTRL_REG3_ADDR);
}
コード例 #9
0
void initAccelerometer() {
    LIS302DL_InitTypeDef LIS302DL_InitStruct;
    LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
    LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
    LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
    LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
    LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
    LIS302DL_Init(&LIS302DL_InitStruct);

    for (uint64_t i = 0; i < 10000000; i++);


    //    LIS302DL_FilterConfigTypeDef LIS302DL_FilterStruct;
    //    LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
    //    LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
    //    LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
    //    LIS302DL_FilterConfig(&LIS302DL_FilterStruct);

    init_acc_fifo();


    //        LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;
    //        LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
    //        LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_XYZ_ENABLE;
    //        LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE;
    //        LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);


    uint8_t ctrl = 0x00;
    //        LIS302DL_Write(&ctrl, LIS302DL_FF_WU_CFG1_REG_ADDR, 1);
    ctrl = 0x04;
    LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG3_ADDR, 1);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource0);


    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
コード例 #10
0
/**
	* @brief Set up configuration for Accelerometer 
	* @param void
	*
	*/
	void set_Accelerometer_MEM(void)
	{
			LIS302DL_InitTypeDef LIS302DL_InitStruct;
			
		/* Set configuration for LIS302DL*/
			LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
		/* Set the sampling frequency as 100Hz*/
			LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
		/* Enable X, Y, Z axes*/
			LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
		/* Set the range to +/- 2.0g */
			LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
		/* Set the self-test*/
			LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
			LIS302DL_Init(&LIS302DL_InitStruct);

	}
コード例 #11
0
/* Function to initialize the LIS302DL. */
void acc_init(void) {
	LIS302DL_InitTypeDef LIS302DL_conf;
	LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;

	
	LIS302DL_conf.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	LIS302DL_conf.Output_DataRate = LIS302DL_DATARATE_100;
	LIS302DL_conf.Axes_Enable = LIS302DL_XYZ_ENABLE;
	LIS302DL_conf.Full_Scale = LIS302DL_FULLSCALE_2_3;
	LIS302DL_conf.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&LIS302DL_conf);

	LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
	LIS302DL_InterruptStruct.SingleClick_Axes=LIS302DL_CLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptStruct.DoubleClick_Axes=LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE;
	LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);
	
	interruptEnable();
}
コード例 #12
0
ファイル: main.c プロジェクト: MbedTinkerer/hc05_stm32f4
/**
*@brief This method is the one provided by STMicroelectronic with the discoverycard demo. It is used to configure the LIS302DL accelerometer embedded on the discovery card.
*/
void init_LIS302DL(){
/* MEMS configuration */
LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
LIS302DL_Init(&LIS302DL_InitStruct);

/* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
= 3/100 = 30ms */
Delay(30);

/* MEMS High Pass Filter configuration */
LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
LIS302DL_FilterConfig(&LIS302DL_FilterStruct);
}
コード例 #13
0
ファイル: accelerometer.c プロジェクト: young-g-cho/MPLAB
void initMems () {
	
	LIS302DL_InitTypeDef init;

	
	init.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;							/*Mode */
	init.Output_DataRate = LIS302DL_DATARATE_100 ;							/* OUT data rate 100 Hz*/
	init.Axes_Enable = LIS302DL_XYZ_ENABLE;											/* Axes enable */
	init.Full_Scale = LIS302DL_FULLSCALE_2_3;										/* Full scale */
	init.Self_Test = LIS302DL_SELFTEST_NORMAL;									/* self test */
	LIS302DL_Init(&init);
	
	LIS302DL_InterruptConfigTypeDef singleClick;
		
	singleClick.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;                      // Latch interrupt request into CLICK_SRC register
  singleClick.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;                   // Single Click Axes Interrupts 
  singleClick.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE;     // Double Click Axes Interrupts 
	
	LIS302DL_InterruptConfig(&singleClick);	
	
}
コード例 #14
0
ファイル: main.c プロジェクト: PUT-PTM/STMDestroyer2016
void LIS302DL_Init1()
{
        LIS302DL_InitTypeDef LIS302DL_InitStruct;

        /*Set configuration of LIS302DL*/

        LIS302DL_InitStruct.Power_Mode=LIS302DL_LOWPOWERMODE_ACTIVE;

        LIS302DL_InitStruct.Output_DataRate=LIS302DL_DATARATE_100;

        LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;

        LIS302DL_InitStruct.Full_Scale=LIS302DL_FULLSCALE_2_3;

        LIS302DL_InitStruct.Self_Test=LIS302DL_SELFTEST_NORMAL;

        LIS302DL_Init(&LIS302DL_InitStruct);

        //int i;
        //for (i=0;i<10000;i++);
}
コード例 #15
0
ファイル: accelerometer.c プロジェクト: aqsaahmed90/MicroP
/**
 * @brief Configures and initializes the accelerometer.
 *
 * @note 	This method configures and initializes the accelerometer. Power
 *			 	mode is set to active, data rate to 100, all axes are enabled,
 *				full scale is set to 2_3, and self test is set to normal.
 */
void ACCEL_Config(void){
	LIS302DL_InitTypeDef LIS302DL_InitStructure;
	LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;
	
	//These four parts are what make up the structure.
	//Defined in the driver.
	
	//Power_Mode can either be LIS302DL_LOWPOWERMODE_ACTIVE or 
	//LIS302DL_LOWPOWERMODE_POWERDOWN. Active seems to be the obvious choice
	LIS302DL_InitStructure.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	
	//Data rate is either 100Hz or 400Hz. As we're only polling at
	//25Hz, the slower rate is fine.
	LIS302DL_InitStructure.Output_DataRate = LIS302DL_DATARATE_100;
	//Enable all the axes. Obvious choice
	LIS302DL_InitStructure.Axes_Enable = LIS302DL_XYZ_ENABLE;
	//Either 2_3 or 9_2. 2_3 (0x00) seems to be a scale capable of +-2g. This
	//is appropriate because we are going to be measuring a stable tilt
	LIS302DL_InitStructure.Full_Scale = LIS302DL_FULLSCALE_2_3;
	//SELFTEST_NORMAL = 0x00. This means that the self test mode is off.
	//Self test applies a known force (electromagnetic) to the sensor
	//so that the user can ensure it's working properly. This should be off.
	LIS302DL_InitStructure.Self_Test = LIS302DL_SELFTEST_NORMAL;
	
	LIS302DL_Init(&LIS302DL_InitStructure);
	
	//Enable single taps on the Z axis
	LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
	//Disable double taps
	LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE;
	//Interrupt will latch to high until LIS302DL_CLICK_SRC_REG_ADDR is read.
	LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
	LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

	Accel_Interrupt_Config();
	Filter_Configuration(&Filter_Structure_X);
	Filter_Configuration(&Filter_Structure_Y);
	Filter_Configuration(&Filter_Structure_Z);
}
コード例 #16
0
ファイル: main.c プロジェクト: Pano89/stm32f4Discovery
/**
 * @brief	Configure MEMS operation
 * @param	None
 * @retval	None
 */
void MEMS_Config(void)
{
  LIS302DL_InitTypeDef  		LIS302DL_InitStruct;
  LIS302DL_FilterConfigTypeDef	LIS302DL_FilterStruct;
  
  /* Set configuration of LIS302DL*/
  LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE;
  LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_Init(&LIS302DL_InitStruct);

  LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
  LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
  LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_OFF;
  LIS302DL_FilterConfig(&LIS302DL_FilterStruct);

  LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 4);
                  
  XOffset = Buffer[0];
  YOffset = Buffer[2];
}
コード例 #17
0
ファイル: main.c プロジェクト: rabryan/stm32f4examples
/**
  * @brief  Execute the demo application.
  * @param  None
  * @retval None
  */
static void Demo_Exec(void)
{
  RCC_ClocksTypeDef RCC_Clocks;
  uint8_t togglecounter = 0x00;

  while(1)
  {
    DemoEnterCondition = 0x00;

    /* Reset UserButton_Pressed variable */
    UserButtonPressed = 0x00;

    /* Initialize LEDs to be managed by GPIO */
    STM_EVAL_LEDInit(LED4);
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED5);
    STM_EVAL_LEDInit(LED6);

    /* SysTick end of count event each 10ms */
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

    /* Turn OFF all LEDs */
    STM_EVAL_LEDOff(LED4);
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED5);
    STM_EVAL_LEDOff(LED6);

    /* Waiting User Button is pressed */
    while (UserButtonPressed == 0x00)
    {
      /* Toggle LED4 */
      STM_EVAL_LEDToggle(LED4);
      Delay(10);
      /* Toggle LED4 */
      STM_EVAL_LEDToggle(LED3);
      Delay(10);
      /* Toggle LED4 */
      STM_EVAL_LEDToggle(LED5);
      Delay(10);
      /* Toggle LED4 */
      STM_EVAL_LEDToggle(LED6);
      Delay(10);
      togglecounter ++;
      if (togglecounter == 0x10)
      {
        togglecounter = 0x00;
        while (togglecounter < 0x10)
        {
          STM_EVAL_LEDToggle(LED4);
          STM_EVAL_LEDToggle(LED3);
          STM_EVAL_LEDToggle(LED5);
          STM_EVAL_LEDToggle(LED6);
          Delay(10);
          togglecounter ++;
        }
       togglecounter = 0x00;
      }
    }

    /* Waiting User Button is Released */
    while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
    {}
    UserButtonPressed = 0x00;

    /* TIM4 channels configuration */
    TIM4_Config();

    /* Disable all Timer4 channels */
    TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE);
    TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE);
    TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE);
    TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE);

    /* MEMS configuration */
    LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
    LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
    LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
    LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
    LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
    LIS302DL_Init(&LIS302DL_InitStruct);

    /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate
    = 3/100 = 30ms */
    Delay(30);

    DemoEnterCondition = 0x01;
    /* MEMS High Pass Filter configuration */
    LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
    LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
    LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
    LIS302DL_FilterConfig(&LIS302DL_FilterStruct);

    LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
    X_Offset = Buffer[0];
    Y_Offset = Buffer[2];
    Z_Offset = Buffer[4];

    /* USB configuration */
    Demo_USBConfig();

    /* Waiting User Button is pressed */
    while (UserButtonPressed == 0x00)
    {}

    /* Waiting User Button is Released */
    while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
    {}

    /* Disable SPI1 used to drive the MEMS accelerometre */
    SPI_Cmd(LIS302DL_SPI, DISABLE);

    /* Disconnect the USB device */
    DCD_DevDisconnect(&USB_OTG_dev);
    USB_OTG_StopDevice(&USB_OTG_dev);
  }
}
コード例 #18
0
ファイル: main.c プロジェクト: Gabo991/PUT_PTM13_Miernik_mocy
int main(void)
{


	SystemInit();
	SystemCoreClockUpdate(); // inicjalizacja dystrybucji czasu procesora

	/* Enable the SPI periph */
	RCC_APB2PeriphClockCmd(LIS302DL_SPI_CLK, ENABLE);
	/* Enable SCK, MOSI and MISO GPIO clocks */
	RCC_AHB1PeriphClockCmd(LIS302DL_SPI_SCK_GPIO_CLK | LIS302DL_SPI_MISO_GPIO_CLK |
	LIS302DL_SPI_MOSI_GPIO_CLK, ENABLE);
	/* Enable CS GPIO clock */
	RCC_AHB1PeriphClockCmd(LIS302DL_SPI_CS_GPIO_CLK, ENABLE);
	/* Enable INT1 GPIO clock */
	RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT1_GPIO_CLK, ENABLE);
	/* Enable INT2 GPIO clock */
	RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT2_GPIO_CLK, ENABLE);

	GPIO_PinAFConfig(LIS302DL_SPI_SCK_GPIO_PORT, LIS302DL_SPI_SCK_SOURCE,
	LIS302DL_SPI_SCK_AF);
	GPIO_PinAFConfig(LIS302DL_SPI_MISO_GPIO_PORT, LIS302DL_SPI_MISO_SOURCE,
	LIS302DL_SPI_MISO_AF);
	GPIO_PinAFConfig(LIS302DL_SPI_MOSI_GPIO_PORT, LIS302DL_SPI_MOSI_SOURCE,
	LIS302DL_SPI_MOSI_AF);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	/* SPI SCK pin configuration */
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_SCK_PIN;
	GPIO_Init(LIS302DL_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
	/* SPI MOSI pin configuration */
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MOSI_PIN;
	GPIO_Init(LIS302DL_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
	/* SPI MISO pin configuration */
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MISO_PIN;
	GPIO_Init(LIS302DL_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

	/* SPI configuration -------------------------------------------------------*/
	SPI_InitTypeDef SPI_InitStructure;
	SPI_I2S_DeInit(LIS302DL_SPI);
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_Init(LIS302DL_SPI, &SPI_InitStructure);

	/* Enable SPI1 */
	SPI_Cmd(LIS302DL_SPI, ENABLE);

	/* Configure GPIO PIN for Lis Chip select */
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_CS_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(LIS302DL_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
	/* Deselect : Chip Select high */
	GPIO_SetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN);
	/* Configure GPIO PINs to detect Interrupts */
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT1_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(LIS302DL_SPI_INT1_GPIO_PORT, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT2_PIN;
	GPIO_Init(LIS302DL_SPI_INT2_GPIO_PORT, &GPIO_InitStructure);

	LIS302DL_InitTypeDef LIS302DL_InitStruct;
	uint8_t ctrl = 0x00;
	/* Set configuration of LIS302DL*/
	LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
	LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE |
	LIS302DL_Z_ENABLE;
	LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
	LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
	LIS302DL_Init(&LIS302DL_InitStruct);

	LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;
	/* Set configuration of Internal High Pass Filter of LIS302DL*/
	LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
	LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOA, ENABLE);

	/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOD, &GPIO_InitStructure);



  STM32F4_Discovery_LEDInit(LED3);
  STM32F4_Discovery_LEDInit(LED4);
  STM32F4_Discovery_LEDInit(LED5);
  STM32F4_Discovery_LEDInit(LED6);
  STM32F4_Discovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

  STM32F4_Discovery_LEDOn(LED3);   
  Delay(0xFFFF);
  STM32F4_Discovery_LEDOff(LED3);   

  USBD_Init(&USB_OTG_dev,
#ifdef USE_USB_OTG_HS
  USB_OTG_HS_CORE_ID,
#else
  USB_OTG_FS_CORE_ID,
#endif
  &USR_desc,
  &USBD_HID_cb,
  &USR_cb);


  //char button=0;
  while (1)
  {

	  Delay(0xFF);

	  if (OutBuffer[0]&1)  	STM32F4_Discovery_LEDOn(LED3);
		  	  else			STM32F4_Discovery_LEDOff(LED3);
	  if (OutBuffer[0]&2)  	STM32F4_Discovery_LEDOn(LED4);
		  	  else			STM32F4_Discovery_LEDOff(LED4);
	  if (OutBuffer[0]&4)  	STM32F4_Discovery_LEDOn(LED5);
		  	  else			STM32F4_Discovery_LEDOff(LED5);
	  if (OutBuffer[0]&8)  	STM32F4_Discovery_LEDOn(LED6);
		  	  else			STM32F4_Discovery_LEDOff(LED6);

	  //if ((STM32F4_Discovery_PBGetState(BUTTON_USER) == Bit_SET)&&(button==0))

	 // {
			//  int i=0;

			  Delay(0xFF);
			  InBuffer[0] = przyspieszenie_x;
			  InBuffer[1] = przyspieszenie_y;
			  USBD_HID_SendReport (&USB_OTG_dev, InBuffer, 64);
	//  }
	 // button = STM32F4_Discovery_PBGetState(BUTTON_USER);

	LIS302DL_Read(&przyspieszenie_x, LIS302DL_OUT_X_ADDR, 1);
		if(przyspieszenie_x>127)
		{
			przyspieszenie_x=przyspieszenie_x-1;
			przyspieszenie_x=(~przyspieszenie_x)&0xFF;
			przyspieszenie_x=-przyspieszenie_x;
		}

		LIS302DL_Read(&przyspieszenie_y, LIS302DL_OUT_Y_ADDR, 1);
		if(przyspieszenie_y>127)
		{
			przyspieszenie_y=przyspieszenie_y-1;
			przyspieszenie_y=(~przyspieszenie_y)&0xFF;
			przyspieszenie_y=-przyspieszenie_y;
		}

		/*LIS302DL_Read(&przyspieszenie_z, LIS302DL_OUT_Z_ADDR, 1);
		if(przyspieszenie_z>127)
		{
				przyspieszenie_z=przyspieszenie_z-1;
				przyspieszenie_z=(~przyspieszenie_z)&0xFF;
				przyspieszenie_z=-przyspieszenie_z;
		}*/
  }
}
コード例 #19
0
/**
  * @brief  Execute the demo application.
  * @param  None
  * @retval None
  */
static void Demo_Exec(void)
{
  RCC_ClocksTypeDef RCC_Clocks;
  
  /* 串口配置 */
  USART1_Config();
  NVIC_Config();
  
  /* Initialize LEDs to be managed by GPIO */
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
  
  //每次系统初始化后,都读取flash查看存储空间剩余情况
  flash_readIndex(IndexList,uniIndexLength, &IndexCount);
  
  while(1)
  {
    DemoEnterCondition = 0x00;
    
    /* Reset UserButton_Pressed variable */
    UserButtonPressed = 0x00;
//    SerialButtonPressed = 0x00;
    
    /* SysTick end of count event each 2.5ms */
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / TimeDev);
    
    

    /******************************************/
    /***空闲状态,清空flash或者发送数据选项****/
    /******************************************/
    /* Waiting User Button is pressed */
    while (UserButtonPressed == 0x00)
    {
      /********通过串口与上位机通信*******************/
      /* if Send serial data Button(PB11) is pressed */
      if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == Bit_SET)//不太灵敏
      {
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == Bit_SET);
        
        /*串口通信状态指示灯*/
        STM_EVAL_LEDOn(LED4);
        STM_EVAL_LEDOn(LED3);
        STM_EVAL_LEDOn(LED5);
        STM_EVAL_LEDOn(LED6);
        
        //读取flash中index区域
        flash_readIndex(IndexList,uniIndexLength, &IndexCount);
        
        
        //与上位机握手操作
        unsigned char* shakeHandSend = "ready? ";//最后一个字符必须是空格字符
        while (*shakeHandSend != ' ')
        {
          SendChar(*shakeHandSend);
          shakeHandSend++;
        }
        
        Delay(100);//等待1s确认接收自上位机的握手信号"ok!"
        if (serialRecv[0] == 'o'&&serialRecv[1] == 'k'&&serialRecv[2] == '!')
        {
          //与上位机握手成功,可发送数据
          serialFlag = 1;
        }
        else
        {
          //与上位机握手失败,无法发送数据
          serialFlag = 0;
          
          /*串口握手失败指示灯*/
          STM_EVAL_LEDOff(LED4);
          Delay(10);
          STM_EVAL_LEDOff(LED3);
          Delay(10);
          STM_EVAL_LEDOff(LED5);
          Delay(10);
          STM_EVAL_LEDOff(LED6);
          Delay(100);
        }
        
        
        if (1 == serialFlag)
        {
          //先发送IndexList数据给上位机,上位机选择需要的记录段(segment)反馈给下位机,下位机据此发送相应记录段给上位机
          uint8_t i;
          unsigned char sendData[4];
          
          SendString("index");
          SendCounter = 0;
          while(SendCounter < IndexCount)
          {
            uint32_to_uint8(sendData,IndexList[SendCounter]);
            i = 0;
            while(i < 4)
            {
              SendChar((unsigned char)sendData[i]);
              i+= 1 ;
            }
            SendCounter += 1;
          }
          
          
          /*开始发送segment数据给上位机*/
          
          while(serialRecv[0] == 'o');//等待确认接收自上位机的选择信号"0"——"6"
          Delay(2);//等待20ms后开始发送data,实现收发同步
          
          if (serialRecv[0] == '0'||serialRecv[0] == '1'||serialRecv[0] == '2'||serialRecv[0] == '3'||serialRecv[0] == '4'||serialRecv[0] == '5'||serialRecv[0] == '6')
          {
            tempCount = IndexList[(serialRecv[0]-48)*uniIndexLength+2];
            tempCount2 = 0;
            //读取flash user data area中对应数据
            while (tempCount > lowerComputerBufferNum)
            {
              
              flash_readData(SendData,lowerComputerBufferNum, tempCount2*lowerComputerBufferNum, IndexList[(serialRecv[0]-48)*uniIndexLength]);
              SendCounter = 0;
              while(SendCounter < lowerComputerBufferNum)
              {
                SendChar((unsigned char)SendData[SendCounter]);
                SendCounter += 1 ;
              }
              tempCount -= lowerComputerBufferNum;
              tempCount2 += 1;
              Delay(9);//延时9ms以便上位机有时间处理缓冲区(上位机缓冲区大小同下位机) 
            }
            
            if (tempCount != 0)
            {
               flash_readData(SendData,tempCount, tempCount2*lowerComputerBufferNum, IndexList[(serialRecv[0]-48)*uniIndexLength]);
               SendCounter = 0;
               while(SendCounter < tempCount)
               {
                  SendChar((unsigned char)SendData[SendCounter]);
                  SendCounter += 1 ;
               } 
            }
          }
          
          /*选择是否擦除对应segment和index*/
          //接收自上位机的删除信号 'Y' for 删除,'N' for 不删除
          if(serialRecv[1] == 'Y')
          {
             //uint32_t tempIndexList[uniIndexLength*7];
             flash_init_sector(IndexList[(serialRecv[0]-48)*uniIndexLength]);//擦除该index所对应flash user data area区域
             flash_init_sector(((uint32_t)0x08010000));//先擦除Index区域(sector 4)
             i = 0;
             uint8_t k = 0;
             while(i < IndexCount)
             {
               if (i != (serialRecv[0]-48)*uniIndexLength)
               {
                  IndexList[k] = IndexList[i];
                  IndexList[k+1] = IndexList[i+1];
                  IndexList[k+2] = IndexList[i+2];
                  k += uniIndexLength;
               }
               i += uniIndexLength;
             }
             flash_writeIndex(IndexList,IndexCount-uniIndexLength);//后重写该条index记录
             IndexCount -= 3;
          }
          
        }
      }
      
      /********************清空flash********************************/
      /* if Button(PB12) is pressed, flash user area will be erased*/
      if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == Bit_SET)
      {
        /*waiting Button(PB12) being released*/
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == Bit_SET);
        
        /* flash初始化 */
        flash_init();
        IndexCount = 0;
        /*灯全闪烁后全灭,指示flash中用户数据被擦除*/
         STM_EVAL_LEDToggle(LED4);
         STM_EVAL_LEDToggle(LED3);
         STM_EVAL_LEDToggle(LED5);
         STM_EVAL_LEDToggle(LED6);
         Delay(10);
         STM_EVAL_LEDOff(LED4);
         STM_EVAL_LEDOff(LED3);
         STM_EVAL_LEDOff(LED5);
         STM_EVAL_LEDOff(LED6);
         Delay(100);
      }
      
      /* Toggle LED4 */
      STM_EVAL_LEDToggle(LED6);
      Delay(50);
      //LED4 3 5用于二进制编码指示flash存储区占用个数
      if((IndexCount/uniIndexLength)&(0x01))//最低位
      { 
        STM_EVAL_LEDOn(LED5);
      }
      else
      {
         STM_EVAL_LEDOff(LED5);
      }
      if((IndexCount/uniIndexLength)&(0x02))
      { 
        STM_EVAL_LEDOn(LED3);
      }
      else
      {
         STM_EVAL_LEDOff(LED3);
      }
      if((IndexCount/uniIndexLength)&(0x04))//最高位
      { 
        STM_EVAL_LEDOn(LED4);
      }
      else
      {
         STM_EVAL_LEDOff(LED4);
      }
    }
    
    /* Waiting User Button is Released */
    while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
    {}
    
    
    
    /******************************************/
    /***退出空闲状态,采样状态配置初始化*******/
    /******************************************/
    STM_EVAL_LEDOn(LED4);
    STM_EVAL_LEDOff(LED4);
    Delay(10);
    STM_EVAL_LEDOn(LED3);
    STM_EVAL_LEDOff(LED3);
    Delay(10);
    STM_EVAL_LEDOn(LED5);
    STM_EVAL_LEDOff(LED5);
    Delay(10);
    STM_EVAL_LEDOn(LED6);
    STM_EVAL_LEDOff(LED6);
    Delay(10);
    
    
    
    
    /*各段记录相互独立的标志位均清零*/
    Counter = 0;//分组采用计数器复位
    DataWriteErrorFlag = 0;//flash写入错误标志位复位
    NoWrittenFlag = 0;//未写入标志位复位
    WritingSectorFlag = 0;//正在写入sector标志位复位
    totalDataNumber = 0;//各段记录数据个数计数器复位
    
    
    UserButtonPressed = 0x00;

    
    /* MEMS configuration */
    LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
    LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_400;//加速度传感器采用率400HZ
    LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
    LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;//18 mg/digit
    LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
    LIS302DL_Init(&LIS302DL_InitStruct);
    
    /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
    = 3/400 = 7.5ms */
    Delay(1);
    
    /* MEMS High Pass Filter configuration */
    LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
    LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
    LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
    LIS302DL_FilterConfig(&LIS302DL_FilterStruct);
    
    LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
    X_Offset = Buffer[0];
    Y_Offset = Buffer[2];
    Z_Offset = Buffer[4];
  
    
    
    /******************************************/
    /***********开始进入采样模式,写入flash****/
    /******************************************/
    DemoEnterCondition = 0x01;
    /* Waiting User Button is pressed */
    while (UserButtonPressed == 0x00) //利用采样间隙写入缓冲器内数据,提高效率
    {
      if((0 == Counter)&&(1 == NoWrittenFlag)&&(0 == DataWriteErrorFlag))
      {
        if (WritingSectorFlag == 0)
        {
          writingDataAddr = getFreeDataStartAddr();
          if (writingDataAddr == 0)
          {
            NoFreeSectors = 1;//无空闲sector块置位
            NoWrittenFlag = 0;
            
            /* 指示灯全亮,指示存储空间已满 */
            STM_EVAL_LEDOn(LED4);
            STM_EVAL_LEDOn(LED3);
            STM_EVAL_LEDOn(LED5);
            STM_EVAL_LEDOn(LED6);
            break;//退出采样模式
          }
          else
          {
            //一次完整flash写入
            flash_init_sector(writingDataAddr);//保险起见,先擦除该空闲sector块
            DataWriteErrorFlag = flash_writeData(Total_Buffer, Total_Buffer_Number,writingDataAddr);
            NoWrittenFlag = 0;//表示已写入(flash)
            WritingSectorFlag = 1;//正在写入sector标志位置位,表示启用该块sector
          }
        }
        else
        {
          //flash写入
          DataWriteErrorFlag = flash_writeData(Total_Buffer, Total_Buffer_Number,writingDataAddr);
          NoWrittenFlag = 0;//表示已写入(flash)
        }
      }
      if(DataWriteErrorFlag != 0)
      {
        flash_init_sector(writingDataAddr);//flash写入错误后,擦除相应存储空间,退出采样状态
        WritingSectorFlag = 0;//归还对该块存储空间的使用权
        
        /* 指示灯全亮后全灭,指示进入采样状态 */
        STM_EVAL_LEDOn(LED4);
        STM_EVAL_LEDOn(LED3);
        STM_EVAL_LEDOn(LED5);
        STM_EVAL_LEDOn(LED6);
        Delay(100);
        STM_EVAL_LEDOff(LED4);
        STM_EVAL_LEDOff(LED3);
        STM_EVAL_LEDOff(LED5);
        STM_EVAL_LEDOff(LED6);
        Delay(100);
        break;
      }
      
      /* 指示灯全灭,指示进入采样状态 */
      STM_EVAL_LEDOff(LED4);
      STM_EVAL_LEDOff(LED4);
      STM_EVAL_LEDOff(LED3);
      STM_EVAL_LEDOff(LED5);
      STM_EVAL_LEDOff(LED6);
    }
    
    /* Waiting User Button is Released */
    while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
    {}
    
   
    
    /******************************************/
    /***********退出采样模式,做善后处理*******/
    /******************************************/
    DemoEnterCondition = 0x00;//防止此时进入采样模式
    
    /*如果退出采样模式时尚有数据未写入flash,在此一并写入flash*/
    if(1 == NoWrittenFlag)  flash_writeData(Total_Buffer, Counter,writingDataAddr);
    
    if ((0 == NoFreeSectors)&&(1 == WritingSectorFlag))
    {
      /* 指示灯全灭后全亮,指示进入数据善后状态 */
      STM_EVAL_LEDOff(LED4);
      STM_EVAL_LEDOff(LED3);
      STM_EVAL_LEDOff(LED5);
      STM_EVAL_LEDOff(LED6);
      Delay(10);
      STM_EVAL_LEDOn(LED4);
      STM_EVAL_LEDOn(LED3);
      STM_EVAL_LEDOn(LED5);
      STM_EVAL_LEDOn(LED6);
      Delay(10);
      
      /*至此,一条“完整”记录写入flash完毕,需要在flash中建立关于这条记录的索引index*/    
      flash_readIndex(IndexList,uniIndexLength, &IndexCount);//从flash中读取原IndexList
      IndexList[IndexCount] = writingDataAddr;//写入最新的数据地址块首地址
      srand((int)time(0));
      IndexList[IndexCount+1] = rand()%100;//写入最新的记录名
      IndexList[IndexCount+2] = totalDataNumber;//写入最新的段记录总个数
      flash_init_sector(((uint32_t)0x08010000));//先擦除Index区域(sector 4)
      flash_writeIndex(IndexList,IndexCount+3);//后写入
      IndexCount += 3;
    }
         
    /* Disable SPI1 used to drive the MEMS accelerometre */
    SPI_Cmd(LIS302DL_SPI, DISABLE);  
  }
}
コード例 #20
0
ファイル: main.c プロジェクト: choupc/stm32_MEMS
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  USART3_Config();  //USART3 init
  uint8_t ctrl = 0;
  
  LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;  
  
  /* SysTick end of count event each 10ms */
  SysTick_Config(SystemCoreClock/ 100);
  
  /* Set configuration of LIS302DL*/
  LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
  LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_Init(&LIS302DL_InitStruct);
    
  /* Set configuration of Internal High Pass Filter of LIS302DL*/
  LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
  LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

  /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
                                                             = 3/100 = 30ms */
  Delay(30);
  
  /* Configure Interrupt control register: enable Click interrupt1 */
  ctrl = 0x07;
  LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG3_ADDR, 1);
  
  /* Enable Interrupt generation on click/double click on Z axis */
  ctrl = 0x70;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);
  
  /* Configure Click Threshold on X/Y axis (10 x 0.5g) */
  ctrl = 0xAA;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSY_X_REG_ADDR, 1);
  
  /* Configure Click Threshold on Z axis (10 x 0.5g) */
  ctrl = 0x0A;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSZ_REG_ADDR, 1);
  
  /* Configure Time Limit */
  ctrl = 0x03;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_TIMELIMIT_REG_ADDR, 1);
    
  /* Configure Latency */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_LATENCY_REG_ADDR, 1);
  
  /* Configure Click Window */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_WINDOW_REG_ADDR, 1);
  
  /* TIM configuration -------------------------------------------------------*/
  TIM_Config(); 
  LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
  XOffset = (int8_t)Buffer[0];
  YOffset = (int8_t)Buffer[2];
  ZOffset = (int8_t)Buffer[4];
  while(1)
  {

b=(float)((int8_t)Buffer[0]-XOffset)*(int32_t)LIS302DL_SENSITIVITY_2_3G/1000*180/3.14;  //把X軸裡面的值轉換成角度
if(b<0){
x_acc=-b;
temp1=(int8_t)x_acc;
temp2=(x_acc-temp1)*10000;  //把小數點部分乘上10000,以便程式印出(usart3的printf功能無法印出float)
if(temp2<0){temp2=-temp2;}
printf("\r\n X_Acc= -%d.%d \r\n", temp1,temp2);
}

else{
x_acc=b;
temp1=(int8_t)x_acc;
temp2=(x_acc-temp1)*10000;
if(temp2<0){temp2=-temp2;}
printf("\r\n X_Acc= %d.%d \r\n", temp1,temp2);
}
        Delay(100);   
  }
}
コード例 #21
0
ファイル: main (copy).c プロジェクト: Windbysea/stm32_MEMS
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  USART3_Config();
  uint8_t ctrl = 0;
  
  LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;  
  
  /* SysTick end of count event each 10ms */
  SysTick_Config(SystemCoreClock/ 100);
  
  /* Set configuration of LIS302DL*/
  LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
  LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
  LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
  LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
  LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
  LIS302DL_Init(&LIS302DL_InitStruct);
    
  /* Set configuration of Internal High Pass Filter of LIS302DL*/
  LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
  LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
  LIS302DL_InterruptConfig(&LIS302DL_InterruptStruct);

  /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
                                                             = 3/100 = 30ms */
  Delay(30);
  
  /* Configure Interrupt control register: enable Click interrupt1 */
  ctrl = 0x07;
  LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG3_ADDR, 1);
  
  /* Enable Interrupt generation on click/double click on Z axis */
  ctrl = 0x70;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);
  
  /* Configure Click Threshold on X/Y axis (10 x 0.5g) */
  ctrl = 0xAA;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSY_X_REG_ADDR, 1);
  
  /* Configure Click Threshold on Z axis (10 x 0.5g) */
  ctrl = 0x0A;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_THSZ_REG_ADDR, 1);
  
  /* Configure Time Limit */
  ctrl = 0x03;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_TIMELIMIT_REG_ADDR, 1);
    
  /* Configure Latency */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_LATENCY_REG_ADDR, 1);
  
  /* Configure Click Window */
  ctrl = 0x7F;
  LIS302DL_Write(&ctrl, LIS302DL_CLICK_WINDOW_REG_ADDR, 1);
  
  /* TIM configuration -------------------------------------------------------*/
  TIM_Config(); 
  LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
  XOffset = (int8_t)Buffer[0];
  YOffset = (int8_t)Buffer[2];
  ZOffset = (int8_t)Buffer[4];
  while(1)
  {
a=(int8_t)(Buffer[0]);
if(a<0){
a=-a;
printf("\r\n X = -%d \r\n", a);
}
else{
printf("\r\n X = %d \r\n", a);
}
//printf("\r\n Y =%d \r\n", (int8_t)Buffer[2]);
//printf("\r\n Z =%d \r\n", (int8_t)Buffer[4]);

/*
temp=XOffset/sqrt((YOffset*YOffset+ZOffset*ZOffset));
angle_x=atan(temp);
angle_x=((angle_x*180)/3.14);

temp1=(int16_t)angle_x;
temp2=(angle_x-temp1)*1000;

printf("\r\n Angle_X=%d.%d \r\n", temp1,temp2);//use sqrt & atan
//原子教你玩的方法*/

/*
angle_ax=(XOffset-1100)/64;
angle_ax=(angle_ax*1.2*180/3.14);

temp1=(int16_t)angle_ax;
temp2=(angle_ax-temp1)*1000;
if(temp1<0){
a=-temp1;
b=-temp2;
printf("\r\n Angle_ax=%d.%d \r\n",a,b);
}
//網路上的方法*/

b=(float)((int8_t)Buffer[0]-XOffset)*(int32_t)LIS302DL_SENSITIVITY_2_3G/1000*180/3.14;
if(b<0){
x_acc=-b;
temp1=(int8_t)x_acc;
temp2=(x_acc-temp1)*10000;
if(temp2<0){temp2=-temp2;}
printf("\r\n X_Acc= -%d.%d \r\n", temp1,temp2);
}

else{
x_acc=b;
temp1=(int8_t)x_acc;
temp2=(x_acc-temp1)*10000;
if(temp2<0){temp2=-temp2;}
printf("\r\n X_Acc= %d.%d \r\n", temp1,temp2);
}
//openbox
        Delay(100);   
  }
}
コード例 #22
0
ファイル: main.c プロジェクト: PUT-PTM/STMDoodle
int main(void)
{
	uint8_t acc_x, acc_y, acc_z;
	tickerSendData = 0;

	LIS302DL_InitTypeDef  LIS302DL_InitStruct;
	       /* Set configuration of LIS302DL*/
	       LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	       LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
	       LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
	       LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
	       LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
	       LIS302DL_Init(&LIS302DL_InitStruct);

	       GPIO_InitTypeDef GPIO_InitStructure;
	       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	       /* SPI SCK pin configuration */
	       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	       GPIO_Init(GPIOB, &GPIO_InitStructure);

	       /* SPI MOSI pin configuration */
	       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	       GPIO_Init(GPIOB, &GPIO_InitStructure);

	       /* SPI MISO pin configuration */
	       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
	       GPIO_Init(GPIOB, &GPIO_InitStructure);


	/* Set up the system clocks */
	SystemInit();

	/* Initialize USB, IO, SysTick, and all those other things you do in the morning */
	init();


	while (1)
	{
		LIS302DL_Read(&acc_x, LIS302DL_OUT_X_ADDR, 1);
		LIS302DL_Read(&acc_y, LIS302DL_OUT_Y_ADDR, 1);
		LIS302DL_Read(&acc_z, LIS302DL_OUT_Z_ADDR, 1);

		if ((tickerSendData) >= 50){

			tickerSendData=0;
			VCP_put_char(acc_x);

		}

		/* Blink the orange LED at 1Hz */
		if (500 == ticker)
		{
			GPIOD->BSRRH = GPIO_Pin_13;
		}
		else if (1000 == ticker)
		{
			ticker = 0;
			GPIOD->BSRRL = GPIO_Pin_13;

		}


		/* If there's data on the virtual serial port:
		 *  - Echo it back
		 *  - Turn the green LED on for 10ms
		 */
		uint8_t theByte;
		if (VCP_get_char(&theByte))
		{



			GPIOD->BSRRL = GPIO_Pin_12;
			downTicker = 10;
		}
		if (0 == downTicker)
		{
			GPIOD->BSRRH = GPIO_Pin_12;
		}
	}

	return 0;
}
コード例 #23
0
ファイル: main.c プロジェクト: ohohyeah/FLU_Handheld
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    //RCC_Config();
    //GPIO_Config();
    //RCC_MCO1Config(RCC_MCO1Source_PLLCLK,RCC_MCO1Div_1);
	uint8_t tmpreg;
	LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f4xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f4xx.c file
  */

  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

  //NVIC_SetPriority(SysTick_IRQn, -1);

    /* MEMS configuration and set int1/int2 pins*/
    LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
    LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
    LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
    LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
    LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
    LIS302DL_Init(&LIS302DL_InitStruct);
	//Set INT1/INT2 pins
	tmpreg = 0x40;
	LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG3_ADDR, 1);

	//Initialize the touchscreen
	TSC_Init();

  	/* Initialize LEDs and push-buttons mounted on STM324xG-EVAL board */
	STM_EVAL_LEDInit(LED1);
	STM_EVAL_LEDInit(LED2);
	//STM_EVAL_LEDInit(LED3);
	//STM_EVAL_LEDInit(LED4);

	/* Initialize the LCD */
  	STM324xG_LCD_Init();
  	//STM_EVAL_LEDOn(LED1);
  	LCD_Clear(Black);
  	LCD_SetTextColor(White);

  	LCD_LOG_SetHeader("STM32 Camera Demo");
  	LCD_LOG_SetFooter ("   Copyright (c) STMicroelectronics" );

  	/* ADC configuration */
  	ADC_Config();

  	/* Initializes the DCMI interface (I2C and GPIO) used to configure the camera */
  	//OV9655_HW_Init();
        OV9712_HW_Init();
  	/* Read the OV9655/OV2640 Manufacturer identifier */
  	//OV9655_ReadID(&OV9655_Camera_ID);
        OV9712_ReadID(&OV9712_Camera_ID);
        //while(1);  
  	if(OV9655_Camera_ID.PID  == 0x96)
  	{
    	Camera = OV9712_CAMERA;
    	sprintf((char*)buffer, "OV9655 Camera ID 0x%x", OV9655_Camera_ID.PID);
    	ValueMax = 2;
  	}
  	else if(OV9712_Camera_ID.PIDH  == 0x97)
  	{
    	Camera = OV9712_CAMERA;
    	sprintf((char*)buffer, "OV9712 Camera ID 0x%x", OV9712_Camera_ID.PIDH);
    	ValueMax = 2;
  	}
  	else
  	{
    	LCD_SetTextColor(LCD_COLOR_RED);
    	sprintf((char*)buffer, "OV Camera ID 0x%02x%02x", OV9655_Camera_ID.Version, OV9712_Camera_ID.PIDH);
    	LCD_DisplayStringLine(LINE(4), buffer);
    	while(1);  
  	}

  	LCD_SetTextColor(LCD_COLOR_YELLOW);
  	LCD_DisplayStringLine(LINE(4), (uint8_t*)buffer);
  	LCD_SetTextColor(LCD_COLOR_WHITE);

  	Delay(200);

  	/* Initialize demo */
  	ImageFormat = (ImageFormat_TypeDef)BMP_QVGA;

  	/* Configure the Camera module mounted on STM324xG-EVAL board */
  	Demo_LCD_Clear();
  	LCD_DisplayStringLine(LINE(4), "Camera Init..               ");
  	Camera_Config();

  	sprintf((char*)buffer, " Image selected: %s", ImageForematArray[ImageFormat]);
  	LCD_DisplayStringLine(LINE(4),(uint8_t*)buffer);

  	LCD_ClearLine(LINE(4));
  	Demo_LCD_Clear();
  
        
        

  	if(ImageFormat == BMP_QQVGA)
  	{
    	/* LCD Display window */
    	LCD_SetDisplayWindow(60, 80, 160, 120);
    	ReverseLCD();
    	LCD_WriteRAM_Prepare(); 
  	}
  	else if(ImageFormat == BMP_QVGA)
  	{
    	/* LCD Display window */
    	LCD_SetDisplayWindow(0, 0, 320, 240);
    	ReverseLCD();
    	LCD_WriteRAM_Prepare(); 
  	}

	{
		int i, j;
		for (j = 0; j < 16; j++)
		{
			LCD_SetDisplayWindow(0, (240 / 16) * j, 320, (240 / 16));
			LCD_WriteRAM_Prepare();
			for (i = 0; i <	320 * 240 / 16; i++)
			{
				LCD_WriteRAM(0x0003 << j);
			}
		}
	}

  	/* Enable DMA2 stream 1 and DCMI interface then start image capture */
  	DMA_Cmd(DMA2_Stream1, ENABLE); 
  	DCMI_Cmd(ENABLE); 

  	/* Insert 100ms delay: wait 100ms */
  	//Delay(200); 

  	DCMI_CaptureCmd(ENABLE); 

  	while(1)
  	{
	      OV9655_BrightnessConfig(0x7F);//

		//static int step = 0;
		int i, block, begin;
		unsigned short *p;

		if (!bShot)
		{
			begin = (datablock - 11 + 48) % 48;
	
			for (block = begin; block < begin + 11; block++)
			{
				p = (unsigned short *)(0x20000000 + (320 * 240 * 2 / 16) * ((block) % 12));
				LCD_SetDisplayWindow(0, (block % 16) * (240 / 16), 320, (240 / 16));
				//LCD_SetCursor(0, (block % 16) * (240 / 16));
				LCD_WriteRAM_Prepare();
				for (i = 0; i <	320 * 240 / 16; i++)
				{
					LCD_WriteRAM(*p++);
				}
			}
		}
		if (TSC_TouchDet())
		{
			int x, y;
			TP_GetAdXY(&x, &y);
			if (x > 300 && x < 2800 && y > 300 && y < 2800)
			{
				if (x < 1550 && y < 1550)
				{
					uint32_t StartSector = 0, EndSector = 0, Address = 0, SectorCounter = 0;
					int m;
					//拍照bShot
					bShot = 1;
					while(datablock % 16);
					DMA_Cmd(DMA2_Stream1, DISABLE); 
					DCMI_Cmd(DISABLE);
					//STM_EVAL_LEDOn(LED2); 
					DMA_ClearFlag(DMA2_Stream1, 0x2F7D0F7D);
					DMA_ClearFlag(DMA2_Stream1, 0x0F7D0F7D);
					DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
					datablock = 0;
					DMA2_Stream1->M0AR = 0x20000000 + 9600 * (datablock % 12);
					DMA_SetCurrDataCounter(DMA2_Stream1, (320 * 240 * 2 / 4) / 16);

					DCMI_Cmd(ENABLE);
					DMA_Cmd(DMA2_Stream1, ENABLE);
					
					{
						unsigned long *p, *q;
						while(datablock < 4);	//等待准备好前4块数据,就将这4块数据导入到0x10000000+0x50000之后。
						q = (unsigned long *)(0x20000000);
						p = (unsigned long *)(0x10000000 + 0x5000);
						while (q < (unsigned long *)(0x20000000 + 4 * (320 * 240 * 2 / 16)))
						{
							*p = *q;
							p++;
							q++;
						}
					}


					while(datablock < 16);		//等待全部采集完成。

					STM_EVAL_LEDOn(LED2);		//LED2亮表示采集完成。
					LCD_SetDisplayWindow(0, 0, 320, 240);
					LCD_WriteRAM_Prepare();
					LCD_Clear(Black);
					//RAM位置
					/*
						序号  地址                大小
						1:    0x10005000+9600*0     9600
						2:    0x10005000+9600*1     9600
						3:    0x10005000+9600*2     9600
						4:    0x10005000+9600*3     9600
						5:    0x20000000+9600*5     9600
						6:    0x20000000+9600*6     9600
						7:    0x20000000+9600*7     9600
						8:    0x20000000+9600*8     9600
						9:    0x20000000+9600*9     9600
						10:   0x20000000+9600*10    9600
						11:   0x20000000+9600*11    9600
						12:   0x20000000+9600*0     9600
						13:   0x20000000+9600*1     9600
						14:   0x20000000+9600*2     9600
						15:   0x20000000+9600*3     9600					
					*/
				 	for (m = 0; m < 16; m++)	//显示保存的图片
					{
						unsigned short *q;
						if (m < 4)
						{
							q = (unsigned short *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16));
						}
						else
						{
							q = (unsigned short *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16));
						}
						LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16));
						LCD_WriteRAM_Prepare();
						for (i = 0; i <	320 * 240 / 16; i++)
						{
							LCD_WriteRAM(*q++);
						}
					}

					/* Erase the user Flash area ***********/
					FLASH_Unlock();
					
					/* Clear pending flags (if any) */  
					FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | 
						FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 
					
					/* Get the number of the start and end sectors */
					StartSector = GetSector(FLASH_USER_START_ADDR);
					EndSector = GetSector(FLASH_USER_END_ADDR);
					
					for (SectorCounter = StartSector; SectorCounter < EndSector; SectorCounter += 8)
					{
						/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
						be done by word */ 
						if (FLASH_EraseSector(SectorCounter, VoltageRange_3) != FLASH_COMPLETE)
						{ 
							/* Error occurred while sector erase. 
							User can add here some code to deal with this error  */
							while (1)
							{
							}
						}
					}
										
				 	for (m = 0; m < 16; m++)	//保存图片到flash
					{
						unsigned long *q;
						Address = FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16);
						if (m < 4)
						{
							q = (unsigned long *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16));
						}
						else
						{
							q = (unsigned long *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16));
						}
						while (Address < FLASH_USER_START_ADDR + (m + 1) *(320 * 240 * 2 / 16))
						{
							if (FLASH_ProgramWord(Address, *q) == FLASH_COMPLETE)
							{
								Address = Address + 4;
								q++;
							}
							else
							{ 
								/* Error occurred while writing data in Flash memory. 
								User can add here some code to deal with this error */
								while (1)
								{
								}
							}
						}
					}

					STM_EVAL_LEDOff(LED2);
					LCD_SetDisplayWindow(0, 0, 320, 240);
					LCD_WriteRAM_Prepare();
					LCD_Clear(Black);
				 	for (m = 0; m < 16; m++)	//显示flash中的图片
					{
						unsigned short *q;
						q = (unsigned short *)(FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16));
						LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16));
						LCD_WriteRAM_Prepare();
						for (i = 0; i <	320 * 240 / 16; i++)
						{
							LCD_WriteRAM(*q++);
						}
					}
					/* Lock the Flash to disable the flash control register access (recommended
					 to protect the FLASH memory against possible unwanted operation) *********/
					FLASH_Lock(); 
				}
				else if (x >= 1550 && y < 1550)
				{
					//righttop
					STM_EVAL_LEDOff(LED1);
					STM_EVAL_LEDOff(LED2);
					bShot = 0;
					datablock = 0;
					DMA_Cmd(DMA2_Stream1, ENABLE);
					DCMI_Cmd(ENABLE); 
				}
				else if (x < 1550 && y >= 1550)
				{
					//righttop
					//DMA_Cmd(DMA2_Stream1, ENABLE);
					//DCMI_Cmd(ENABLE); 
				}
				else if (x >= 1550 && y >= 1550)
				{
					//righttop
					//DMA_Cmd(DMA2_Stream1, ENABLE);
					//DCMI_Cmd(ENABLE); 
				}
			}
		}
		//Delay(10);
  	}	
}