转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi
欢迎加入讨论群 64770604
1、Arduino UNO板
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连接方式的硬件连接图
2、adxl345的连接方法
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)进行转化。如图片:
打开图片取模软件,首先进行参数设置,转出格式选C语言,输出大小128*64,点参数确认。
然后点载入图片,
点击数据保存,得到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的值:
#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模块平放在桌面时,屏幕显示中心,当向上、下、左、右等八个方向转动时,屏幕会依次显示向上、向下、向左、向右等。