标签:
最近做一个设计G-Sense的设备,需要一个三轴加速度传感器,最终选择飞思卡尔的MMA7455,因为看起来似乎比博士的三轴加速度芯片简单一些,哈哈。原本是准备应用在ARM上的,首先在比较熟悉的AVR单片机的环境下做个测试。
来个MMA7455模块的近照:
考虑到7455工作在3.3V,所以用宽电压工作的Atmega16L,这样就不存在电压冲突的问题。电路图参考7455的Datasheet:
在这里,中断INT1和INT2我都没有用,ACC_CS接3.3采用I2C模式,IADDR0接3.3,设置7455的写地址为0X3A,读地址为0X3B,该引脚如果接地,则7455的读写地址分分别为0X38和0X39(参考7455手册)。
下面贴出所有的代码(测试通过)
<span style="font-size:13px;">/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: Type.h ** Last modified Date: 2011-09-1 ** Last Version: 1.0 ** Description: 定义数据类型的别名 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-01 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #ifndef __TYPE_H__ #define __TYPE_H__ /******************************************************************************************************** * Date types(Compiler specific) 数据类型(和编译器相关) * ********************************************************************************************************/ typedef unsigned char uint8; // Unsigned 8 bit quantity 无符号8位整型变量 typedef signed char int8; // Signed 8 bit quantity 有符号8位整型变量 typedef unsigned short uint16; // Unsigned 16 bit quantity 无符号16位整型变量 typedef signed short int16; // Signed 16 bit quantity 有符号16位整型变量 typedef unsigned int uint32; // Unsigned 32 bit quantity 无符号32位整型变量 typedef signed int int32; // Signed 32 bit quantity 有符号32位整型变量 typedef float fp32; // Single precision floating point 单精度浮点数(32位长度) typedef double fp64; // Double precision floating point 双精度浮点数(64位长度) #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif #endif /********************************************************************************************************* * End Of File * *********************************************************************************************************/ </span>
/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: I2C.h ** Last modified Date: 2011-09-1 ** Last Version: 1.0 ** Description: IO口模拟I2C协议的函数声明 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-1 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #ifndef __I2C_H__ #define __I2C_H__ #include "Type.h" extern void I2C_Init(void); extern void Delay(void); extern void I2C_Start(void); extern void I2C_Stop(void); extern uint8 Write_I2C(uint8 data); extern uint8 Read_I2C(uint8 ack); #endif /********************************************************************************************************* * End Of File * *********************************************************************************************************/
/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: I2C.c ** Last modified Date: 2011-09-01 ** Last Version: 1.0 ** Description: IO口模拟I2C协议的函数实现 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-01 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #include<iom16v.h> #include<macros.h> #include"I2C.h" #include "Type.h" //外部上拉电阻,PORTC.0--SCL,PORTC.1--SDA,模拟I2C协议 //当DDRC.0和DDRC.1置为输出时,拉低SDA;置为输入时,内部上拉拉高SDA(省去了外部上拉电阻) #define SCL_0 DDRA|=BIT(0) #define SCL_1 DDRA&=~BIT(0) #define SDA_0 DDRA|=BIT(1) #define SDA_1 DDRA&=~BIT(1) #define SDA_in (PINA&0X02) //判断SDA的电平 /********************************************************************************************************* ** Function name: I2C_Inti ** Descriptions: I2C初始化 ** input parameters: 无 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void I2C_Init(void) { PORTA&=~(BIT(0)|BIT(1)); SCL_1; SDA_1; } /********************************************************************************************************* ** Function name: Delay ** Descriptions: 模拟I2C时序的延时 ** input parameters: 无 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void Delay(void) { NOP(); NOP(); NOP(); NOP(); } /********************************************************************************************************* ** Function name: I2C_Start ** Descriptions: 模拟I2C的开始时序 ** input parameters: 无 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void I2C_Start(void) { SDA_1; SCL_1; Delay(); SDA_0; Delay(); SCL_0; Delay(); } /********************************************************************************************************* ** Function name: I2C_Stop ** Descriptions: 模拟I2C时序的结束时序 ** input parameters: 无 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void I2C_Stop(void) { SDA_0; SCL_1; Delay(); SDA_1; Delay(); } /********************************************************************************************************* ** Function name: Write_I2C ** Descriptions: 向I2C器件的指定单元写一个字节 ** input parameters: 代写入的一字节数据 ** output parameters: 无 ** Returned value: 接收器件的应答信号(1-已经应答 0-没有应答) *********************************************************************************************************/ uint8 Write_I2C(uint8 data) { uint8 ack,i; for(i=0;i<8;i++) { if(data&0X80) { SDA_1; } else { SDA_0; } SCL_1; Delay(); SCL_0; data <<= 1; Delay(); } Delay(); SDA_1; //释放总线,等到应答 Delay(); SCL_1; Delay(); if(SDA_in) { ack=0; } else { ack=1; } SCL_0; Delay(); return ack; } /********************************************************************************************************* ** Function name: Read_I2C ** Descriptions: 读取I2C器件一个字节 ** input parameters: 是否应答(0-NACK,1-ACK) ** output parameters: 无 ** Returned value: 读到的一个字节 *********************************************************************************************************/ uint8 Read_I2C(uint8 ack) { uint8 i,temp; temp=0; SDA_1; for(i=0;i<8;i++) { Delay(); SCL_0; Delay(); SCL_1; Delay(); temp<<=1; if(SDA_in) { temp++; } } SCL_0; //使从机等待,发出应答信号 Delay(); if(!ack) { SDA_1; } else { SDA_0; } Delay(); SCL_1; Delay(); SCL_0; Delay(); return temp; }
/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: MMA7455.h ** Last modified Date: 2011-09-02 ** Last Version: 1.0 ** Description: 读写MMA7455的声明 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-02 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #ifdef __MMA7455_H__ #define __MMA7455_H__ #include "Type.h" void MMA7455_ByteWrite(uint8 add, uint8 data); uint8 MMA7455_ByteRead(uint8 add); #endif
/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: MMA7455.c ** Last modified Date: 2011-09-02 ** Last Version: 1.0 ** Description: 读写MMA7455的实现 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-02 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #include<iom16v.h> #include<macros.h> #include"I2C.h" #include "Type.h" #include "MMA7455.h" #include "USART.h" #define ACK 1 //MMA7455应答 #define NACK 0 /********************************************************************************************************* ** Function name: 毫秒级延时程序 ** Descriptions: 延时N毫秒 ** input parameters: 延时秒数 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void Delay_ms(uint32 ms) { uint32 i,j; for(i=0;i<ms;i++) for(j=0;j<564;j++); } /********************************************************************************************************* ** Function name: MMA7455_ByteWrite ** Descriptions: 向MMA7455指定地址写一个字节 ** input parameters: 写入地址:add,写入字节:data ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void MMA7455_ByteWrite(uint8 add,uint8 data) { I2C_Start(); Write_I2C(0x3A); //写MMA7455的设备地址 Write_I2C(add); Write_I2C(data); I2C_Stop(); Delay_ms(10); //这个延时很重要,保证写入时间 } /********************************************************************************************************* ** Function name: MMA7455_ByteRead ** Descriptions: 读MMA7455指定地址一个字节 ** input parameters: 读字节地址:add ** output parameters: 无 ** Returned value: 读出的字节 *********************************************************************************************************/ uint8 MMA7455_ByteRead(uint8 add) { uint8 data; I2C_Start(); Write_I2C(0x3A); //写命令 Write_I2C(add); I2C_Start(); Write_I2C(0x3B); //读命令 data=Read_I2C(NACK); I2C_Stop(); return data; }
/****************************************Copyright (c)************************************************** ** Southeast Univerdity **--------------File Info------------------------------------------------------------------------------- ** File Name: main.c ** Last modified Date: 2011-09-05 ** Last Version: 1.0 ** Description: 测试函数 **------------------------------------------------------------------------------------------------------ ** Created By: JianGe ** Created date: 2011-09-01 ** Version: 1.0 **------------------------------------------------------------------------------------------------------ */ #include<iom16v.h> #include<macros.h> #include"I2C.h" #include "Type.h" #include "MMA7455.h" #include "USART.h" void main(void) { int8 X,Y,Z = 0; MMA7455_ByteWrite(0x16,0x05); //配置为measurement模式,8位精度 MMA7455_ByteWrite(0x18,0x80); MMA7455_ByteWrite(0x10,11); //MMA7455_ByteWrite(0x11,0); MMA7455_ByteWrite(0x12,50); //MMA7455_ByteWrite(0x13,0); MMA7455_ByteWrite(0x14,0xD7); MMA7455_ByteWrite(0x15,0xFF); //若校正值为负,则一定要写入0XFF X = MMA7455_ByteRead(0x06); Y = MMA7455_ByteRead(0x07); Z = MMA7455_ByteRead(0x08); while(1); }
标签:
原文地址:http://www.cnblogs.com/tifnan2015/p/4760424.html