ShareIdeas

本博客不再更新,欢迎访问我的github,https://github.com/sunke-github/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  花了两天时间,结合以前写的程序2440 MDK 裸机IIC程序 整理出了,这个通过IIC读取AHRS传感器的程序且可以通过VisualScope.exe 这个软件

进行传感器的调试. 至于云台什么的,感觉应该不会太难,我会持续更新我的四轴进度.加速度计:

角速度计:

 

核心代码:

 1 #include <S3C2440.H>
 2 #include "serial.h"
 3 #include "iic.h"
 4 #include "L3G4200D.h"
 5 #include "ADXL345.h"
 6 unsigned char data[6];
 7 unsigned char Array[10];
 8 void revise(void);
 9 unsigned int CRC16(unsigned char *Array ,unsigned int Len);
10 
11 int main()
12 {
13     unsigned int  i;
14     unsigned int temp; 
15 //    int pitch=0,yaw=0,roll=1;
16     //波特率  0x1a   115200     325    9600
17     init_uart(325);
18     //程序启动,若输出则串口是正常的
19     putstring("Serial communication is normal !\r\n");
20     init_iic();
21     putstring("Initialize IIC successful !\r\n");
22     Init_ADXL345();
23     putstring("Initialize ADXL345 successful !\r\n");
24     InitL3G4200D() ;
25     putstring("Initialize L3G4200D successful !\r\n");
26     while(1)
27     {
28         if(0)
29         {
30             Multiple_read_ADXL345(data);     //测试加速度计
31         }
32         else
33         {
34             Multiple_read_L3G4200D(data);      //测试角速度计
35         }
36  /*****************************************多余内容,可删除*********************************/
37         revise();                //数据校正
38         //Array[6]=Read24C08_nbyte(SlaveAddress_L3G4200D,0x26,1,); //读取温度,温度地址0x26
39         Array[6]=0x00;
40         Array[7]=0x00;      //  温度是8位的,故这个赋值给0x00
41         temp=CRC16(Array,8);
42         Array[8]=temp&0x00ff;
43         Array[9]=(temp&0xff00)>>8;
44  /****************************************************************************************/
45                                              
46         for(i=0 ; i<10;i++)
47         {  
48             //这里是把数据从串口发送出去
49             sendchar(Array[i]);          
50         }
51     }
52     
53     
54 }
55 /******************************************************************
56                            CRC16校验函数
57 *******************************************************************/
58 
59 unsigned int CRC16(unsigned char *Array ,unsigned int Len)          //len在调用时指定为8就OK
60 {  
61     // unsigned char *Rcvbuf ;
62     unsigned int IX,IY,CRC;
63     CRC=0xffff;   //set all 1
64     for(IX=0;IX<Len;IX++)
65     {
66        CRC=CRC^(unsigned int)(Array[IX]);
67        for(IY=0;IY<=7;IY++)
68        {
69          if((CRC&1)!=0)
70             CRC=(CRC>>1)^0xA001;         //0xA001是自定义多项式
71          else 
72             CRC=CRC>>1;
73         }
74      }
75     return(CRC);    
76 }
77 void revise(void)
78 {
79     unsigned int dis_data;
80     dis_data=(data[1]<<8)+data[0];  //合成数据     //进行修正
81     Array[0]=dis_data&0x00ff;
82     Array[1]=(dis_data&0xff00)>>8;
83     dis_data=(data[3]<<8)+data[2];  //合成数据     //进行修正
84     Array[2]=dis_data&0x00ff;
85     Array[3]=(dis_data&0xff00)>>8;
86     Array[4]=data[4];        //把值 给Array数组
87     Array[5]=data[5];    
88 }

补充说明 :这个程序的几乎所有内容都可以在我以前的博客中找到 ,iic通信结合 我写的IIC通信程序;ARM9串口通信部分网上可以找的到;L3G4200D

和 ADXL345的初始化部分,可以结合 自带的测试代码;VisualScope 通信部分协议部分 就按我贴 出的这部分程序写就可以了.波特率 9600 串口 1 .

转载请表明出处 2440(ARM9) L3G4200D ADXL345 裸机程序

posted on 2013-03-10 20:07  ShareIdeas  阅读(725)  评论(2编辑  收藏  举报