#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......\r\n");
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%x\n", 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=%.2f\n", anglex, angley , anglez) ;
}
// Software_Delay(100000) ;
}
}
|