#include "PT32x007x.h"
#include "string.h"
#include "filter.h"
#include "gpio_i2c.h"
#define READ_ADDR 0xA7
#define DEVICE_ADDR 0xA6
u8 arry_write[8]={0x0B,0x08,0x08,0x80,0x00,0x00,0x00};
u8 arry_read[7]={0};
float anglex = 0.0 ;
float angley = 0.0 ;
float anglez = 0.0 ;
void Software_Delay(u32 val)//延时函数
{
while(val)
{
val--;
}
}
/*******************************************************************************
*Function: UART_GPIO_Config
*Description: 配置UART引脚
*Input: 无
*Return: 无
*Others:
该函数负责使能UART模块相关引脚
*******************************************************************************/
void UART_GPIO_Config(void)
{
/* 配置UART管脚的复用功能 */
GPIO_DigitalRemapConfig(AFIOD, GPIO_Pin_5, AFIO_AF_0,ENABLE); //PD5 TX0
GPIO_DigitalRemapConfig(AFIOD, GPIO_Pin_6, AFIO_AF_0,ENABLE); //PD6 RX0
}
/*******************************************************************************
*Function: UART_Mode_Config
*Description: 配置UART
*Input: 无
*Return: 无
*Others:
该函数负责初始化UART模块的工作及其工作方式
*******************************************************************************/
void UART_Mode_Config(void)
{
UART_InitTypeDef UART_InitStruct;
/*初始化UART0*/
UART_InitStruct.UART_BaudRate = 9600;
UART_InitStruct.UART_WordLengthAndParity=UART_WordLengthAndParity_8D;
UART_InitStruct.UART_StopBitLength=UART_StopBitLength_1;
UART_InitStruct.UART_ParityMode=UART_ParityMode_Even;
UART_InitStruct.UART_Receiver=UART_Receiver_Enable;
UART_InitStruct.UART_LoopbackMode=UART_LoopbackMode_Disable;
UART_Init(UART0, &UART_InitStruct);
/*开启UART0的收发功能*/
UART_Cmd(UART0, ENABLE);
}
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 配置USART为中断源 */
NVIC_InitStructure.NVIC_IRQChannel = UART0_IRQn;
/* 优先级*/
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
/* 使能中断 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* 初始化配置NVIC */
NVIC_Init(&NVIC_InitStructure);
UART_ITConfig(UART0, UART_IT_RXNEI, ENABLE); // 使能串口接收中断
//---------------------------------
// 使能串口
UART_Cmd(UART0, ENABLE);
}
/*******************************************************************************
*Function: UART_Driver
*Description: UART模块驱动函数
*Input: 无
*Return: 无
*Others:
*******************************************************************************/
void UART_Driver(void)
{
UART_GPIO_Config();
UART_Mode_Config();
//
NVIC_Configuration() ;//串口接受
}
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO_InitTypeDef类型的结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; //选择要控制的GPIO引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OutPP; //设置引脚模式为通用推挽输出
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up; //无偏置
GPIO_Init(GPIOC, &GPIO_InitStructure); //调用库函数,初始化GPIO
// /* 配置I2C管脚的复用功能 */
// GPIO_DigitalRemapConfig(AFIOB, GPIO_Pin_5, AFIO_AF_0,ENABLE); //PB5 I2C SDA
// GPIO_DigitalRemapConfig(AFIOB, GPIO_Pin_4, AFIO_AF_0,ENABLE); //PB4 I2C SCL
}
void I2C_Driver(void)
{
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_Acknowledge = I2C_Acknowledge_Disable;
I2C_InitStruct.I2C_Broadcast = I2C_Broadcast_Disable;
I2C_InitStruct.I2C_OwnAddress = 0x00;
I2C_InitStruct.I2C_Prescaler = 640-1;
I2C_Init(I2C,&I2C_InitStruct);
}
int main (void)
{
uint32_t time = 0 ;
uint16_t i=0;
UART_Driver();
I2C_GPIO_Config();
// I2C_Driver();
printf("start......rn");
adxl_i2c_write(0x31, 0x0B) ; //Software_Delay(100000) ;
adxl_i2c_write(0x2c, 0x08) ; //Software_Delay(100000) ;
adxl_i2c_write(0x2d, 0x08) ; //Software_Delay(100000) ;
adxl_i2c_write(0x2e, 0x80) ; //Software_Delay(100000) ;
adxl_i2c_write(0x1E, 0x00) ; //Software_Delay(100000) ;
adxl_i2c_write(0x1F, 0x00) ; //Software_Delay(100000) ;
adxl_i2c_write(0x20, 0x05) ; //Software_Delay(100000) ;
//////// I2C_EE_Write(0xc0, 0x38, 1);Software_Delay(100000) ;
printf("device id=0x%xn", adxl_i2c_read(0x00)) ;
//I2C_EE_Read(arry_read, 0x20, DEVICE_ADDR, 1);
sc7a20_filter_init() ; //初始化滤波
while(1)
{
time++;
if(time%5000==0)
{
ADXL345_ReadDates(&anglex, &angley, &anglez) ;
printf("x=%.2f y=%.2f z=%.2fn", anglex, angley , anglez) ;
}
// Software_Delay(100000) ;
}
}
#include "filter.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"
extern short x ;
extern short y ;
extern short z ;
typedef struct FilterChannelTag{
int16_t sample_l;
int16_t sample_h;
int16_t flag_l;
int16_t flag_h;
} FilterChannel;
typedef struct Silan_core_channel_s{
int16_t filter_param_l;
int16_t filter_param_h;
int16_t filter_threhold;
FilterChannel sl_channel[3];
} Silan_core_channel;
Silan_core_channel core_channel;
void sc7a20_filter_init(void)
{
int16_t j = 0;
core_channel.filter_param_l = 4;
core_channel.filter_param_h = 16;
core_channel.filter_threhold = 50; //4G scale: 25; 2G scale: 50
for(j = 0; j < 3; j++)
{
core_channel.sl_channel[j].sample_l = 0;
core_channel.sl_channel[j].sample_h = 0;
core_channel.sl_channel[j].flag_l = 0;
core_channel.sl_channel[j].flag_h = 0;
}
}
static int16_t filter_average(int16_t preAve, int16_t sample, int16_t Filter_num, int16_t * flag)
{
if (*flag == 0)
{
preAve = sample;
*flag = 1;
}
return preAve + (sample - preAve) / Filter_num;
}
static int16_t silan_filter_process(FilterChannel* fac, int16_t sample)
{
if(fac == NULL)
{
return 0;
}
fac->sample_l = filter_average(fac->sample_l, sample, core_channel.filter_param_l, &fac->flag_l);
fac->sample_h = filter_average(fac->sample_h, sample, core_channel.filter_param_h, &fac->flag_h);
if (abs(fac->sample_l - fac->sample_h) > core_channel.filter_threhold)
{
fac->sample_h = fac->sample_l;
}
return fac->sample_h;
}
void filter_base_dates(void)
{
x = silan_filter_process(&core_channel.sl_channel[0], x);
y = silan_filter_process(&core_channel.sl_channel[1], y);
z = silan_filter_process(&core_channel.sl_channel[2], z);
}
审核编辑 黄宇
-
通讯
+关注
关注
9文章
917浏览量
35183 -
IIC
+关注
关注
11文章
303浏览量
38639 -
源代码
+关注
关注
96文章
2947浏览量
67111 -
ADXL345
+关注
关注
14文章
73浏览量
34123
发布评论请先 登录
相关推荐
DLPC350怎么才能实现iic通讯?
源代码加密、源代码防泄漏c/c++与git服务器开发环境

基于STM32F103C8T6 ADXL345 加速度传感器的计步器设计指南和代码
基于STM32设计的姿态感应刹车灯

ADXL345 3 轴数字加速计: 小型、薄片式、低功耗移动设备应用的理想选择

hex可以转成源代码么
adxl345动态测值问题
使用SPI总线将ADXL345与PSoC64集成,无法工作怎么解决?
PT32L007F8P7K& QMA6100P IIC 通讯源代码分享

评论