智能车学习(十一)——陀螺仪学习

一、学习说明

     感觉就是配置I2C通信,然后直接移植51代码。。。

 

二、代码分享:

1、头文件:

#ifndef I2C_GYRO_H_
#define I2C_GYRO_H_



/***********函数声明***********/

extern  void Gyro_Init(void);
extern  void Read_GY(void);

/*****************************/


/***********全局变量声明***********/

extern float Gyro_X ;
extern float Gyro_Y ;
extern float Gyro_Z ;

/*********************************/

#endif

 

2、源文件:

#include "include.h"
#include "common.h"
#include "gpio.h"
#include "I2C_GYRO.h"
#include "lptmr.h"


/***********管脚声明***********/

#define L3G_S_I2C_SDA       (PTE21)//(PTB3)//(PTE0) //(PTE22)//(PTC1)//                     
#define L3G_S_I2C_SCL       (PTE5)//(PTB2)//(PTE1)//(PTE20)//(PTC0)//

/*****************************/


//IO方向设置
#define L3G_I2C_SDA_IN()   DDRE21 = 0//输入模式
#define L3G_I2C_SDA_OUT()  DDRE21 = 1//输出模式


//IO操作函数     
#define L3G_I2C_SCL        PTE5_OUT //SCL
#define L3G_I2C_SDA        PTE21_OUT //SDA     
#define L3G_I2C_READ_SDA   PTE21_IN  //输入SDA 

/***********全局变量声明***********/
float Gyro_X = 0;
float Gyro_Y = 0;
float Gyro_Z = 0;
/********************************/



//**********L3G4200D内部寄存器地址*********
#define L3G_WHO_AM_I 0x0F
#define L3G_CTRL_REG1 0x20
#define L3G_CTRL_REG2 0x21
#define L3G_CTRL_REG3 0x22
#define L3G_CTRL_REG4 0x23
#define L3G_CTRL_REG5 0x24
#define L3G_REFERENCE 0x25
#define L3G_OUT_TEMP 0x26
#define L3G_STATUS_REG 0x27
#define L3G_OUT_X_L 0x28
#define L3G_OUT_X_H 0x29
#define L3G_OUT_Y_L 0x2A
#define L3G_OUT_Y_H 0x2B
#define L3G_OUT_Z_L 0x2C
#define L3G_OUT_Z_H 0x2D
#define L3G_FIFO_CTRL_REG 0x2E
#define L3G_FIFO_SRC_REG 0x2F
#define L3G_INT1_CFG 0x30
#define L3G_INT1_SRC 0x31
#define L3G_INT1_TSH_XH 0x32
#define L3G_INT1_TSH_XL 0x33
#define L3G_INT1_TSH_YH 0x34
#define L3G_INT1_TSH_YL 0x35
#define L3G_INT1_TSH_ZH 0x36
#define L3G_INT1_TSH_ZL 0x37
#define L3G_INT1_DURATION 0x38
//****************************************
#define    L3G_SlaveAddress   0xD2      //定义器件在IIC总线中的从地址,根据ALT  ADDRESS地址引脚不同修改
#define    L3G_SlaveAddfire   0x69      //定义器件在IIC总线中的从地址,根据ALT  ADDRESS地址引脚不同修改


//============================================================================
//函数名称:void L3G_I2C_Config(void)
//函数返回:无
//参数说明:无
//功能概要:配置I2C管脚
//============================================================================
void L3G_I2C_Config(void)
{
  //L3G_I2C_SDA_OUT();
  gpio_init(L3G_S_I2C_SDA,GPO,0);
  gpio_init(L3G_S_I2C_SCL,GPO,1);
  
  L3G_I2C_SCL=1;
  L3G_I2C_SDA=1;
}


//============================================================================
//函数名称:void L3G_I2C_Start(void)
//函数返回:无
//参数说明:无
//功能概要:I2C启动
//============================================================================
void L3G_I2C_Start(void)
{
  L3G_I2C_SDA_OUT();
  L3G_I2C_SDA=1;
  L3G_I2C_SCL=1;
  lptmr_delay_us(1); 
  L3G_I2C_SDA=0;      
  lptmr_delay_us(1); 
  L3G_I2C_SCL=0;   
}


//============================================================================
//函数名称:void L3G_I2C_Stop(void)
//函数返回:无
//参数说明:无
//功能概要:I2C停止
//============================================================================
void L3G_I2C_Stop(void)
{
  L3G_I2C_SDA_OUT();
  L3G_I2C_SDA=0;
  L3G_I2C_SCL=1;
  lptmr_delay_us(1);                        
  L3G_I2C_SDA=1;
  lptmr_delay_us(1);                                           
  L3G_I2C_SCL=0;
}


