Arduino入門筆記(8):利用12864和ADXL345學習重力感應控制


轉載請注明:@小五義 http://www.cnblogs.com/xiaowuyi

歡迎加入討論群 64770604

一、本次實驗所需器材

1、Arduino UNO板 https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-15820725129.16.AtgoEm&id=545093340395

2、12864顯示器(帶中文字庫)

3、ADXL345加速度傳感器:ADI公司推出的一款小而薄的低功耗3軸加速度計,分辨率高(13位),測量范圍達±16g(1g顯示數字為256)。數字輸出數據為16位二進制補碼格式,可通過SPI(3線或4線)或I2C數字接口訪問。該系列傳感器還有ADXL335、ADXL346等。這種傳感器被廣泛應用,如用來制作計步器(http://www.21ic.com/app/control/201011/69048.htm)等。

4、面包板:2個

5、電阻:1KΩ電阻2個。

二、硬件連接方法

1、arduino與12864液晶SPI連接方式的硬件連接圖

Untitled Sketch 2_bb

2、adxl345的連接方法

adxl345

12864_adxl345_bb

拍大師圖片1

三、實驗過程

1、12864顯示文字和圖像

(1)12864在顯示漢字時,需要用到漢字的內碼,我使用的軟件可從這里下載http://www.kuaipan.cn/file/id_749007936689913.htm?source=1。如漢字“開始”對應內碼是BFAA和CABC,那么就轉換成0xBF,0xAA,0xCA,0xBC。

(2)12864顯示圖像時,只能顯示位圖。一般圖片可通過ps轉換為位圖,具體方法可百度。獲取位圖后,再利用圖片取模軟件(http://www.kuaipan.cn/file/id_749007936689914.htm?source=1)進行轉化。如圖片:

9999

打開圖片取模軟件,首先進行參數設置,轉出格式選C語言,輸出大小128*64,點參數確認。

未標題-2 拷貝

 

然后點載入圖片,

333333

點擊數據保存,得到C語言文件:

/////////////////////////////////////////////////////////////////////////
// Bitmap點陣數據表                                                    //
// 圖片: C:\..\24tttt88.bmp,橫向取模左高位,數據排列:從左到右從上到下   //
// 圖片尺寸: 128 * 64                                                  //
/////////////////////////////////////////////////////////////////////////
unsigned char code nBitmapDot[] =                  // 數據表
{
      0xFF,0x80,0x00,0x00,0x00,0x00,0x18,0x61,
      0x80,0x00,0x00,0x01,0xFE,0x70,0x00,0x00,
      0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x60,
      0xC0,0x00,0x00,0x08,0xFF,0xFA,0x40,0x08,
      0xDB,0xE0,0x00,0x00,0x00,0x00,0x00,0x60,
      0x60,0x00,0x00,0x07,0xFF,0xEF,0x80,0x00,
      0xFC,0xF0,0x00,0x00,0x00,0x00,0x00,0x60,
      0x20,0x00,0x00,0x07,0x4F,0xFF,0xC0,0x10,
      0xFC,0x10,0x00,0x00,0x00,0x00,0x00,0x40,
      0x20,0x00,0x00,0x00,0x7F,0xFF,0xD0,0x00,
      0x70,0x18,0x00,0x00,0x01,0xF8,0x00,0xC0,
      0x20,0x00,0x00,0x00,0xC3,0xBF,0xFF,0x80,
      0x58,0x5C,0x00,0x00,0x0F,0x00,0x01,0xC0,
      0x20,0x00,0x00,0x00,0xC3,0xFF,0xFF,0x00,
      0x18,0x0C,0x00,0x00,0x18,0x00,0x01,0x10,
      0x60,0x00,0x00,0x00,0x81,0xFF,0x80,0x00,
      0x10,0x00,0x00,0x00,0x60,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0xFD,0x80,0x00,
      0x30,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x01,0x7D,0xC0,0x00,
      0x38,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x31,0xC0,0x00,
      0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x18,0xC0,0x00,
      0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x30,0xC0,0x00,
      0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x30,0xC0,0x00,
      0x08,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x20,0xC0,0x00,
      0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x60,0xC0,0x00,
      0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x0B,0xA0,0x00,0x01,0xA0,0x00,
      0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
      0x00,0x05,0x7F,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
      0x00,0x1E,0x7F,0xF0,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x3A,0x00,0x80,0x00,0x00,
      0x00,0x3B,0x7F,0xF0,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x3D,0x00,0x00,0x00,0x00,
      0x00,0x2B,0x70,0xE0,0x00,0x08,0x00,0x00,
      0x00,0x00,0x00,0x1F,0x60,0x00,0x00,0x00,
      0x00,0x7E,0x3F,0x80,0x06,0x3C,0x00,0x00,
      0x00,0x00,0x00,0x0F,0xB6,0x40,0x00,0x00,
      0x00,0x9E,0x3E,0x00,0x0F,0xFE,0x00,0x00,
      0x00,0x00,0x00,0x0F,0xD7,0xF6,0x3F,0xE7,
      0x80,0xBF,0xBC,0x00,0x0F,0xFF,0x00,0x00,
      0x00,0x00,0x00,0x06,0xFF,0x3F,0xE6,0x7D,
      0xB0,0xFF,0xF8,0x00,0x03,0xFF,0x80,0x00,
      0x00,0x00,0x00,0x03,0xEE,0x66,0x67,0x7C,
      0xF8,0xEF,0xE0,0x00,0x00,0xFF,0x80,0x00,
      0xFF,0xC4,0x00,0x00,0x3F,0x5E,0x7E,0xDF,
      0xCF,0x3F,0xC0,0x00,0x00,0x1F,0xA0,0x00,
      0x69,0x7D,0xF0,0x00,0x2F,0xFE,0xE7,0xFF,
      0xFC,0x4F,0x80,0x00,0x00,0x0F,0x80,0x00,
      0xFB,0x3F,0xFC,0x00,0x0D,0xFF,0xE5,0x3F,
      0xFF,0xFE,0x00,0x00,0x00,0x03,0x80,0x00,
      0xFF,0xFF,0xFA,0x00,0x01,0xBF,0xF2,0xFF,
      0xFB,0xF8,0x00,0x00,0x00,0x07,0xC8,0x00,
      0xF7,0xFC,0x0F,0x80,0x00,0x3C,0xFD,0xFA,
      0xEF,0xF8,0x00,0x00,0x00,0x07,0xBE,0x00,
      0xFE,0x80,0x00,0xC0,0x00,0x05,0x2A,0xDF,
      0xFD,0xF8,0x00,0x00,0x00,0x07,0xFF,0xF5,
      0xC0,0x00,0x00,0x40,0x00,0x01,0x7D,0xFE,
      0x7F,0xE0,0x00,0x00,0x00,0x03,0xBF,0xCF,
      0x80,0x08,0x01,0x80,0x00,0x00,0x9F,0xDF,
      0xFE,0xE0,0x00,0x00,0x00,0x03,0xFF,0xFF,
      0x80,0x02,0x07,0x00,0x00,0x00,0x9F,0xF3,
      0xDF,0x40,0x00,0x00,0x00,0x01,0xFF,0xFD,
      0x88,0x00,0x14,0x00,0x00,0x00,0xDF,0xD6,
      0xF4,0x80,0x00,0x00,0x00,0x01,0xFF,0xFF,
      0x81,0xE6,0xE0,0x00,0x00,0x01,0xFF,0xCF,
      0xEC,0x80,0x00,0x00,0x00,0x01,0xFB,0xFF,
      0xE0,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,
      0xFF,0x00,0x00,0x00,0x00,0x00,0xFD,0xFE,
      0xE0,0x00,0x00,0x00,0x00,0x01,0xDF,0xFF,
      0xF6,0x00,0x00,0x00,0x00,0x01,0xFF,0xFE,
      0xF0,0x00,0x00,0x00,0x00,0x01,0xFE,0x7F,
      0xF7,0x00,0x00,0x00,0x00,0x01,0xF7,0xFF,
      0xE0,0x00,0x00,0x00,0x00,0x0F,0xEE,0x20,
      0x11,0x00,0x00,0x00,0x00,0x01,0xF6,0xBF,
      0x78,0x00,0x00,0x00,0x00,0x0F,0xF9,0xBE,
      0x19,0x00,0x00,0x00,0x00,0x03,0xF7,0x9F,
      0x1C,0x00,0x00,0x00,0x00,0x0F,0xE0,0xBE,
      0x00,0x80,0x00,0x00,0x00,0x0E,0xF7,0x8F,
      0x0C,0x00,0x00,0x00,0x00,0x0F,0xC0,0x05,
      0x00,0x00,0x00,0x00,0x00,0x02,0x63,0x87,
      0x06,0x00,0x00,0x00,0x00,0x1E,0x00,0x05,
      0x00,0x00,0x00,0x00,0x00,0x00,0x23,0xC3,
      0x04,0x00,0x00,0x00,0x00,0x3C,0x00,0x87,
      0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xCF,
      0x06,0x00,0x00,0x00,0x00,0x70,0x00,0x07,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE5,
      0x02,0x00,0x00,0x00,0x00,0xF0,0x00,0x02,
      0x40,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
      0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x67,
      0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC3,
      0x02,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC3,
      0x06,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC3,
      0x06,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,
      0x00,0x00,0x00,0x00,0x01,0xA0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,
      0x00,0x00,0x00,0x00,0x01,0xB0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x01,0x4C,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x1B,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x3F,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x3D,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x13,0xF8,0x00,0x00,0x00,0x3D,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x07,0xF8,0x00,0x00,0x00,0x37,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x06,0x1F,0xC0,0x00,0x00,0x1F,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x1F,0x68,0x0B,0x80,0x01
};

這里的數據表,就是圖片數據。

下面用12864顯示上面的圖片,代碼如下:

#小五義:http://www.cnblogs.com/xiaowuyi
#include <LCD12864RSPI.h>
    /*
    LCD  Arduino
    PIN1 = GND
    PIN2 = 5V
    RS(CS) = 8;
    RW(SID)= 9;
    EN(CLK) = 3;
    PIN15 PSB = GND;
    */
     
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
     
unsigned char show0[]={0xD0,0xA1,0xCE,0xE5,0xD2,0xE5};//小五義
unsigned char show1[]="http://www.cnblo";
unsigned char show2[]="gs.com/xiaowuyi";


unsigned char logo0[]={                                         
      0xFF,0x80,0x00,0x00,0x00,0x00,0x18,0x61,
      0x80,0x00,0x00,0x01,0xFE,0x70,0x00,0x00,
      0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x60,
      0xC0,0x00,0x00,0x08,0xFF,0xFA,0x40,0x08,
      0xDB,0xE0,0x00,0x00,0x00,0x00,0x00,0x60,
      0x60,0x00,0x00,0x07,0xFF,0xEF,0x80,0x00,
      0xFC,0xF0,0x00,0x00,0x00,0x00,0x00,0x60,
      0x20,0x00,0x00,0x07,0x4F,0xFF,0xC0,0x10,
      0xFC,0x10,0x00,0x00,0x00,0x00,0x00,0x40,
      0x20,0x00,0x00,0x00,0x7F,0xFF,0xD0,0x00,
      0x70,0x18,0x00,0x00,0x01,0xF8,0x00,0xC0,
      0x20,0x00,0x00,0x00,0xC3,0xBF,0xFF,0x80,
      0x58,0x5C,0x00,0x00,0x0F,0x00,0x01,0xC0,
      0x20,0x00,0x00,0x00,0xC3,0xFF,0xFF,0x00,
      0x18,0x0C,0x00,0x00,0x18,0x00,0x01,0x10,
      0x60,0x00,0x00,0x00,0x81,0xFF,0x80,0x00,
      0x10,0x00,0x00,0x00,0x60,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0xFD,0x80,0x00,
      0x30,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x01,0x7D,0xC0,0x00,
      0x38,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x31,0xC0,0x00,
      0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x18,0xC0,0x00,
      0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x30,0xC0,0x00,
      0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x30,0xC0,0x00,
      0x08,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x20,0xC0,0x00,
      0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x60,0xC0,0x00,
      0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x0B,0xA0,0x00,0x01,0xA0,0x00,
      0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
      0x00,0x05,0x7F,0x30,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
      0x00,0x1E,0x7F,0xF0,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x3A,0x00,0x80,0x00,0x00,
      0x00,0x3B,0x7F,0xF0,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x3D,0x00,0x00,0x00,0x00,
      0x00,0x2B,0x70,0xE0,0x00,0x08,0x00,0x00,
      0x00,0x00,0x00,0x1F,0x60,0x00,0x00,0x00,
      0x00,0x7E,0x3F,0x80,0x06,0x3C,0x00,0x00,
      0x00,0x00,0x00,0x0F,0xB6,0x40,0x00,0x00,
      0x00,0x9E,0x3E,0x00,0x0F,0xFE,0x00,0x00,
      0x00,0x00,0x00,0x0F,0xD7,0xF6,0x3F,0xE7,
      0x80,0xBF,0xBC,0x00,0x0F,0xFF,0x00,0x00,
      0x00,0x00,0x00,0x06,0xFF,0x3F,0xE6,0x7D,
      0xB0,0xFF,0xF8,0x00,0x03,0xFF,0x80,0x00,
      0x00,0x00,0x00,0x03,0xEE,0x66,0x67,0x7C,
      0xF8,0xEF,0xE0,0x00,0x00,0xFF,0x80,0x00,
      0xFF,0xC4,0x00,0x00,0x3F,0x5E,0x7E,0xDF,
      0xCF,0x3F,0xC0,0x00,0x00,0x1F,0xA0,0x00,
      0x69,0x7D,0xF0,0x00,0x2F,0xFE,0xE7,0xFF,
      0xFC,0x4F,0x80,0x00,0x00,0x0F,0x80,0x00,
      0xFB,0x3F,0xFC,0x00,0x0D,0xFF,0xE5,0x3F,
      0xFF,0xFE,0x00,0x00,0x00,0x03,0x80,0x00,
      0xFF,0xFF,0xFA,0x00,0x01,0xBF,0xF2,0xFF,
      0xFB,0xF8,0x00,0x00,0x00,0x07,0xC8,0x00,
      0xF7,0xFC,0x0F,0x80,0x00,0x3C,0xFD,0xFA,
      0xEF,0xF8,0x00,0x00,0x00,0x07,0xBE,0x00,
      0xFE,0x80,0x00,0xC0,0x00,0x05,0x2A,0xDF,
      0xFD,0xF8,0x00,0x00,0x00,0x07,0xFF,0xF5,
      0xC0,0x00,0x00,0x40,0x00,0x01,0x7D,0xFE,
      0x7F,0xE0,0x00,0x00,0x00,0x03,0xBF,0xCF,
      0x80,0x08,0x01,0x80,0x00,0x00,0x9F,0xDF,
      0xFE,0xE0,0x00,0x00,0x00,0x03,0xFF,0xFF,
      0x80,0x02,0x07,0x00,0x00,0x00,0x9F,0xF3,
      0xDF,0x40,0x00,0x00,0x00,0x01,0xFF,0xFD,
      0x88,0x00,0x14,0x00,0x00,0x00,0xDF,0xD6,
      0xF4,0x80,0x00,0x00,0x00,0x01,0xFF,0xFF,
      0x81,0xE6,0xE0,0x00,0x00,0x01,0xFF,0xCF,
      0xEC,0x80,0x00,0x00,0x00,0x01,0xFB,0xFF,
      0xE0,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,
      0xFF,0x00,0x00,0x00,0x00,0x00,0xFD,0xFE,
      0xE0,0x00,0x00,0x00,0x00,0x01,0xDF,0xFF,
      0xF6,0x00,0x00,0x00,0x00,0x01,0xFF,0xFE,
      0xF0,0x00,0x00,0x00,0x00,0x01,0xFE,0x7F,
      0xF7,0x00,0x00,0x00,0x00,0x01,0xF7,0xFF,
      0xE0,0x00,0x00,0x00,0x00,0x0F,0xEE,0x20,
      0x11,0x00,0x00,0x00,0x00,0x01,0xF6,0xBF,
      0x78,0x00,0x00,0x00,0x00,0x0F,0xF9,0xBE,
      0x19,0x00,0x00,0x00,0x00,0x03,0xF7,0x9F,
      0x1C,0x00,0x00,0x00,0x00,0x0F,0xE0,0xBE,
      0x00,0x80,0x00,0x00,0x00,0x0E,0xF7,0x8F,
      0x0C,0x00,0x00,0x00,0x00,0x0F,0xC0,0x05,
      0x00,0x00,0x00,0x00,0x00,0x02,0x63,0x87,
      0x06,0x00,0x00,0x00,0x00,0x1E,0x00,0x05,
      0x00,0x00,0x00,0x00,0x00,0x00,0x23,0xC3,
      0x04,0x00,0x00,0x00,0x00,0x3C,0x00,0x87,
      0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xCF,
      0x06,0x00,0x00,0x00,0x00,0x70,0x00,0x07,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE5,
      0x02,0x00,0x00,0x00,0x00,0xF0,0x00,0x02,
      0x40,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
      0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x67,
      0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC3,
      0x02,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC3,
      0x06,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC3,
      0x06,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,
      0x00,0x00,0x00,0x00,0x01,0xA0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,
      0x00,0x00,0x00,0x00,0x01,0xB0,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x01,0x4C,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x1B,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x3F,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x3D,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x13,0xF8,0x00,0x00,0x00,0x3D,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x07,0xF8,0x00,0x00,0x00,0x37,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x06,0x1F,0xC0,0x00,0x00,0x1F,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x1F,0x68,0x0B,0x80,0x01
    };
    

 void setup()
    {
    LCDA.Initialise(); // 屏幕初始化
    delay(100);
    }
     
void loop()
    {
    LCDA.CLEAR();//清屏
    delay(100);
    LCDA.DisplayString(0,0,show0,AR_SIZE(show0));//第1行第1格開始,顯示文字小五義
    //delay(1000);
    LCDA.DisplayString(1,0,show1,AR_SIZE(show1));//第2行第1格開始,顯示文字www.cnblogs.com/xiaowuyi
    LCDA.DisplayString(2,0,show2,AR_SIZE(show2));
    delay(5000);
    LCDA.CLEAR();//清屏
    delay(100);
    LCDA.DrawFullScreen(logo0);
    delay(1000);
    }

 

這里用的LCD12864RSPI.h這個庫是針對1.0以上版本IDE的,下載:http://www.kuaipan.cn/file/id_749007936689932.htm?source=1

實現視頻:

 

2、ADXL345獲取加速度值

通過以下代碼,12864顯示器上會顯示出X、Y、Z的值:

#小五義: http://www.cnblogs.com/xiaowuyi
#include <Wire.h>  //調用arduino自帶的I2C庫
#include <LCD12864RSPI.h>
 
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37


unsigned char showx[]="X=";
unsigned char showy[]="Y=";
unsigned char showz[]="Z=";

 
int ADXAddress = 0xA7>>1;  //轉換為7位地址
int reading = 0;
int val = 0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;
 
void setup()
{
  LCDA.Initialise(); // 屏幕初始化
  delay(100);
  Wire.begin();  //初始化I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();
}
 
void loop()
{
  double tempx=0;
  char strx[5];
  double tempy=0;
  char stry[5];
  double tempz=0;
  char strz[5];
  
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_X0);
  Wire.write(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    X0 = Wire.read();
    X1 = Wire.read();
    X1 = X1<<8;
    X_out = X0+X1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Y0);
  Wire.write(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Y0 = Wire.read();
    Y1 = Wire.read();
    Y1 = Y1<<8;
    Y_out = Y0+Y1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Z0);
  Wire.write(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Z0 = Wire.read();
    Z1 = Wire.read();
    Z1 = Z1<<8;
    Z_out = Z0+Z1;
  }
 
  Xg = X_out/256.00;//把輸出結果轉換為重力加速度g,精確到小數點后2位。
  Yg = Y_out/256.00;
  Zg = Z_out/256.00;
  LCDA.CLEAR();//清屏
  
  LCDA.DisplayString(0,0,showx,2);//使屏幕顯示文字X=
  tempx=Xg;
  dtostrf(tempx,5,2,strx);
  LCDA.DisplayString(0,3,(unsigned char *)strx,6);
  
  LCDA.DisplayString(1,0,showy,2);//使屏幕顯示文字Y=
  tempy=Yg;
  dtostrf(tempy,5,2,stry);
  LCDA.DisplayString(1,3,(unsigned char *)stry,6);

  LCDA.DisplayString(2,0,showz,2);//使屏幕顯示文字Z=
  tempz=Zg;
  dtostrf(tempz,5,2,strz);
  LCDA.DisplayString(2,3,(unsigned char *)strz,6);  

  delay(300);  //延時0.3秒,刷新頻率這里進行調整
 
}

實現視頻效果:

 

3、利用ADXL345實現重力感應效果

實驗中,當向某一方向轉動ADXL345模塊時,12864屏幕會顯示轉動方向,如果能配合上動畫效果會更加明顯,這里只是用文字進行了顯示。

代碼如下:

#小五義:http://www.cnblogs.com/xiaowuyi
#include <Wire.h>  //調用arduino自帶的I2C庫
#include <LCD12864RSPI.h>
 
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )

