Ejemplo n.º 1
0
void LIS302D_ChangePowerControl(SPI_TypeDef* spi, bool enableActiveMode) {
	uint8_t tmpreg;
	
	// Read the CTRL_REG1, enable/disable the PD memory flag and write it back
	LIS302DL_Read(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
	tmpreg &= (uint8_t)~LIS302DL_BIT1; // Unset the FS bit
	if (enableActiveMode) { // If the bit is set, we are in active mode, else in power down control
		tmpreg |= (uint8_t)LIS302DL_BIT1;
	}
	LIS302DL_Write(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
Ejemplo n.º 2
0
void LIS302DL_ChangeScaleMode(SPI_TypeDef* spi, bool fullScaleEnable) {
	uint8_t tmpreg;
	
	// Read the CTRL_REG1, enable/disable the FS memory flag and write it back
	LIS302DL_Read(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
	tmpreg &= (uint8_t)~LIS302DL_BIT2; // Unset the FS bit
	if (!fullScaleEnable) { // If the bit is not set, we are in ±2.3g mode (full scale)
		tmpreg |= (uint8_t)LIS302DL_BIT2;
	}
	LIS302DL_Write(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
Ejemplo n.º 3
0
void LIS302D_ChangeDataRate(SPI_TypeDef* spi, bool enableHighSpeed) {
	uint8_t tmpreg;
	
	// Read the CTRL_REG1, enable/disable the DR memory flag and write it back
	LIS302DL_Read(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
	tmpreg &= (uint8_t)~LIS302DL_BIT0; // Unset the FS bit
	if (!enableHighSpeed) { // If the bit is set, we are on 400Hz, otherwise on 100Hz
		tmpreg |= (uint8_t)LIS302DL_BIT0;
	}
	LIS302DL_Write(spi, &tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
/**
 * @brief  Reboot memory content of LIS302DL
 * @param  None
 * @retval None
 */
void LIS302DL_RebootCmd(void) {
    uint8_t tmpreg;
    /* Read CTRL_REG2 register */
    LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG2_ADDR, 1);

    /* Enable or Disable the reboot memory */
    tmpreg |= LIS302DL_BOOT_REBOOTMEMORY;

    /* Write value to MEMS CTRL_REG2 regsister */
    LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG2_ADDR, 1);
}
Ejemplo n.º 5
0
void LIS302DL_InterruptConfig(LIS302DL_InterruptConfigTypeDef *LIS302DL_IntConfigStruct)
{
uint8_t ctrl = 0x00;
/* Read CLICK_CFG register */
LIS302DL_Read(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);
/* Configure latch Interrupt request, click interrupts and double click interrupts */
ctrl = (uint8_t)(LIS302DL_IntConfigStruct->Latch_Request| \
LIS302DL_IntConfigStruct->SingleClick_Axes | \
LIS302DL_IntConfigStruct->DoubleClick_Axes);
/* Write value to MEMS CLICK_CFG register */
LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);
}
Ejemplo n.º 6
0
void LIS302DL_Init(LIS302DL_InitTypeDef *LIS302DL_InitStruct)
{
	uint8_t ctrl = 0x00;
	/* Configure the low level interface ---------------------------------------*/
	// LIS302DL_LowLevel_Init();
	/* Configure MEMS: data rate, power mode, full scale, self test and axes */
	ctrl = (uint8_t) (LIS302DL_InitStruct->Output_DataRate | LIS302DL_InitStruct->Power_Mode | \
	LIS302DL_InitStruct->Full_Scale | LIS302DL_InitStruct->Self_Test
	| \
	LIS302DL_InitStruct->Axes_Enable);
	/* Write value to MEMS CTRL_REG1 regsister */
	LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG1_ADDR, 1);
}
/**
 * @brief  Change the lowpower mode for LIS302DL
 * @param  LowPowerMode: new state for the lowpower mode.
 *   This parameter can be one of the following values:
 *     @arg LIS302DL_LOWPOWERMODE_POWERDOWN: Power down mode
 *     @arg LIS302DL_LOWPOWERMODE_ACTIVE: Active mode  
 * @retval None
 */
void LIS302DL_LowpowerCmd(uint8_t LowPowerMode) {
    uint8_t tmpreg;

    /* Read CTRL_REG1 register */
    LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);

    /* Set new low power mode configuration */
    tmpreg &= (uint8_t) ~LIS302DL_LOWPOWERMODE_ACTIVE;
    tmpreg |= LowPowerMode;

    /* Write value to MEMS CTRL_REG1 regsister */
    LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
/**
 * @brief  Data Rate command 
 * @param  DataRateValue: Data rate value
 *   This parameter can be one of the following values:
 *     @arg LIS302DL_DATARATE_100: 100 Hz output data rate 
 *     @arg LIS302DL_DATARATE_400: 400 Hz output data rate    
 * @retval None
 */
void LIS302DL_DataRateCmd(uint8_t DataRateValue) {
    uint8_t tmpreg;

    /* Read CTRL_REG1 register */
    LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);

    /* Set new Data rate configuration */
    tmpreg &= (uint8_t) ~LIS302DL_DATARATE_400;
    tmpreg |= DataRateValue;

    /* Write value to MEMS CTRL_REG1 regsister */
    LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
Ejemplo n.º 9
0
void LIS302DL_ClickInterruptConfiguration(SPI_TypeDef* spi, LIS302DL_ClickInterruptConfig* pConfig) {
	// Map the configuration to be always BigEndian
	uint8_t cnf = 0x00;
	cnf |= pConfig->Latched ? (uint8_t)LIS302DL_BIT1 : 0x00;
	cnf |= pConfig->DoubleClick_Z ? (uint8_t)LIS302DL_BIT2 : 0x00;
	cnf |= pConfig->SingleClick_Z ? (uint8_t)LIS302DL_BIT3 : 0x00;
	cnf |= pConfig->DoubleClick_Y ? (uint8_t)LIS302DL_BIT4 : 0x00;
	cnf |= pConfig->SingleClick_Y ? (uint8_t)LIS302DL_BIT5 : 0x00;
	cnf |= pConfig->DoubleClick_X ? (uint8_t)LIS302DL_BIT6 : 0x00;
	cnf |= pConfig->SingleClick_X ? (uint8_t)LIS302DL_BIT7 : 0x00;
	
	LIS302DL_Write(&cnf, LIS302DL_CLICK_CFG_REG_ADDR, 1);
}
/**
 * @brief  Change the Full Scale of LIS302DL
 * @param  FS_value: new full scale value. 
 *   This parameter can be one of the following values:
 *     @arg LIS302DL_FULLSCALE_2_3: +-2.3g
 *     @arg LIS302DL_FULLSCALE_9_2: +-9.2g   
 * @retval None
 */
void LIS302DL_FullScaleCmd(uint8_t FS_value) {
    uint8_t tmpreg;

    /* Read CTRL_REG1 register */
    LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);

    /* Set new full scale configuration */
    tmpreg &= (uint8_t) ~LIS302DL_FULLSCALE_9_2;
    tmpreg |= FS_value;

    /* Write value to MEMS CTRL_REG1 regsister */
    LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1);
}
Ejemplo n.º 11
0
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);
}
Ejemplo n.º 12
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);
}
Ejemplo n.º 13
0
void LIS302DL_Init(SPI_TypeDef* spi, const LIS302DL_Config* pConfig) {
	// Map the configuration to be always BigEndian
	uint8_t cnf = 0x00;
	cnf |= pConfig->DataRate ? (uint8_t)LIS302DL_BIT0 : 0x00;
	cnf |= pConfig->PowerDown ? (uint8_t)LIS302DL_BIT1 : 0x00;
	cnf |= pConfig->FullScale ? (uint8_t)LIS302DL_BIT2 : 0x00;
	cnf |= pConfig->SelfTest_P ? (uint8_t)LIS302DL_BIT3 : 0x00;
	cnf |= pConfig->SelfTest_M ? (uint8_t)LIS302DL_BIT4 : 0x00;
	cnf |= pConfig->ZAxisEnabled ? (uint8_t)LIS302DL_BIT5 : 0x00;
	cnf |= pConfig->YAxisEnabled ? (uint8_t)LIS302DL_BIT6 : 0x00;
	cnf |= pConfig->XAxisEnabled ? (uint8_t)LIS302DL_BIT7 : 0x00;
	
	LIS302DL_Write(&cnf, LIS302DL_CTRL_REG1_ADDR, 1);
}
Ejemplo n.º 14
0
void LIS302DL_WakeupInterruptConfiguration(SPI_TypeDef* spi, LIS302DL_WakeupInterruptConfig* pConfig, uint8_t intNum) {
	// Map the configuration to be always BigEndian
	uint8_t cnf = 0x00;
	cnf |= pConfig->CombineInterrupts ? (uint8_t)LIS302DL_BIT0 : 0x00;
	cnf |= pConfig->Latched ? (uint8_t)LIS302DL_BIT1 : 0x00;
	cnf |= pConfig->Z_High ? (uint8_t)LIS302DL_BIT2 : 0x00;
	cnf |= pConfig->Z_Low ? (uint8_t)LIS302DL_BIT3 : 0x00;
	cnf |= pConfig->Y_High ? (uint8_t)LIS302DL_BIT4 : 0x00;
	cnf |= pConfig->Y_Low ? (uint8_t)LIS302DL_BIT5 : 0x00;
	cnf |= pConfig->X_High ? (uint8_t)LIS302DL_BIT6 : 0x00;
	cnf |= pConfig->X_Low ? (uint8_t)LIS302DL_BIT7 : 0x00;
	
	LIS302DL_Write(&cnf, ((intNum & LIS302DL_INTERRUPT_NUM_2) ? LIS302DL_FF_WU_CFG2_REG_ADDR : LIS302DL_FF_WU_CFG1_REG_ADDR), 1);
}
Ejemplo n.º 15
0
void LIS302DL_GlobalInterruptConfiguration(SPI_TypeDef* spi, LIS302DL_GlobalInterruptConfig* pConfig) {
	// Get the current configuration
	uint8_t cnf = 0x00;
	LIS302DL_Read(&cnf, LIS302DL_CTRL_REG2_ADDR, 1);
	
	// Unset the bits we want configure
	cnf &= (uint8_t)~(LIS302DL_BIT3 | LIS302DL_BIT4 | LIS302DL_BIT5 | LIS302DL_BIT6 | LIS302DL_BIT7);
	
	// Map the configuration to be always BigEndian
	cnf |= pConfig->SendData ? (uint8_t)LIS302DL_BIT3 : 0x00;
	cnf |= pConfig->Interrupt_1 ? (uint8_t)LIS302DL_BIT4 : 0x00;
	cnf |= pConfig->Interrupt_2 ? (uint8_t)LIS302DL_BIT5 : 0x00;
	cnf |= (pConfig->CutOffFrequency <= (uint8_t)LIS302DL_HIGHPASS_CUTOFF_SLOW) ? pConfig->CutOffFrequency : 0x00;
	
	LIS302DL_Write(&cnf, LIS302DL_CTRL_REG2_ADDR, 1);
}
/**
 * @brief  Set LIS302DL Internal High Pass Filter configuration.
 * @param  LIS302DL_Filter_ConfigTypeDef: pointer to a LIS302DL_FilterConfig_TypeDef 
 *         structure that contains the configuration setting for the LIS302DL Filter.
 * @retval None
 */