//============================================================================
//函数名称:void L3G_I2C_Mack(void)
//函数返回:无
//参数说明:无
//功能概要:
//============================================================================
void L3G_I2C_Mack(void)
{ 
  L3G_I2C_SDA_OUT();
  L3G_I2C_SDA=0;
  L3G_I2C_SCL=1;
  lptmr_delay_us(1); 
  L3G_I2C_SCL=0;
  L3G_I2C_SDA=1;
}



//============================================================================
//函数名称:void L3G_I2C_Mnack(void)
//函数返回:无
//参数说明:无
//功能概要:读取完的完成停顿信号
//============================================================================
void L3G_I2C_Mnack(void)
{
  L3G_I2C_SDA_OUT();
  L3G_I2C_SDA=1;    
  lptmr_delay_us(1); 
  L3G_I2C_SCL=1;
  lptmr_delay_us(1); 
  L3G_I2C_SCL=0;
  lptmr_delay_us(1);  
  //L3G_I2C_SDA=0;
}


//============================================================================
//函数名称:uint8_t L3G_I2C_Cack(void)
//函数返回:无
//参数说明:无
//功能概要:发送间隔函数
//============================================================================
uint8_t L3G_I2C_Cack(void)
{  
  L3G_I2C_SDA_IN();
  L3G_I2C_SCL=0;
  lptmr_delay_us(1); 
  L3G_I2C_SCL=1;
  lptmr_delay_us(1); 
  if(L3G_I2C_READ_SDA)
  {
    //L3G_I2C_Stop();
    //return 0xff;
  }
  L3G_I2C_SCL=0;              
  L3G_I2C_SDA_OUT();  
  return 0;  
}


//============================================================================
//函数名称:void L3G_I2C_WriteByte(unsigned char data)
//函数返回:无
//参数说明:无
//功能概要:I2C发送一个字节的数据
//============================================================================
void L3G_I2C_WriteByte(unsigned char data)
{
  unsigned char i=8;
  L3G_I2C_SDA_OUT();
  while(i--)
  {
    lptmr_delay_us(1); 
    if(data & 0x80)  
      L3G_I2C_SDA=1;
    else
      L3G_I2C_SDA=0;
    lptmr_delay_us(1); 
    L3G_I2C_SCL=1; 
    lptmr_delay_us(1); 
    L3G_I2C_SCL=0;         
    data<<=1;            
  }
}


//============================================================================
//函数名称:uint8_t L3G_I2C_ReadByte(void)
//函数返回:无
//参数说明:无
//功能概要:I2C接收一个字节的数据
//============================================================================
uint8_t L3G_I2C_ReadByte(void)
{
  unsigned char i;
  unsigned char data=0;
  char temp;                
  L3G_I2C_SDA_IN();                      
  for(i=0;i<8;i++)            
  {  
    data<<=1;
    lptmr_delay_us(1); 
    L3G_I2C_SCL=1; 
    temp=L3G_I2C_READ_SDA;
    if(temp)
      data++;
    lptmr_delay_us(1);   
    L3G_I2C_SCL=0;            
  }
  return data;
}


//============================================================================
//函数名称:uint8_t L3G_I2C_WR_REG(uint8_t dev_addr, uint8_t reg_addr, uint8_t writeData)
//函数返回:无
//参数说明:无
//功能概要:I2C向一个地址写一个数据
//============================================================================
uint8_t L3G_I2C_WR_REG(uint8_t dev_addr, uint8_t reg_addr, uint8_t writeData)
{
  L3G_I2C_Start();
  L3G_I2C_WriteByte(dev_addr);
  L3G_I2C_Cack();
  L3G_I2C_WriteByte(reg_addr);
  L3G_I2C_Cack();
  L3G_I2C_WriteByte(writeData);
  L3G_I2C_Cack();
  L3G_I2C_Stop();
  
  return 0;
}