unsigned char show1x[]={0xBF,0xAA,0xCA,0xBC};
unsigned char logomid[]={0xD6,0xD0,0xD0,0xC4};
unsigned char logoleft[]={0xCF,0xF2,0xD7,0xF3};
unsigned char logoright[]={0xCF,0xF2,0xD3,0xD2};
unsigned char logoup[]={0xCF,0xF2,0xC9,0xCF};
unsigned char logodown[]={0xCF,0xF2,0xCF,0xC2};
unsigned char logoleftup[]={0xD7,0xF3,0xC9,0xCF};
unsigned char logorightup[]={0xD3,0xD2,0xC9,0xCF};
unsigned char logorightdown[]={0xD3,0xD2,0xCF,0xC2};
unsigned char logoleftdown[]={0xD7,0xF3,0xCF,0xC2};

 
int ADXAddress = 0xA7>>1;  //轉換為7位地址
int reading = 0;
int val = 0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;
 
void setup()
{
  Serial.begin(9600);//串口初始化
  LCDA.Initialise(); // 屏幕初始化
  delay(100);
  Wire.begin();  //初始化I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();
  
}
 
void loop()
{

  double tempx=0;
  char strx[10];
  double tempy=0;
  char stry[10];
  double tempz=0;
  char strz[10];
  
  LCDA.CLEAR();//清屏
  LCDA.DisplayString(2,2,show1x,AR_SIZE(show1x)); 
  delay(1000);
  while(1)
  {
    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_X0);
    Wire.write(Register_X1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress,2);
    if(Wire.available()<=2);
    {
      X0 = Wire.read();
      X1 = Wire.read();
      X1 = X1<<8;
      X_out = X0+X1;
    }
   
    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_Y0);
    Wire.write(Register_Y1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress,2);
    if(Wire.available()<=2);
    {
      Y0 = Wire.read();
      Y1 = Wire.read();
      Y1 = Y1<<8;
      Y_out = Y0+Y1;
    }
   
    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_Z0);
    Wire.write(Register_Z1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress,2);
    if(Wire.available()<=2);
    {
      Z0 = Wire.read();
      Z1 = Wire.read();
      Z1 = Z1<<8;
      Z_out = Z0+Z1;
    }
   
    Xg = X_out/256.00;//把輸出結果轉換為重力加速度g,精確到小數點后2位。
    Yg = Y_out/256.00;
    Zg = Z_out/256.00;
    

    Serial.println("X:");
    Serial.println(Xg);
    Serial.println("Y:");
    Serial.println(Yg);
    LCDA.CLEAR();//清屏

    if (Xg>0.2 and Yg<0.2 and Yg>-0.2){
      LCDA.DisplayString(2,2,logoleft,AR_SIZE(logoleft)); 
      delay(500);
  //
    }
    if(Xg<-0.2 and Yg<0.2 and Yg>-0.2){
      LCDA.DisplayString(2,2,logoright,AR_SIZE(logoright));
      delay(500);
  
    }
    if (Xg<0.2 and Xg>-0.2 and Yg<0.2 and Yg>-0.2){
        LCDA.DisplayString(2,2,logomid,AR_SIZE(logomid)); 
        delay(500);
    }
    if (Xg<0.2 and Xg>-0.2 and Yg>0.2){
        LCDA.DisplayString(2,2,logoup,AR_SIZE(logoup)); 
        delay(500);
    }  
    if (Xg<0.2 and Xg>-0.2 and Yg<-0.2){
        LCDA.DisplayString(2,2,logodown,AR_SIZE(logodown)); 
        delay(500);
    } 
    if (Xg>0.2 and Yg<-0.2){
        LCDA.DisplayString(2,2,logoleftdown,AR_SIZE(logoleftdown)); 
        delay(500);
    } 
    if (Xg>0.2 and Yg>0.2){
        LCDA.DisplayString(2,2,logoleftup,AR_SIZE(logoleftup)); 
        delay(500);
    }  
    if (Xg<-0.2 and Yg<-0.2){
        LCDA.DisplayString(2,2,logorightdown,AR_SIZE(logorightdown)); 
        delay(500);
    }  
    if (Xg<-0.2 and Yg>0.2){
        LCDA.DisplayString(2,2,logorightup,AR_SIZE(logorightup)); 
        delay(500);
    }  
  }

 
}

實驗效果:

 

從視頻來看,當ADXL345模塊平放在桌面時,屏幕顯示中心,當向上、下、左、右等八個方向轉動時,屏幕會依次顯示向上、向下、向左、向右等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM