本帖最后由 Ryanli 于 2012-5-12 13:41 编辑
D://1.jpg[/img]- #include <Wire.h> // IIC运行库
- //#include <SSD1306.h> // OLED运行库
-
- //#define OLED_CS 42 // CS脚
- //#define OLED_CLK 44 // CLK脚
- //#define OLED_MOSI 46 // MISO脚
- //#define OLED_RESET 48 // RST脚
- //#define OLED_DC 50 // D/C脚
-
- //SSD1306 oled(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- // 定义OLED
- #define Acc 0x1D; // ADXL345的I2C地址
- #define Gyr 0x69; // L3G4200D的I2C地址
- #define Mag 0x1E; // HMC5883L的I2C地址
-
- char str[16]; // 输出字符串(切勿溢出)
- int curve[128]; // 曲线数据
- int i = 0;
-
- void setup() {
- //oled.ssd1306_init(SSD1306_SWITCHCAPVCC);
- //oled.clear(); // 清空oled屏
- //oled.drawstring(0, 0, "Setting up...");
- //oled.display(); // 显示到oled
- // 初始化OLED
- Serial.begin(9600);
- sensor_init(); // 初始化传感器
- delay(2000);
- }
- void loop() {
- int x;
- curve[i]=gDat(Gyr,0); // 读取传感器,根据需要读取不同传感器数据
- if (curve[i] < -26) curve[i] = -26; // 上溢出
- if (curve[i] > 26) curve[i] = 26; // 下溢出
- //oled.clear(); // 清空oled屏
-
- for (x = 0; x < 127; x++)
- {
- Serial.print("x=");// 输出字符串
- Serial.println(curve[i]); // 输出到到oled //oled.drawline(x, 37 - curve[x], x+1, 37 - curve[x+1], WHITE);
- } // 绘制曲线
- //oled.display(); // 显示到oled
- i++;
- if (i == 128) i = 0;
- delay(10);
- }
-
- int gDat(int device, int axis) {
-
- // 读九轴姿态传感器寄存器函数
- // For Arduino, by 黑马
- // 调用参数表
- // type device axis
- // 0 1 2
- // ADXL345 Acc x y z
- // L3G4200D Gyr x y z
- // HMC5883L Mag x z y
- // Example
- // 00 #include <Wire.h>
- // 01 #define Acc 0x1D;
- // 02 #define Gyr 0x69;
- // 03 #define Mag 0x1E;
- // 04
- // 05 void setup() {
- // 06 sensor_init();
- // 07 delay(1000);
- // 08 }
- // 09
- // 10 void loop() {
- // 11 Z-Gyroscope = gDat(Gyr, 2);
- // 12 delay(50);
- // 13 }
-
- int v;
- byte vL, vH, address; // 存放byte数值
- if (device == Acc) address = 0x32; // ADXL345的读数地址
- if (device == Gyr) address = 0xA8; // L3G4200D的读数地址
- if (device == Mag) address = 0x03; // HMC5883L的读数地址
- address = address + axis * 2; // 数据偏移-坐标轴
- Wire.beginTransmission(device); // 开始传输数据
- Wire.send(address); // 发送指针
- Wire.requestFrom(device, 2); // 请求2 byte数据
- while(Wire.available() < 2); // 成功获取前等待
- vL = Wire.receive();
- vH = Wire.receive(); // 读取数据
- Wire.endTransmission(); // 结束传输
- if (device == Mag) v = (vL << 8) | vH;
- else v = (vH << 8) | vL; // 将byte数据合并为Int
- return v; // 返回读书值
- }
-
- void sensor_init() { // 配置九轴姿态传感器
- writeRegister(Acc, 0x2D, 0b00001000); // 测量模式
- // 配置ADXL345
- writeRegister(Gyr, 0x20, 0b00001111); // 设置睡眠模式、x, y, z轴使能
- writeRegister(Gyr, 0x21, 0b00000000); // 选择高通滤波模式和高通截止频率
- writeRegister(Gyr, 0x22, 0b00000000); // 设置中断模式
- writeRegister(Gyr, 0x23, 0b00110000); // 设置量程(2000dps)、自检状态、SPI模式
- writeRegister(Gyr, 0x24, 0b00000000); // FIFO & 高通滤波
- // 配置L3G4200D(2000 deg/sec)
- writeRegister(Mag, 0x02, 0x00); // 连续测量
- // 配置HMC5883L
- }
-
- void writeRegister(int device, byte address, byte val) { // 写寄存器
- Wire.beginTransmission(device);
- Wire.send(address);
- Wire.send(val);
- Wire.endTransmission();
- }
复制代码 前辈你好,我是第一次用arduino,因为没有显示屏所以我把你的程序稍稍改了一下,想用串口观察,可是为什么出现好多同一种错误呢?还望前辈解答啊。
|