void LIS302DL_FilterConfig(LIS302DL_FilterConfigTypeDef *LIS302DL_FilterConfigStruct) {
    uint8_t ctrl = 0x00;

    /* Read CTRL_REG2 register */
    LIS302DL_Read(&ctrl, LIS302DL_CTRL_REG2_ADDR, 1);

    /* Clear high pass filter cut-off level, interrupt and data selection bits*/
    ctrl &= (uint8_t) ~(LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER | \
                     LIS302DL_HIGHPASSFILTER_LEVEL_3 | \
                     LIS302DL_HIGHPASSFILTERINTERRUPT_1_2);
    /* Configure MEMS high pass filter cut-off level, interrupt and data selection bits */
    ctrl |= (uint8_t) (LIS302DL_FilterConfigStruct->HighPassFilter_Data_Selection | \
                    LIS302DL_FilterConfigStruct->HighPassFilter_CutOff_Frequency | \
                    LIS302DL_FilterConfigStruct->HighPassFilter_Interrupt);

    /* Write value to MEMS CTRL_REG2 register */
    LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG2_ADDR, 1);
}
Ejemplo n.º 17
0
void send_byte_to_LIS(uint8_t byte, uint8_t address) {
	LIS302DL_Write(&byte, address, 1);
}
Ejemplo n.º 18
0
void LIS302DL_Init() {
    GPIO_SetBits(GPIOE, GPIO_Pin_3);
    u8 reg = 0x47;
    LIS302DL_Write(0x20, &reg, 1);
    GPIO_ResetBits(GPIOE, GPIO_Pin_3);
}
Ejemplo n.º 19
0
/**
  * @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);   
  }
}
Ejemplo n.º 20
0
/**
  * @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);
  	}	
}
Ejemplo n.º 21
0
/**
  * @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);   
  }
}