線路配置(並行)
RS = 17; Analog Pin3
RW = 16; Analog Pin2
EN = 18; Analog Pin4
D0 = 8;
D1 = 9;
D2 = 10;
D3 = 11;
D4 = 4;
D5 = 5;
D6 = 6;
D7 = 7;
PIN15 PSB = 5V;(並行)
VR (3k) 配置
+ 5v
Analog Pin1
GND
官方12864網頁
http://playground.arduino.cc/Code/LCD12864
使用 Lib
LCD12864
http://playground.arduino.cc/uploads/Code/LCD12864.zip
官方原始碼
http://playground.arduino.cc/uploads/Code/pongLCD.zip
參考原始碼
//http://playground.arduino.cc/Code/LCD12864補充說明
#include <LCD12864.h>
int score = 0;//分數
boolean lost = false;//遊戲結束
int relive = 0;//復活讀秒
int top=128,down=159,left=0,right=63;
int Xpos = 0;//球座標 x
int Ypos = 133;//球座標 y
int momentumX = 2;//x增量
int momentumY = 1;//y增量
boolean momr = true;//球向右(x)
boolean momd = true;//球向下(y)
int PaddlePos = 139;// 130<PaddlePos<148 (128<y<160)
void setup() {
LCDA.Initialise(); // INIT SCREEN
delay(500); // Some delay
LCDA.Render(); // Start the Rendering process
Title(); // 畫 ASCII 字元
delay(500); // Some Delay
}
void loop(){
int tmp;
tmp = analogRead(1);// 讀取VR值 0-1023
PaddlePos = map(tmp,0,1023,129,149);// 把 0-1023 值轉為 130-148
run();
}
void AI(){// BALL MOVEMENT 球移動
//left=0,right=63
if (Xpos+momentumX < right && momr == true) {
Xpos+=momentumX;//x->
}else if (Xpos+momentumX >= right && momr == true) {
Xpos-=momentumX;//x->反彈
momr = false;//<-x
}else if (Xpos-momentumX > left && momr == false){
Xpos-=momentumX;//<-x
}else if (Xpos-momentumX <= left && momr == false){//反彈<-x
if (Ypos+1 >= PaddlePos && Ypos < PaddlePos+10) {//擊中
Xpos+=momentumX;//反彈||<-x
momr = true;//x->
scored();//計分
// 變化球,棒子兩端+-3
if (Ypos < PaddlePos+3 || Ypos-1 > PaddlePos+7) {
if (momentumY < 4) momentumY +=2;
}else{
if (momentumY > 1) momentumY -=1;
}
}else{// 沒擊中
lost = true;
}
}
//top=128,down=159
if (Ypos+momentumY < down && momd == true) {//向下
Ypos+=momentumY;
}else if (Ypos+momentumY >= down && momd == true) {//向下反彈
Ypos-=momentumY;
momd = false;
}else if (Ypos-momentumY > top && momd == false){//向上
Ypos-=momentumY;
}else if (Ypos-momentumY <= top && momd == false){//向上反彈
Ypos+=momentumY;
momd = true;
}
}
void run() {//循環
if (lost == false) {
AI();
LCDA.DumpScreenBuffer(0,0,8,32);//清空記憶體資料(X,Y,maxX,maxY)
// Draw Room (x,y,w,h),畫框(0<=X<=63),(128<=Y<=159)
//top=128,down=159,left=0,right=63
LCDA.FillRectangle(left,top,right,1);
LCDA.FillRectangle(right,top,1,32);//60,128
LCDA.FillRectangle(left,down,right,1);//0,158
// Draw Ball 畫球
LCDA.FillRectangle(Xpos,Ypos,2,2);// 2*3 球
// Draw Paddle 畫板子
LCDA.FillRectangle(1,PaddlePos,2,10);// 2*10 板子
LCDA.RenderScreenBuffer(0,0,8,32);//送出記憶體資料
}else{
relive++;
if(relive>3000){//復活讀秒=3秒
lost = false;
relive=0;
score=0;
}
}
}
// 秀數字
void numbertobinary(char num) { //RS=1,RW=0,寫資料到內部 RAM (RS,RW,D7,D6,D5,D4,D3,D2,D1,D0)
if (num == 0) LCDA.setPins(1,0,0,0,1,1,0,0,0,0); //30h=48=0
else if (num == 1) LCDA.setPins(1,0,0,0,1,1,0,0,0,1); //31h=49=1
else if (num == 2) LCDA.setPins(1,0,0,0,1,1,0,0,1,0); //32h=50=2
else if (num == 3) LCDA.setPins(1,0,0,0,1,1,0,0,1,1); //33h=51=3
else if (num == 4) LCDA.setPins(1,0,0,0,1,1,0,1,0,0); //34h=52=4
else if (num == 5) LCDA.setPins(1,0,0,0,1,1,0,1,0,1); //35h=53=5
else if (num == 6) LCDA.setPins(1,0,0,0,1,1,0,1,1,0); //36h=54=6
else if (num == 7) LCDA.setPins(1,0,0,0,1,1,0,1,1,1); //37h=55=7
else if (num == 8) LCDA.setPins(1,0,0,0,1,1,1,0,0,0); //38h=56=8
else if (num == 9) LCDA.setPins(1,0,0,0,1,1,1,0,0,1); //39h=57=9
}
void Title() { // 秀 ASCII 字元
LCDA.Draw(false,25,0);//(sw,x,?)
delay(1);
byte title[] = {
0,1,1,0,1,0,1,1,//0x6b=k
0,1,1,0,1,1,1,1,//0x6f=o
0,1,1,1,0,1,1,1,//0x77=w
0,1,1,0,0,0,0,1,//0x61=a
0,1,1,0,1,1,0,0,//0x6f=l
0,1,1,0,0,0,0,1,//0x61=a
0,0,1,0,0,1,1,1,//0x27='
0,1,1,1,0,0,1,1,//0x73=s
0,0,1,0,0,0,0,0,//0x20=
0,1,1,0,1,0,0,0,//0x68=h
0,1,1,0,1,1,1,1,//0x6f=o
0,1,1,0,1,1,0,1,//0x6d=m
0,1,1,0,0,1,0,1 //0x65=e
};
int count = 0;
int counter = 0;
while (count < sizeof(title)/sizeof(byte)) {
for (int i = count; i < count+8; i++) {
LCDA.temp[counter] = title[i];
counter++;
}
LCDA.setPins(1,0,
LCDA.temp[0],LCDA.temp[1],LCDA.temp[2],LCDA.temp[3],
LCDA.temp[4],LCDA.temp[5],LCDA.temp[6],LCDA.temp[7]);
count += 8;
counter = 0;
}
LCDA.Draw(true,0,0);
}
void scored() { // 計分+1
LCDA.Draw(false,12,20);
delay(1);
score++;
momentumX = random(3, 6);
if (score < 10) {
numbertobinary('0');
numbertobinary('0');
numbertobinary(score);
}else if (score < 100) {
int res = score/10;
int lef = score%10;
numbertobinary('0');
numbertobinary(res);
numbertobinary(lef);
}else if (score < 1000) {
int res = score/100;
int lef = score%100;
int res2 = lef/10;
int lef2 = lef%10;
numbertobinary(res);
numbertobinary(res2);
numbertobinary(lef2);
}
LCDA.Draw(true,0,0);
}
void Title() { // 秀 ASCII 字元
LCDA.Draw(false,25,0);//(sw,x,?)
在文字顯示中,X座標指定,是從左上角,由0開始,分成四行,一直到右下角為止,如下表所示。
ASCII 字碼輸入
LCDA.setPins(1,0,0,0,1,1,0,0,0,0); //30h=48=0
setPins() 的位元如下
(RS,RW,D7,D6,D5,D4,D3,D2,D1,D0)
RS=1,RW=0,時,為寫資料到內部 DRAM,D0-D7 為 ASCII Code,請參考下表
例如我們要在螢幕中央顯示 7,那 X 就是 12,然後先關掉繪圖,並指定 X
LCDA.Draw(false,12,0);
接著輸入 7 的 ASCII CODE,查上表為 0x37 ,二進位碼 00110111 ,可由計算機轉換
然後寫入,頭兩個位元必須是 RS=1,RW=0,後面就是二進位碼 00110111,如下
LCDA.setPins(1,0,0,0,1,1,0,1,1,1);
最後就是把繪圖打開,就會顯示了
LCDA.Draw(true,0,0);
大大我下載您給的連結 可是我的函示庫錯誤
回覆刪除