ADXL345 与 PT32L007 IIC通讯源代码分享

电子说

1.2w人已加入

描述

#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);
}

ADXL345


审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分