Bouncing

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

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


デバイスを乗せたところ
左からPIC16F640、AQM1248、KXM52-1050、3.3V三端子レギュレータ。後ろには1.5V単四電池3本
main.c
/*
  超小型グラフィックLCD AQM1248 と
   3軸加速度センサー KXM52-1050 を用いて
   8×8の四角を画面でバウンドさせる
*/

sbit _cs   at RC6_bit;
sbit _rs   at RB5_bit;
#define AN5  0x15                  // X軸
#define AN6  0x19                  // Y軸

void writeCommand(unsigned short cmd)
{
  _cs = 0;
  _rs = 0;
  SPI1_Write(cmd);
  _cs = 1;
}

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

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

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

void writeData(unsigned short dat)
{
  _cs = 0;
  _rs = 1;
  SPI1_Write(dat);
  _cs = 1;
}

void clear()
{
  int i,j;
  for(i=0; i<8; i++)
  {
    pageAddressSet(i);
    colAddressSet(0);
    for(j=0; j<128; j++)
    writeData(0);
  }
}

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
  Delay_ms(2);
  writeCommand(0x2E);   // power control 2
  Delay_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 main() {
  int i, j, x, y, tmp;
  unsigned int high;
  float xx, yy, coef;
  int direction=0;
  char buff[10];
  OSCCON = 0x75;          // 内部OSC8MHzで動作させる
  TRISB5_bit = 0;         // を出力に設定する
  TRISC6_bit = 0;         // を出力に設定する
  ANSELH  = 0x06;         // ans11(RB5), ans8(RC6)=0 : digital IO

  /****************************************/
  OPTION_REG = 0;
  WPUA4_bit = 0;              // analog pull-upはしない
  SPI1_Init();      /*master mode
                    clock Fosc/4
                    clock idle state low
                    data transimtted on low to high edge
                    input data sampled at the middle of interval
                    */
  init();
  clear();
  pageAddressSet(5);
  colAddressSet(0);
  startLineSet(0);
  coef = 3.3/256;
  x=0;

  while(1) {
    ADCON0 = AN5;              // X軸
    GO_NOT_DONE_bit = 1;
    while(GO_NOT_DONE_bit);
    high = ADRESH;
    tmp= (int)high-127;
    xx = tmp*coef*100;
    ADCON0 = AN6;              // Y軸
    GO_NOT_DONE_bit = 1;
    while(GO_NOT_DONE_bit);
    high = ADRESH;
    tmp= (int)high-127;
    yy = tmp*coef*100;
    colAddressSet(x);
    if(xx<-0.1 || 0.1<xx)
    {
      direction = xx>0;
      if(!direction) writeData(0);
      for(j=0; j<8; j++) writeData(0xff);
      if(direction) 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) startLineSet(y++);
      else startLineSet(y--);
      if(y>40) y=40;          // 壁にぶつかった時の処理
      if(y<1)  y=1;
    }
    Delay_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 
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 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145