金牌会员
- 积分
- 1390
- 威望
- 676
- 贡献
- 390
- 兑换币
- 411
- 注册时间
- 2013-10-7
- 在线时间
- 162 小时
- 毕业学校
- 湖北工业大学
|
- #ifndef __DEV_L3G4200_H__
- #define __DEV_L3G4200_H__
- #include "common.h"
- #include "HW_I2C.h"
- /********用户可修改值 开始***********/
- //连接L3G4200用到的I2C通道
- #define L3G4200_I2CX (I2C1)
- //连接L3G4200用到的SCL引脚
- #define L3G4200_SCLPIN (PTC10)
- //连接L3G4200用到的SDA引脚
- #define L3G4200_SDAPIN (PTC11)
- /********用户可修改值 结束***********/
- //定义SCL Bus Speed取值,外设总线为50Mhz时的计算结果
- #define L3G4200_SCL_50KHZ (0x33)
- #define L3G4200_SCL_100KHZ (0x2B)
- #define L3G4200_SCL_150KHZ (0x28)
- #define L3G4200_SCL_200KHZ (0x23)
- #define L3G4200_SCL_250KHZ (0x21)
- #define L3G4200_SCL_300KHZ (0x20)
- #define L3G4200_SCL_400KHZ (0x17)
- //=========MMA8451 功能参数==================//
- #define L3G4200_DEV_ADDR 0x69 //定义器件地址,可根据硬件修改
- #define L3G4200_DEV_WRITE 0xD2
- #define L3G4200_DEV_READ 0xD3
- #define L3G4200_DEV_ID 0xD3 //设备ID
- //L3G4200 内部寄存器地址
- #define WHO_AM_I 0x0F
- #define CTRL_REG1 0x20
- #define CTRL_REG2 0x21
- #define CTRL_REG3 0x22
- #define CTRL_REG4 0x23
- #define CTRL_REG5 0x24
- #define REFERENCE 0x25
- #define OUT_TEMP 0x26
- #define STATUS_REG 0x27
- #define OUT_X_L 0x28
- #define OUT_X_H 0x29
- #define OUT_Y_L 0x2A
- #define OUT_Y_H 0x2B
- #define OUT_Z_L 0x2C
- #define OUT_Z_H 0x2D
- //=========L3G4200 STATUS寄存器 寄存器位==================//
- #define L3G4200_STATUS_X_READY 0x01
- #define L3G4200_STATUS_Y_READY 0x02
- #define L3G4200_STATUS_Z_READY 0x04
- #define L3G4200_STATUS_XYZ_READY 0x08
- #define L3G4200_STATUS_X_OW 0x10
- #define ML3G4200_STATUS_Y_OW 0x20
- #define L3G4200_STATUS_Z_OW 0x40
- #define L3G4200_STATUS_XYZ_OW 0x80
- //函数声明
- uint8 L3G4200_Init(void);
- void L3G4200_WriteReg(uint8, uint8);
- uint8 L3G4200_ReadReg(uint8);
- int16 L3G4200_GetResult(uint8, uint8);
- #endif
- #include "dev_l3g4200.h"
- static void L3G4200_Delay(void);
- uint8 L3G4200_Init(void)
- {
- I2C_InitTypeDef i2c_init_param;
- uint8 device_id=0;
- //初始化L3G4200_I2CX
- i2c_init_param.I2C_I2Cx = L3G4200_I2CX ; //在DEV_L3G4200.h中修改该值
- i2c_init_param.I2C_IntEnable = FALSE;
- i2c_init_param.I2C_ICR = L3G4200_SCL_200KHZ; //可根据实际电路更改SCL频率
- i2c_init_param.I2C_SclPin = L3G4200_SCLPIN; //在DEV_MMA8451.h中修改该值
- i2c_init_param.I2C_SdaPin = L3G4200_SDAPIN; //在DEV_MMA8451.h中修改该值
- i2c_init_param.I2C_Isr = NULL;
- LPLD_I2C_Init(i2c_init_param);
- //读取设备ID
- device_id = L3G4200_ReadReg(WHO_AM_I);
- //进行寄存器配置
- L3G4200_WriteReg(CTRL_REG1, 0x0f); //100hz 0xff(800hz)
- L3G4200_WriteReg(CTRL_REG2, 0x00);
- L3G4200_WriteReg(CTRL_REG3, 0x08);
- L3G4200_WriteReg(CTRL_REG4, 0x30); //+-2000dps , Data LSB @ lower address
- L3G4200_WriteReg(CTRL_REG5, 0x00);
- return device_id ;
- }
- /*
- * L3G4200_WriteReg
- * 该函数用于配置L3G4200的寄存器
- *
- * 参数:
- * RegisterAddress
- * |__ L3G4200寄存器地址
- * Data
- * |__ 具体写入的字节型数据
- */
- void L3G4200_WriteReg(uint8 RegisterAddress, uint8 Data)
- {
- //发送从机地址
- LPLD_I2C_StartTrans(L3G4200_I2CX,L3G4200_DEV_ADDR, I2C_MWSR);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //写L3G4200寄存器地址
- LPLD_I2C_WriteByte(L3G4200_I2CX, RegisterAddress);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //向寄存器中写具体数据
- LPLD_I2C_WriteByte(L3G4200_I2CX, Data);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- LPLD_I2C_Stop(L3G4200_I2CX);
- L3G4200_Delay();
- }
- /*
- * L3G4200_ReadReg
- * 该函数用于读取L3G4200的数据
- *
- * 参数:
- * RegisterAddress
- * |__ MMA8451寄存器地址
- * 返回值
- * 加速传感器的测量值
- */
- uint8 L3G4200_ReadReg(uint8 RegisterAddress)
- {
- uint8 result=0;
- //发送从机地址
- LPLD_I2C_StartTrans(L3G4200_I2CX,L3G4200_DEV_ADDR, I2C_MWSR);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //写L3G4200_I2CX寄存器地址
- LPLD_I2C_WriteByte(L3G4200_I2CX, RegisterAddress);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //再次产生开始信号
- LPLD_I2C_ReStart(L3G4200_I2CX);
- //发送从机地址和读取位
- LPLD_I2C_WriteByte(L3G4200_I2CX , L3G4200_DEV_READ);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //转换主机模式为读
- LPLD_I2C_SetMasterWR(L3G4200_I2CX, I2C_MRSW);
- //关闭应答ACK
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_OFF);//关闭ACK
- // Dummy read 虚假读取
- result =LPLD_I2C_ReadByte(L3G4200_I2CX);
- LPLD_I2C_WaitAck(L3G4200_I2CX, I2C_ACK_ON);
- //发送停止信号
- LPLD_I2C_Stop(L3G4200_I2CX);
- //读IIC数据
- result = LPLD_I2C_ReadByte(L3G4200_I2CX);
- L3G4200_Delay();
- return result;
- }
- /*
- * 函数功能:读L3G4200加速度输出
- * 参数?
- * Status - 数据寄存器状态
- * Regs_Addr - 数据寄存器地址
- * 函数返回值:加速度值(int16)
- */
- int16 L3G4200_GetResult(uint8 Status, uint8 Regs_Addr)
- {
- uint8 ret=0;
- uint16 cnt=0;
- int16 result,temp;
- // 等待转换完成并取出值
- while(!(ret&Status))
- {
- ret = L3G4200_ReadReg( STATUS_REG);
- if(++cnt>500)
- // break;
- ;
- }
- temp= L3G4200_ReadReg( Regs_Addr); //低字节数据
- result = L3G4200_ReadReg( Regs_Addr+1); //高字节
- result=result<<8;
- result=result|temp;
- return result;
- }
- /*
- * 延时函数
- */
- static void L3G4200_Delay(void){
- int n;
- for(n=1;n<200;n++) {
- asm("nop");
- }
- }
复制代码
求助,为什么不管我读什么寄存器,读回来的都是0xff,
器件地址应该是没写错的
|
|