Bouncing

加速度センサーとグラフィックLCDを用いてBouncingさせる

超小型グラフィックLCD AQM1248A3軸加速度センサモジュールを用います。


配線

デバイスを乗せたところ
AQM1248A.h
#include "mbed.h"
#ifndef _AQM1248A_
#define _AQM1248A_

#define SPI_FREQ        1000000   // 1MHz
#define SPI_MODE        0
#define SPI_NBIT        8

class AQM1248A : public SPI
{
public:
  AQM1248A(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs);
  void init();
  void writeData(int data);
  void writeCommand(int cmd);
  void diplayOn(bool onoff);
  void startLineSet(int address);
  void pageAddressSet(int address);
  void colAddressSet(int address);
  void clear();
private:
  DigitalOut *_rs;
  DigitalOut _cs;
};

#endif

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 




AQM1248A.cpp
#include"AQM1248A.h"

extern Serial pc;

// CONSTRUCTOR
AQM1248A::AQM1248A(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs) : SPI(mosi, miso, sclk), _cs(cs) {
    this->format(SPI_NBIT, SPI_MODE);
    this->frequency(SPI_FREQ);
    _rs = new DigitalOut(p14);
    init();
}

void AQM1248A::init()
{
  writeCommand(0xAE);   // Display Off
  writeCommand(0xA0);   // ADC normal
  writeCommand(0xC8);   // Common output = reverse/normal 0xC0
  writeCommand(0xA3);   // bias = 1/7
  // 内部レギュレータを順番にONする
  writeCommand(0x2C);   // power control 1
  wait_ms(2);
  writeCommand(0x2E);   // power control 2
  wait_ms(2);
  writeCommand(0x2F);   // power control 3
  // コントラスト設定
  writeCommand(0x23);   // Vo voltage resistor retio set
  writeCommand(0x81);   // Electric volume mode set
  writeCommand(0x1C);   // Electric volume value set
  // 表示設定
  writeCommand(0xA4);   // display all point = normal(全点灯しない)
  writeCommand(0x40);   // display start line = 0
  writeCommand(0xA6);   // Display normal/reverse = normal(白黒反転しない)
  writeCommand(0xAF);   // Display ON


}

void AQM1248A::writeCommand(int cmd)
{
  _cs = 0;
  *_rs = 0;
  this->write(cmd);
  _cs = 1;

}

void AQM1248A::diplayOn(bool onoff)
{// on:1  off:0
  writeCommand(onoff ? 0xAF : 0xAE);
}

// Set the display RAM display start line
void AQM1248A::startLineSet(int address)
{
  writeCommand(0x40 | address);
}

// Set the display RAM page address
void AQM1248A::pageAddressSet(int address)
{
  writeCommand(0xB0 | address&7);
}

void AQM1248A::colAddressSet(int address)
{
    int upper = address>>4;
    int lower = address & 0x0f;
    writeCommand(0x10 | upper);
    writeCommand(0x00 | lower);
}
void AQM1248A::writeData(int data)
{
  _cs = 0;
  *_rs = 1;
  this->write(data);
  _cs = 1;
}

void AQM1248A::clear()
{
    for(int i=0; i<8; i++)
    {
        pageAddressSet(i);
        colAddressSet(0);
        for(int j=0; j<128; j++)
            writeData(0);
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 


Mainプログラム



Mainプログラム
#include "AQM1248A.h"

AQM1248A gr(p11, p12, p13, p8, p14);// mosi, miso, sclk, cs, rs
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx
AnalogIn X(p20);
AnalogIn Y(p19);

int main()
{
    gr.startLineSet(0);
    gr.clear();
    int x=0;
    bool direction=false;
    int y=0;
    bool Ydirection=false;
    gr.pageAddressSet(5);
    gr.colAddressSet(0);
    for(int j=0; j<8; j++) gr.writeData(0xff);
    while(1)
    {
        float xx = (X.read()*3.3-1.652)*50;
        float yy = (Y.read()*3.3-1.65)*5;
        gr.colAddressSet(x);
        if(xx<-0.1 || 0.1<xx)
        {
            direction = xx<0;
            if(!direction) gr.writeData(0);
            for(int j=0; j<8; j++) gr.writeData(0xff);
            if(direction) gr.writeData(0);

            if(!direction) x++;
            else x--;
            if(x>120) x = 120;
            if(x<=0)  x = 0;
        }
        if(yy<-0.1 || 0.1<yy)
        {
            if(yy>0) gr.startLineSet(y++);
            else gr.startLineSet(y--);
            if(y>40) y=40;
            if(y<1)  y=1;
        }
        wait_ms(10);
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46