本帖最后由 DavidZH 于 2019-10-7 18:58 编辑
原理解析: 通过读取LIS2DW12不同位置时,XYZ轴的值,然后进行判断,比如X轴翻转后,进行开关,Y轴翻转后进行亮度的调节,亮度根据翻转的周数来控制;
1、初始化串口和LIS2DW12;
//Uart_Init***************************************************************************//
PSOC6_UART_Init();
//Uart_Init***************************************************************************//
PSOC6_I2C_Init();
__enable_irq(); /* Enable global interrupts. */
lis2dw12_block_data_update_set(LIS2DW12_I2C_ADD_H, PROPERTY_ENABLE);
lis2dw12_full_scale_set(LIS2DW12_I2C_ADD_H, LIS2DW12_8g);
lis2dw12_filter_path_set(LIS2DW12_I2C_ADD_H, LIS2DW12_LPF_ON_OUT);
lis2dw12_filter_bandwidth_set(LIS2DW12_I2C_ADD_H, LIS2DW12_ODR_DIV_4);
lis2dw12_power_mode_set(LIS2DW12_I2C_ADD_H, LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit);
lis2dw12_data_rate_set(LIS2DW12_I2C_ADD_H, LIS2DW12_XL_ODR_25Hz);
2、读取LIS2DW12 XYZ的值:
lis2dw12_status_reg_get(LIS2DW12_I2C_ADD_H, ®.status);
if(reg.status.drdy)
{
/* Read acceleration data */
memset(data_raw_acceleration.u8bit, 0x00, 3*sizeof(int16_t));
lis2dw12_acceleration_raw_get(LIS2DW12_I2C_ADD_H, data_raw_acceleration.u8bit);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_X_L, &Acc_XL, 1);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_X_H, &Acc_XH ,1);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_Y_L, &Acc_YL, 1);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_Y_H, &Acc_YH, 1);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_Z_L, &Acc_ZL, 1);
lis2dw12_read_reg(LIS2DW12_I2C_ADD_H, LIS2DW12_OUT_Z_H, &Acc_ZH, 1);
data_raw_acceleration.i16bit[0] = ((uint16_t)Acc_XH << 8) |((uint16_t)Acc_XL);
data_raw_acceleration.i16bit[1] = ((uint16_t)Acc_YH <<8 ) |((uint16_t)Acc_YL);
data_raw_acceleration.i16bit[2] = ((uint16_t)Acc_ZH <<8 ) |((uint16_t)Acc_ZL);
acceleration_mg[0] = LIS2DW12_FROM_FS_8g_TO_mg( data_raw_acceleration.i16bit[0]);
acceleration_mg[1] = LIS2DW12_FROM_FS_8g_TO_mg( data_raw_acceleration.i16bit[1]);
acceleration_mg[2] = LIS2DW12_FROM_FS_8g_TO_mg( data_raw_acceleration.i16bit[2]);
}
3、基于三轴加速度翻转算法简单演示:
if(cnt0 ==0)
{
acc_date[0] = data_raw_acceleration.i16bit[0];
acc_date[1] = data_raw_acceleration.i16bit[1];
acc_date[2] = data_raw_acceleration.i16bit[2];
}else{
cnt0 = 2;
if(acc_date[0] != data_raw_acceleration.i16bit[0])
{
if(acc_date[0] > data_raw_acceleration.i16bit[0])
{
Position= acc_date[0] - data_raw_acceleration.i16bit[0];
if(Position > 0x1000)
{
acc_date[0] = data_raw_acceleration.i16bit[0];
Cy_GPIO_Write(Red_0_PORT,Red_0_NUM,1);
Cy_GPIO_Write(Green_0_PORT,Green_0_NUM,1);
Cy_GPIO_Write(Blue_0_PORT,Blue_0_NUM,1);
}
}else{
Position= data_raw_acceleration.i16bit[0] -acc_date[0];
if(Position > 0x1000)
{
acc_date[0] = data_raw_acceleration.i16bit[0];
Cy_GPIO_Write(Red_0_PORT,Red_0_NUM,0);
Cy_GPIO_Write(Green_0_PORT,Green_0_NUM,0);
Cy_GPIO_Write(Blue_0_PORT,Blue_0_NUM,0);
}
}
}
}
CyDelay(500);
动图演示:
代码:
PSOC6_IKS01A3.zip
(4.23 MB, 下载次数: 17)
此内容由EEWORLD论坛网友DavidZH原创,如需转载或用于商业用途需征得作者同意并注明出处