Bouncing

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

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



AQM1248A.cs
using System;
using System.Threading;         // 追加
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;  // 追加、参照設定にも追加
using GHI.OSHW.Hardware;        // 追加、参照設定にも追加
using GHI.Hardware.FEZCerb;     // 追加、参照設定にも追加
using BoeBotLib;
using Pin = Microsoft.SPOT.Hardware.Cpu.Pin;

class AQM1248A
{
    DigitalOut _cs, _rs;
    SPI spi;
    byte[] buffer = new byte[1];

  public AQM1248A(Pin cs, Pin rs)      // SPI1固定
  {
      SPI.Configuration Config =
         new SPI.Configuration(Cpu.Pin.GPIO_Pin1,
         false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
      spi = new SPI(Config);
      _rs = new DigitalOut(rs);
      _cs = new DigitalOut(cs);
      init();
  }

  void 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
      CPU.wait_ms(2);
      writeCommand(0x2E);   // power control 2
      CPU.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 writeCommand(byte cmd)
  {
      buffer[0] = cmd;
      _cs <<= 0;
      _rs <<= 0;
      spi.Write(buffer);
      _cs <<== 1;
  }

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

  // Set the display RAM display start line
  public void startLineSet(byte address)
  {
    writeCommand((byte)(0x40 | address));
  }

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

  public void colAddressSet(byte address)
  {
      byte upper = (byte)(address >> 4);
      byte lower = (byte)(address & 0x0f);
      writeCommand((byte)(0x10 | upper));
      writeCommand((byte)(0x00 | lower));
  }

  public void writeData(byte data)
  {
      buffer[0] = data;
      _cs <<== 0;
      _rs <<== 1;
      spi.Write(buffer);
      _cs <<== 1;
  }

  public void clear()
  {
    for(int i=0; i<=8; i++)
    {
      pageAddressSet((byte)i);
      //writeCommand(0xB0 | i);
      colAddressSet(0);
      //writeCommand(0x10);
      //writeCommand(0x00);
      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 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 


Mainプログラム



Mainプログラム
using System;
using System.Threading;         // 追加
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;  // 追加、参照設定にも追加
using GHI.OSHW.Hardware;        // 追加、参照設定にも追加
using GHI.Hardware.FEZCerb;     // 追加、参照設定にも追加
using BoeBotLib;
using Pin = GHI.Hardware.FEZCerb.Pin;

/*
 * 	SPI1 SCK  : P12
 * 	SPI1 MOSI : P20
 * 	cs        : P09
 * 	rs        : P11
 */
namespace Bouncing
{
    public class Program
    {
        public static void Main()
        {
            OutputPort LED1 = new OutputPort(Pin.PC0, false);
            AQM1248A gr = new AQM1248A(CPU.P09,CPU.P11); // cs, rs
            ADcon X = new ADcon(CPU.P24);
            ADcon Y = new ADcon(CPU.P23);
            byte x = 0, y=0;
            gr.clear();
            gr.pageAddressSet((byte)5);
            gr.startLineSet((byte)0);
            bool direction = false;
            while (true)
            {
                double xx = (X.Read() * 3.3 - 1.66) * 50;
                double 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 i = 0; i < 8; i++) gr.writeData((byte)0xff);
                    if (direction) gr.writeData(0);
                    if (!direction) x++;
                    else x--;
                    if (x > 120) x = 120;
                    if (x <= 1) { x = 1; gr.colAddressSet(0); gr.writeData(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;
                }
                CPU.delay(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 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58