//============================================================================
//函数名称:uint8_t L3G_I2C_RD_REG(uint8_t dev_addr, uint8_t reg_addr)
//函数返回:无
//参数说明:无
//功能概要:I2C从一个地址读一个数据
//============================================================================
uint8_t L3G_I2C_RD_REG(uint8_t dev_addr, uint8_t reg_addr)
{
  uint8_t read_reg = 0;
  L3G_I2C_Start();
  L3G_I2C_WriteByte(dev_addr);
  L3G_I2C_Cack();
  L3G_I2C_WriteByte(reg_addr);
  L3G_I2C_Cack();
  L3G_I2C_Start();
  L3G_I2C_WriteByte(dev_addr + 1);
  L3G_I2C_Cack();
  read_reg = L3G_I2C_ReadByte();
  L3G_I2C_Mnack();
  L3G_I2C_Stop();
  
  return read_reg;
}


//============================================================================
//函数名称:uint8_t L3G_I2C_RD_Buffer(uint8_t dev_addr, uint8_t reg_addr, uint8_t data_num, uint8_t *readBuffer)
//函数返回:无
//参数说明:无
//功能概要:I2C向一个地址写一连串数据
//============================================================================
uint8_t L3G_I2C_RD_Buffer(uint8_t dev_addr, uint8_t reg_addr, uint8_t data_num, uint8_t *readBuffer)
{
  uint8_t i = 0;
  L3G_I2C_Start();
  L3G_I2C_WriteByte(dev_addr);
  L3G_I2C_Cack();
  L3G_I2C_WriteByte(reg_addr);
  L3G_I2C_Cack();
  L3G_I2C_Start();
  L3G_I2C_WriteByte(dev_addr + 1);
  L3G_I2C_Cack();
  
  for(i = 0; i < data_num; i++){
    *(readBuffer+i) = L3G_I2C_ReadByte();
    if(i == data_num - 1)
      L3G_I2C_Mnack();
    else
      L3G_I2C_Mack();
  }
  L3G_I2C_Stop();
  
  return 0;
}



//============================================================================
//函数名称:void Gyro_Init( __RAMFUNC  void)
//函数返回:无
//参数说明:无
//功能概要://初始化为指定模式 
//============================================================================
void Gyro_Init(void)
{    
  L3G_I2C_Config();
  L3G_I2C_WR_REG(L3G_SlaveAddress,L3G_CTRL_REG1,0xcf);    //数据手册30  0xbf 400hz 20~110截止频率
  L3G_I2C_WR_REG(L3G_SlaveAddress,L3G_CTRL_REG2,0x20);//前两位00,后两位高通滤波器模式选择,后四位截止频率  0x29
  L3G_I2C_WR_REG(L3G_SlaveAddress,L3G_CTRL_REG3,0x00);     
  L3G_I2C_WR_REG(L3G_SlaveAddress,L3G_CTRL_REG4,0x30); 
  L3G_I2C_WR_REG(L3G_SlaveAddress,L3G_CTRL_REG5,0x00); 
}


//============================================================================
//函数名称:void Read_GY(void)
//函数返回:无
//参数说明:无
//功能概要:陀螺仪传感器测量值读取接口
//============================================================================

uint8_t tmpBuffer[6] = {0};

void Read_GY(void)
{
  //int16_t x_h = 0,x_l = 0,y_l = 0,y_h = 0,z_h = 0,z_l = 0;//Gyro_X = 0,Gyro_Y = 0,Gyro_Z = 0;
  //Gyro_Init();
  L3G_I2C_RD_Buffer(L3G_SlaveAddress,0xA8,6,tmpBuffer);
 
  /*
  Gyro_X = ((short)((tmpBuffer[1]<<8)|tmpBuffer[0]))>>2;// 14位精度
  
  Gyro_Y = ((short)((tmpBuffer[3]<<8)|tmpBuffer[2]))>>2;// 14位精度
  
  Gyro_Z = ((short)((tmpBuffer[5]<<8)|tmpBuffer[4]))>>2;// 14位精度
  */
  
  int temp;
  
  temp = ((short)((tmpBuffer[1]<<8)|tmpBuffer[0]))>>4;//2;// 14位精度  1g数值为1024(原本为4096,但是右移了(无效位2位)(有效位14位)两位)
  Gyro_X = (double)Gyro_X*0.5+(double)temp*0.5;
  
  temp = ((short)((tmpBuffer[3]<<8)|tmpBuffer[2]))>>4;//2;// 14位精度
  Gyro_Y = (double)Gyro_Y*0.5+(double)temp*0.5;
  
  temp = ((short)((tmpBuffer[5]<<8)|tmpBuffer[4]))>>4;//2;// 14位精度   
  Gyro_Z = (double)Gyro_Z*0.5+(double)temp*0.5;
}

你可能感兴趣的:(智能车学习(十一)——陀螺仪学习)