在线时间23 小时
UID3092806
注册时间2015-1-15
NXP金币0
该用户从未签到
金牌会员
 
- 积分
- 1687
- 最后登录
- 2015-2-10
|
发表于 2015-2-2 14:06:56
|
显示全部楼层
- #include "common.h"
- #include "math.h"
- #include "isr.h"
- #include "uart.h"
- #include "i2c.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define SlaveAddress 0x3c //定义器件在IIC总线中的从地址
- int X,Y,Z;
- double Angle1,Angle2,Angle3;
- uchar Rec_Data[6];
- uint Acr1,Acr2,Acr3;
- void Delay(uint t)
- {
- while(t--);
- }
- void pause(void)
- {
- int n;
- for(n=0; n<100; n++);
- }
- void Single_Write_HMC5883(uint8 REG_Address,uint8 REG_data)
- {
- I2C_start(I2C0_BASE_PTR); //起始信号
-
-
- I2C_write_byte(I2C0_BASE_PTR,SlaveAddress); //发送设备地址+写信号
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
- I2C_write_byte(I2C0_BASE_PTR,REG_Address); //内部寄存器地址
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
-
- I2C_write_byte(I2C0_BASE_PTR,REG_data); //内部寄存器数据
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
-
- I2C_stop(I2C0_BASE_PTR); //发送停止信号
- pause();
- }
- uchar Single_Read_HMC5883(uint8 REG_Address)
- {
- uint8 result;
- I2C_start(I2C0_BASE_PTR); //起始信号
-
- I2C_write_byte(I2C0_BASE_PTR,SlaveAddress); //发送设备地址+写信号
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
-
- I2C_write_byte(I2C0_BASE_PTR,REG_Address); //发送存储单元地址,从0开始
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
-
- I2C_repeated_start(I2C0_BASE_PTR); //起始信号
-
- I2C_write_byte(I2C0_BASE_PTR,SlaveAddress+1); //发送设备地址+读信号
- I2C_wait(I2C0_BASE_PTR);
- I2C_get_ack(I2C0_BASE_PTR);
-
- I2C_set_rx_mode(I2C0_BASE_PTR);
- I2C_give_nack(I2C0_BASE_PTR);
- result = I2C_read_byte(I2C0_BASE_PTR);
- I2C_wait(I2C0_BASE_PTR);
- I2C_stop(I2C0_BASE_PTR);
- result = I2C_read_byte(I2C0_BASE_PTR);
- pause();
- return result;
- }
- void dis()
- {
- Rec_Data[0]=Single_Read_HMC5883(0x03);
- Rec_Data[1]=Single_Read_HMC5883(0x04);
- Rec_Data[2]=Single_Read_HMC5883(0x05);
- Rec_Data[3]=Single_Read_HMC5883(0x06);
- Rec_Data[4]=Single_Read_HMC5883(0x07);
- Rec_Data[5]=Single_Read_HMC5883(0x08);
- Delay(1000);
- X=Rec_Data[0]<<8 | Rec_Data[1];//Combine MSB and LSB of X Data output register
- Z=Rec_Data[2]<<8 | Rec_Data[3];//Combine MSB and LSB of Z Data output register
- Y=Rec_Data[4]<<8 | Rec_Data[5];//Combine MSB and LSB of Y Data output register
- if(X>=32768)
- X=X-65536;
- if(Y>=32768)
- Y=Y-65536;
- if(Z>=32768)
- Z=Z-65536;
- Angle1=atan2((double)Y,(double)X)*(180/3.1415)+180;
- Angle2=atan2((double)X,(double)Z)*(180/3.1415)+180;
- Angle3=atan2((double)Z,(double)Y)*(180/3.1415)+180;
- Acr1=(uint)Angle1;
- Acr2=(uint)Angle2;
- Acr3=(uint)Angle3;
- printf("Acr1=%d Acr2=%d Acr3=%d \n",Acr1,Acr2,Acr3);
- }
- int main(void)
- {
- printf("Hellow");
- GPIOA_PDDR |= (1<<3);
- I2C_Init(I2C0_BASE_PTR);
- I2C_set_master_mode(I2C0_BASE_PTR);
- Single_Write_HMC5883(0x02,0x00);
- while(1)
- {
- dis();
- }
- }
复制代码 |
|